Poj 3268 silver cow Party (Shortest Path)

Source: Internet
Author: User
Silver cow party
Time limit:2000 ms   Memory limit:65536 K
Total submissions:10147   Accepted:4497

Description

One cow from eachNFarms (1 ≤N≤ 1000) conveniently numbered 1 ..NIs going to attend the big cow party to be held at farm #X(1 ≤XN). A totalM(1 ≤MLess than or equal to 100,000) unidirectional (one-way roads connects pairs of farms; roadIRequiresTi(1 ≤Ti≤ 100) units of time to traverse.

Each cow must walk to the Party and, when the party is over, return to her farm. each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the Party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input

Line 1: three space-separated integers, respectively: N , M , And X
Lines 2 .. M + 1: Line I + 1 describes Road I With three space-separated integers: AI , Bi , And Ti . The described road runs from farm AI To farm Bi , Requiring Ti Time units to traverse.

Output

Line 1: One INTEGER: the maximum of time any one cow must walk.

Sample Input

 
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3

Sample output

 
10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

Source

Usaco 2007 February silver exercise template. Use the source image and the reverse image to use the single-source shortest path. Dijkstra Algorithm
//  ========================================================== ==========================================  //  Name: poj. cpp  //  Author:  //  Version:  //  Copyright: Your copyright notice  //  Description: Hello world in C ++, ANSI-style  // ========================================================== ==========================================  # Include <Iostream> # Include <Stdio. h> # Include < String . H> # Include <Algorithm> Using   Namespace  STD;  Const   Int Maxn = 1010  ; Const   Int INF = 0x3f3f3f  ;  Bool  Vis [maxn];  Void Dijkstra ( Int Cost [] [maxn], Int Lowcost [], Int N, Int  Beg ){  For ( Int I = 1 ; I <= N; I ++ ) {Lowcost [I] = INF; vis [I] = False  ;} Lowcost [beg] = 0  ;  For ( Int J = 0 ; J <n; j ++ ){  Int K =- 1  ;  Int Min =INF;  For ( Int I = 1 ; I <= N; I ++ )  If (! Vis [I] & lowcost [I] < Min) {min = Lowcost [I]; k = I ;}  If (K =- 1 ) Break  ; Vis [k] =True  ;  For ( Int I = 1 ; I <= N; I ++ )  If (! Vis [I] & lowcost [k] + cost [k] [I] < Lowcost [I]) lowcost [I] = Lowcost [k] + Cost [k] [I] ;}}  Int  Dist1 [maxn];  Int  Dist2 [maxn];  Int Cost [maxn] [maxn];  Int  Main (){  //  Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      Int  N, m, X;  Int  U, V, W;  While (Scanf ( "  % D  " , & N, & M, & X) =3  ){  For ( Int I = 1 ; I <= N; I ++ )  For ( Int J = 1 ; J <= N; j ++ ){  If (I = J) cost [I] [J] = 0  ;  Else Cost [I] [J] =INF ;}  While (M -- ) {Scanf (  "  % D  " , & U, & V ,& W); cost [u] [v] = Min (cost [u] [v], W);} Dijkstra (cost, dist1, n, x );  For ( Int I = 1 ; I <= N; I ++ )  For (Int J = 1 ; J <I; j ++ ) Swap (cost [I] [J], cost [J] [I]); Dijkstra (cost, dist2, n, x );  Int Ans = 0  ;  For ( Int I = 1 ; I <= N; I ++ ) Ans = Max (ANS, dist1 [I] + Dist2 [I]); printf (  "  % D \ n "  , ANS );}  Return   0  ;} 

 

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.