SPFA Dual-ended queue optimization.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<deque>#defineMAXV 100050#defineMaxe 400050#defineINF 0x7f7f7f7fusing namespacestd;structedge{intV,W,NXT;} E[maxe];intN,m,s,t1,t2,nume=0, g[maxv],dis[maxv],a,b,c,ans=inf;BOOLVIS[MAXV];d eque<int>Q;voidAddedge (intUintVintW) {e[++nume].v=W; E[NUME].W=W; E[NUME].NXT=G[u]; G[u]=Nume;}voidSPFA (intx) { while(!q.empty ()) Q.pop_front (); memset (Vis,false,sizeof(VIS)); Fill (Dis+1, dis+n+1, INF); Q.push_front (x); Vis[x]=true;d is[x]=0; while(!Q.empty ()) { intHead=q.front (); Q.pop_front (); vis[head]=false; for(intI=g[head];i;i=e[i].nxt) { intv=e[i].v; if(dis[v]>dis[head]+E[I].W) {Dis[v]=dis[head]+E[I].W; if(!Vis[v]) { if(Q.empty ()) Q.push_front (v); Else if(dis[v]<Dis[q.front ()]) Q.push_front (v); ElseQ.push_back (v); } } } }}intMain () {scanf ("%d%d%d%d%d",&m,&n,&s,&t1,&T2); for(intI=1; i<=m;i++) {scanf ("%d%d%d",&a,&b,&c); Addedge (A,B,C); Addedge (B,A,C); } SPFA (t1); Ans=dis[s]+DIS[T2]; SPFA (T2); Ans=min (ans,dis[s]+Dis[t1]); printf ("%d\n", ans); return 0;}
Bzoj 2100 Apple Delivery