Layered SPFA.
Vis=false when the team retired.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#defineMAXV 10050#defineMaxe 100500using namespacestd;structedge{intV,W,NXT;} E[maxe];intn,m,k,s,t,a,b,c,dis[maxv][ -],g[maxv],nume=0, vis[maxv][ -];queue<int>Q;voidAddedge (intUintVintW) {e[++nume].v=v; E[NUME].W=W; E[NUME].NXT=G[u]; G[u]=Nume;}voidSPFA () { while(!q.empty ()) Q.pop (); memset (DIS,0x7f,sizeof(DIS)); Q.push (s); Q.push (0); vis[s][0]=true;d is[s][0]=0; while(!Q.empty ()) { intDot,now; Dot=Q.front (); Q.pop (); now=Q.front (); Q.pop (); Vis[dot][now]=false; for(intI=g[dot];i;i=e[i].nxt) { intv=e[i].v; if(dis[v][now]>dis[dot][now]+E[I].W) {Dis[v][now]=dis[dot][now]+E[I].W; if(!Vis[v][now]) {Q.push (v); Q.push (now); Vis[v][now]=true; } } if((now!=k) && (dis[v][now+1]>Dis[dot][now])) {Dis[v][now+1]=Dis[dot][now]; if(!vis[v][now+1]) {Q.push (v); Q.push ( now+1); Vis[v][now+1]=true; } } } }}intMain () {scanf ("%d%d%d",&n,&m,&k); scanf ("%d%d", &s,&t); s++;t++; for(intI=1; i<=m;i++) {scanf ("%d%d%d",&a,&b,&c); Addedge (A+1, B +1, c); Addedge (b+1, A +1, c); } SPFA (); intans=0x7f7f7f7f; for(intI=0; i<=k;i++) ans=min (ans,dis[t][i]); printf ("%d\n", ans); return 0;}
Bzoj 2763 Flight Route