Rokua P3003 [Usaco10dec] apple on delivery apple Delivery

Source: Internet
Author: User

Rokua P3003 [Usaco10dec] apple on 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, 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 sample #: Copy
Output Example # #: Replication


idea: SPFA + SLF optimization (never made before) Difficulty: Improve the +/province-(self-think the difficulty should be a little lower)
Next, let's talk about the (being) course of the problem.
First of all, I looked at the problem is not running on both sides of the SPFA, and then ...
#include <algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<queue>#defineM 200005#defineMAXN 0x7fffffffusing namespaceStd;queue<int>Q;intm, n, S, t1, T2;inttot, Minn;intDis[m], vis[m];intto[m*2], net[m*2], head[m*2], cap[m*2];voidAddintUintVintW) {to[++tot] = v; Net[tot] = Head[u]; Head[u] = tot; Cap[tot] =W; to[++tot] = u; Net[tot] = Head[v]; HEAD[V] = tot; Cap[tot] =W;}voidSPFA (intx) { for(inti =1; I <= N; i++) Vis[i] =0, dis[i] =MAXN; DIS[X]=0; VIS[X] =1; Q.push (x);  while(!Q.empty ()) {        inty = Q.front (); Q.pop (); Vis[y] =0;  for(inti = head[y]; I i =Net[i]) {            intt =To[i]; if(Dis[t] > dis[y]+Cap[i]) {Dis[t]= dis[y]+Cap[i]; if(!vis[t]) vis[t] =1, Q.push (t); }        }    }}intMain () {scanf ("%d%d%d%d%d", &m, &n, &s, &AMP;T1, &T2);  for(inti =1; I <= m; i++) {        intA, B, C; scanf ("%d%d%d", &a, &b, &c);    Add (A, B, c);    } SPFA (t1); Minn= Dis[s] +DIS[T2];    SPFA (T2); Minn= MIN (Minn, dis[s]+Dis[t1]); printf ("%d", Minn); return 0;}
naked Spfa,re, two points.

It's re! I should be driving big enough, then the array opened a 0, all open long long,and then ...

#include <algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<queue>#defineLL Long Long#defineM 2000005#defineMAXN 0x7fffffffusing namespaceStd;queue<LL>Q; LL m, n, S, T1, T2; LL tot, Minn; LL Dis[m], vis[m]; LL to[m*2], net[m*2], head[m*2], cap[m*2];voidAdd (ll u, ll V, ll W) {to[++tot] = v; Net[tot] = Head[u]; Head[u] = tot; Cap[tot] =W; to[++tot] = u; Net[tot] = Head[v]; HEAD[V] = tot; Cap[tot] =W;}voidSPFA (LL x) { for(LL i =1; I <= N; i++) Vis[i] =0, dis[i] =MAXN; DIS[X]=0; VIS[X] =1; Q.push (x);  while(!Q.empty ()) {        inty = Q.front (); Q.pop (); Vis[y] =0;  for(LL i = head[y]; i; i =Net[i]) {LL T=To[i]; if(Dis[t] > dis[y]+Cap[i]) {Dis[t]= dis[y]+Cap[i]; if(!vis[t]) vis[t] =1, Q.push (t); }        }    }}intMain () {scanf ("%lld%lld%lld%lld%lld", &m, &n, &s, &AMP;T1, &T2);  for(LL i =1; I <= m; i++) {LL A, B, C; scanf ("%lld%lld%lld", &a, &b, &c);    Add (A, B, c);    } SPFA (t1); Minn= Dis[s] +DIS[T2];    SPFA (T2); Minn= MIN (Minn, dis[s]+Dis[t1]); printf ("%lld", Minn); return 0;} 
as a result, there is no egg, continue re two points.

Then I was wise (really no way) asked the elder sister, the result she told me: "This problem I remember to use SLF optimization" Qaq

But I won't.

Then learned elder sister to add the SLF after the change, but do not know why, I can not go through the example (crying)

By comparison found that the long long did not open the result is still on the drip.

#include <algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<deque>#defineM 200005#defineMAXN 0x7fffffffusing namespaceStd;deque<int>Q;intm, n, S, t1, T2;inttot, Minn;intDis[m], vis[m];intto[m*2], net[m*2], head[m*2], cap[m*2];voidAddintUintVintW) {to[++tot] = v; Net[tot] = Head[u]; Head[u] = tot; Cap[tot] =W; to[++tot] = u; Net[tot] = Head[v]; HEAD[V] = tot; Cap[tot] =W;}voidSPFA (intx) { for(inti =1; I <= N; i++) Vis[i] =0, dis[i] =MAXN; DIS[X]=0; VIS[X] =1; q.push_back (x);  while(!Q.empty ()) {        inty = Q.front (); Q.pop_front (); Vis[y] =0;  for(inti = head[y]; I i =Net[i]) {            intt =To[i]; if(Dis[t] > dis[y]+Cap[i]) {Dis[t]= dis[y]+Cap[i]; if(!Vis[t]) {Vis[t]=1; if(Q.empty () | | dis[t]<Dis[q.front ()]) Q.push_front (t); ElseQ.push_back (t); }            }        }    }}intMain () {scanf ("%d%d%d%d%d", &m, &n, &s, &AMP;T1, &T2);  for(inti =1; I <= m; i++) {        intA, B, C; scanf ("%d%d%d", &a, &b, &c);    Add (A, B, c);    } SPFA (t1); Minn= Dis[s] +DIS[T2];    SPFA (T2); Minn= MIN (Minn, dis[s]+Dis[t1]); printf ("%d", Minn); return 0;}
long long changed back to int and then A.

10,000 heads in my heart * * * * *

But fortunately, finally, it's a.

Rokua P3003 [Usaco10dec] apple on delivery apple Delivery

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.