P3003 [Usaco10dec] apple delivery apples Delivery

Source: Internet
Author: User

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:
1 /*This problem is very good to do, directly set SLF optimization template, run two times to take the minimum value, I began to put SLF and lll optimization together added, but because the LLL is constant card burst, card to exponential level, and then how to optimize will t off the second point, If using LLL optimization will t lose 4 points (I personally experiment), with SLF can drop, and soon. Go directly to the code. */2 3#include <algorithm>4#include <iostream>5#include <cstdlib>6#include <cstring>7#include <climits>8#include <cstdio>9#include <string>Ten#include <cmath> One#include <stack> A#include <queue> -#include <deque> - using namespacestd; the Const intgg=450000; - Const intinf=1e9; - intHead[gg]; - structnode + { -     intNext; +     intW; A     intto ; at } A[gg]; - intDis[gg]; - BOOLVis[gg]; - intc,p,pb,pa1,pa2,cnt; - intans; - intAns2; in intSum,tot; - intrans; toInlinevoidAddintIintJintW) + { -a[++cnt].to=J; thea[cnt].next=Head[i]; *a[cnt].w=W; $head[i]=CNT;Panax Notoginseng } -InlinevoidSPFA (ints) the { +deque<int>Q; Amemset (Vis,false,sizeof(Vis)); thememset (DIS,0x7f,sizeof(DIS)); +dis[s]=0; -vis[s]=true; $ Q.push_back (s); $      while(!q.empty ()) -     { -         intu=Q.front (); the Q.pop_front (); -vis[u]=false;Wuyi          for(RegisterintI=head[u]; I I=a[i].next) the         { -             intv=a[i].to; Wu             if(dis[v]>dis[u]+A[I].W) -             { Aboutdis[v]=dis[u]+A[I].W; $                 if(!Vis[v]) -                 { -vis[v]=true; -                     if(Q.empty () | | Dis[v]>Dis[q.front ()]) A                     { + Q.push_back (v); the                     } -                     Else $ Q.push_front (v); the                 } the             } the         } the     } - } in intMain () the { thescanf"%d%d%d%d%d",&c,&p,&pb,&pa1,&PA2); About      for(RegisterintI=1; i<=c; i++) the     { the         intx, y, z thescanf"%d%d%d",&x,&y,&z); + Add (x, y, z); - Add (y,x,z); the     }Bayi SPFA (PA1); theans+=dis[pa2]+DIS[PB]; the SPFA (PA2); -ans2+=dis[pa1]+DIS[PB]; -rans=min (ans,ans2); theprintf"%d\n", rans); the     return 0; the}


P3003 [Usaco10dec] apple delivery apples 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.