HDU 5669 Road(线段树建树)(分层图最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5669

【分析】线段树建树+分层图最短路

#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef pair<int,int>pii;
typedef long long LL;
const int N=6e5+;
const int mod=1e9+;
int n,m,s,k,t,cnt,idl[N<<],idr[N<<];
bool vis[N][];
LL d[N][];
vector<pii>edg[N];
void buildl(int rt,int l,int r)
{
idl[rt]=++cnt;
if(l==r)return ;
int m=l+r>>;
buildl(rt<<,l,m);
buildl(rt<<|,m+,r);
edg[idl[rt<<]].push_back(make_pair(idl[rt],));
edg[idl[rt<<|]].push_back(make_pair(idl[rt],));
}
void buildr(int rt,int l,int r)
{
idr[rt]=++cnt;
if(l==r)return ;
int m=l+r>>;
buildr(rt<<,l,m);
buildr(rt<<|,m+,r);
edg[idr[rt]].push_back(make_pair(idr[rt<<],));
edg[idr[rt]].push_back(make_pair(idr[rt<<|],));
}
void pre(int rt,int l,int r)
{
if(l==r)
{
edg[l].push_back(make_pair(idl[rt],));
edg[idr[rt]].push_back(make_pair(l,));
return;
}
int m=l+r>>;
pre(rt<<,l,m);
pre(rt<<|,m+,r);
}
void addl(int rt,int l,int r,int L,int R,int w){
if(L<=l&&r<=R){
edg[idl[rt]].push_back(make_pair(cnt,w));
return;
}
int mid=(l+r)/;
if(L<=mid)addl(rt*,l,mid,L,R,w);
if(R>mid)addl(rt*+,mid+,r,L,R,w);
}
void addr(int rt,int l,int r,int L,int R){
if(L<=l&&r<=R){
edg[cnt].push_back(make_pair(idr[rt],));
return;
}
int mid=(l+r)/;
if(L<=mid)addr(rt*,l,mid,L,R);
if(R>mid)addr(rt*+,mid+,r,L,R);
}
struct man{
int v;
int c;
LL w;
bool operator<(const man &e)const{
return w>e.w;
}
};
priority_queue<man>q;
void dij(int s){
memset(d,-,sizeof d);memset(vis,,sizeof vis);
d[s][]=;
q.push(man{s,,});
while(!q.empty()){
int u=q.top().v,c=q.top().c;q.pop();
if(vis[u][c])continue;
vis[u][c]=;
for(int i=;i<edg[u].size();++i){
int v=edg[u][i].first,w=edg[u][i].second;
if(!vis[v][c]&&(d[v][c]==-||d[v][c]>d[u][c]+w)){
d[v][c]=d[u][c]+w;
q.push(man{v,c,d[v][c]});
}
if(c<k){
if(!vis[v][c+]&&(d[v][c+]==-||d[v][c+]>d[u][c])){
d[v][c+]=d[u][c];
q.push(man{v,c+,d[v][c+]});
}
}
}
}
}
int main()
{
int x,y,w,l,r;
int T;
scanf("%d",&T);
while(T--){
for(int i=;i<N;i++)edg[i].clear();
scanf("%d%d%d",&n,&m,&k);
cnt=n;
buildl(,,n);
buildr(,,n);
pre(,,n);
while(m--)
{
++cnt;
scanf("%d%d%d%d%d",&x,&y,&l,&r,&w);
addl(,,n,x,y,w);
addr(,,n,l,r);
cnt++; //此处要注意
addl(,,n,l,r,w);
addr(,,n,x,y);
}
dij();
LL ans=;
for(int i=;i<=k;i++)ans=min(ans,d[n][i]);
if(ans!=-)printf("%lld\n",ans);
else puts("CreationAugust is a sb!");
}
return ;
}
上一篇:HDU 3591 (完全背包+二进制优化的多重背包)


下一篇:钉钉C# 使用数据接口要注意的问题