Bzoj 2100: [Usaco2010 dec]apple Delivery "SPFA"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.