标签:
突然发现我不会分层图最短路,写一发。 就是同层中用双向边相连,用单向边连下一层
1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <string> 5 #include <cstring> 6 #include <queue> 7 #include <vector> 8 #define pa pair<int,int> 9 #define mp make_pair 10 #define fi first 11 #define se second 12 using namespace std; 13 const int Maxm=5000100; 14 const int Maxn=1000100; 15 const int Inf=0x3f3f3f3f; 16 priority_queue<pa,vector<pa>,greater<pa> > Q; 17 int head[Maxn],dis[Maxn],n,m,k,S,T,u,v,w,cnt; 18 struct Edge{int to,next,w;}edge[Maxm]; 19 inline void Add(int u,int v,int w) 20 {edge[cnt].to=v;edge[cnt].next=head[u];edge[cnt].w=w;head[u]=cnt++;} 21 inline void ADD(int u,int v,int w) {Add(u,v,w),Add(v,u,w);} 22 inline int Get(int u,int Dep) {return u+(Dep-1)*n;} 23 24 void Dij() 25 { 26 Q.push(mp(0,Get(S,1))); 27 for (int i=0;i<=Get(n,k);i++) dis[i]=Inf; 28 dis[Get(S,1)]=0; 29 while (!Q.empty()) 30 { 31 int u=Q.top().se; Q.pop(); 32 for (int i=head[u];i!=-1;i=edge[i].next) 33 if (dis[u]+edge[i].w<dis[edge[i].to]) 34 { 35 dis[edge[i].to]=dis[u]+edge[i].w; 36 Q.push(mp(dis[edge[i].to],edge[i].to)); 37 } 38 } 39 } 40 int main() 41 { 42 scanf("%d%d%d",&n,&m,&k); k++; 43 scanf("%d%d",&S,&T); S++,T++; 44 memset(head,-1,sizeof(head)); 45 for (int i=1;i<=m;i++) 46 { 47 scanf("%d%d%d",&u,&v,&w); u++,v++; 48 for (int j=1;j<=k;j++) ADD(Get(u,j),Get(v,j),w); 49 for (int j=1;j<k;j++) 50 Add(Get(u,j),Get(v,j+1),0), 51 Add(Get(v,j),Get(u,j+1),0); 52 } 53 Dij(); 54 printf("%d\n",dis[Get(T,k)]); 55 return 0; 56 }
标签:
原文地址:http://www.cnblogs.com/yyjxx2010xyu/p/5533327.html