P3003 [Usaco10dec] apples delivery Apple Delivery title description
Bessie has a crisp red apples to deliver to both of her friends in the herd. Of course, she travels the C (1 <= c <= 200,000)
Cowpaths which is arranged as the usual graph which connects P (1 <= p <= 100,000) pastures conveniently numbered F Rom 1..p:no Cowpath leads from a pasture to itself, cowpaths is bidirectional, each cowpath have an associated distance, And, best of all, it's always possible-get from any pasture to any other pasture. Each cowpath connects, differing pastures p1_i (1 <= p1_i <= p) and p2_i (1 <= p2_i <= p) with a distance b Etween them of d_i. The sum of the distances d_i does not exceed 2,000,000,000.
What's the minimum total distance Bessie must travel to deliver both apples by starting at pasture PB (1 <= PB <= P ) and visiting Pastures PA1 (1 <= PA1 <= p) and PA2 (1 <= PA2 <= p) in any order. All three of these pastures is distinct, of course.
Consider this map of bracketed pasture numbers and cowpaths with distances:
3 2 2 [1]-----[2]------[3]-----[4] \ / \ / 7\ /4 \3 /2 \ / \ / [5]-----[6]------[7] 1 2
If Bessie starts at pasture [5] and delivers apples to pastures [1] and [4], she best path is:
5, 6-> 7, 4, 3 , 2, 1
With a total distance of 12.
Bessie had two sweet and crisp red apples to give to her two friends. Of course, she can go. The C (1<=c<=200000) "Cattle Road" is contained in a common figure, containing the P (1<=p<=100000) pasture, labeled 1, respectively. P. No cattle road will go back to itself from a pasture. "Cattle Road" is two-way, each cattle road will be marked a distance. Most importantly, every ranch can lead to another ranch. Each cattle road is connected to two different pastures p1_i and p2_i (1<=p1_i,p2_i<=p), and the distance is d_i. The sum of the distances of all cattle roads is not greater than 2000000000.
Now, Bessie will start from the ranch PB to Pa_1 and pa_2 Ranch each to send an apple (pa_1 and pa_2 order can be swapped), then the shortest distance is how much? Of course, PB, pa_1 and pa_2 are different.
Input output Format input format:
Line 1:line 1 contains five space-separated integers:c, P, PB, PA1, and PA2
- Lines 2..c+1:line i+1 describes Cowpath I by naming II pastures it connects and the distance between Them:p1_i, P2_i, D _i
Output format:
- Line 1:the Shortest distance Bessie must travel to deliver both apples
Input and Output Sample input example # #:
Sample # # of output:
/*It 's basically a short-circuit template problem. is to find the shortest path between T1 and T2 A1 and then ask Min (Dis[s][t1],dis[s][t2]) to add the two can be thought that this is not so simple, just write a result I used the heap optimization dij*/#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespacestd;#defineMAXN 100010intN,M,S,T1,T2,DIS[MAXN],NUM,HEAD[MAXN];BOOLVIS[MAXN];structnode{intid,d; BOOL operator< (ConstNode B)Const{ returnD>B.D; }};structnode{intTo,pre,v;} e[200010*2];voidInsert (int from,intTo,intv) {e[++num].to=to ; E[NUM].V=v; E[num].pre=head[ from]; head[ from]=num;}voidDij (intStintTT1,inttt2) { intflag=0; Priority_queue<node>Q; memset (DIS,127/3,sizeof(DIS)); memset (Vis,0,sizeof(VIS)); DIS[ST]=0; node now; Now.id=st;now.d=0; Q.push (now); while(!Q.empty ()) { Now=q.top (); Q.pop (); intu=now.id; if(Vis[u])Continue; Vis[u]=1; if(U==TT1) flag++; if(U==TT2) flag++; if(flag==2)return; for(intI=head[u];i;i=e[i].pre) { intto=e[i].to; if(dis[u]+e[i].v<Dis[to]) {Dis[to]=dis[u]+e[i].v; Node NXT;NXT.D=dis[to];nxt.id=to ; Q.push (NXT); } } }}intMain () {//freopen ("Cola.txt", "R", stdin);scanf"%d%d%d%d%d",&m,&n,&s,&t1,&T2); intx, Y, Z for(intI=1; i<=m;i++) {scanf ("%d%d%d",&x,&y,&z); Insert (x, y, z); Insert (Y,X,Z); } intA1,a2=0x7fffffff; Dij (S,T1,T2); A1=min (dis[t1],dis[t2]); Dij (T1,T2,T2); A2=min (DIS[T2],A2); Dij (T2,T1,T1); A2=min (DIS[T1],A2); cout<<a1+A2;}
Rokua P3003 [Usaco10dec] apple on delivery apple Delivery