Rokua Data Strong ah, ordinary SPFA Open O2 can not live, to add double-ended queue optimization
Because it is a two-way edge, so Dis (u,v) =dis (v,u), so PA1 and PA2 as the starting point SPFA again, representing PB-->PA1-->PA2 and PB-->PA2-->PA1, take a min to
#include <iostream>#include <cstdio>#include <queue>using namespaceStdConst intn=200005, inf=2e9+7;intM,n,a,b1,b2,h[n],cnt,dis[n];BOOLV[n];structqwe{intNe,to,va;} e[n<<2];intRead () {intR=0, f=1;CharP=getchar (); while(p>' 9 '|| p<' 0 ') {if(p=='-') f=-1; P=getchar (); } while(p>=' 0 '&&p<=' 9 ') {r=r*Ten+p-48; P=getchar (); }returnR*f;}voidAddintUintVintW) {cnt++; E[cnt].ne=h[u]; E[cnt].to=v; E[cnt].va=w; h[u]=cnt;}voidSPFA (ints) {deque<int>q; for(intI=1; i<=n;i++) Dis[i]=inf; dis[s]=0, v[s]=1, Q.push_back (s); while(!q.empty ()) {intU=q.front (); Q.pop_front (); v[u]=0; for(intI=h[u];i;i=e[i].ne)if(Dis[e[i].to]>dis[u]+e[i].va) {Dis[e[i].to]=dis[u]+e[i].va;if(!v[e[i].to]) {v[e[i].to]=1;if(!q.empty () &&dis[q.front ()]>dis[e[i].to]) Q.push_front (e[i].to);ElseQ.push_back (e[i].to); } } }}intMain () {M=read (), N=read (), A=read (), B1=read (), B2=read (); for(intI=1; i<=m;i++) {intX=read (), Y=read (), Z=read (); Add (x, Y, z), add (y,x,z); } SPFA (B1);intANS=DIS[A]+DIS[B2]; SPFA (B2); printf"%d\n", Min (ans,dis[a]+dis[b1]));return 0;}
Bzoj 2100: [Usaco2010 dec]apple Delivery "SPFA"