POJ 3268 Silver Cow Party shortest circuit

Source: Internet
Author: User

Original title Link: http://poj.org/problem?id=3268

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15545 Accepted: 7053

Description

One cow from each of N Farms (1≤ n ≤1000) conveniently numbered 1.. N is going to attend the big Cow party to being held at Farm #x (1≤ XN). A total of m (1≤ m ≤100,000) unidirectional (one-way roads connects pairs of farms; Road I re Quires ti (1≤ ti ≤100) units of time to traverse.

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

Of all the cows, what's 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 IWith three space-separated integers: Ai, Bi, and Ti. The described road runs from farm AiTo farm Bi, requiring TiTime units to traverse.

Output

Line 1:one integer:the maximum of the 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 ten time units.

Source

Usaco February Silver Test instructions

There's a bull party, the other cows go, the cows take the shortest way, and the party ends up back at home. Ask which cow to walk the longest path, output the longest path.

Exercises

Running on both sides of the SPFA, is running two times against the opposite. And then it's done.

Code
#include <iostream>#include<queue>#include<cstring>#include<algorithm>#include<vector>#include<cstdio>#defineINF 1000006#defineMax_n 1003using namespacestd;structNode { Public:    intu, c; Node (intUuintcc): U (UU), C (cc) {} node () {}};structEdge { Public:    intto, cost; Edge (intTintc): to (t), cost (c) {} edge () {}};queue<node>que;intn,m,x;voidSPFA (intS,vector<edge> g[],intd[]) {Fill (d, D+ N +1, INF); Que.push (node (s),0)); D[s]=0;  while(Que.size ()) {node now=Que.front ();        Que.pop (); if(now.c! = d[now.u])Continue; intU =now.u;  for(inti =0; I < g[u].size (); i++) {            intv =g[u][i].to; intt = D[u] +G[u][i].cost; if(T <D[v]) {D[v]=T;            Que.push (Node (v, t)); }}}}vector<edge>G[max_n],rg[max_n];intD[max_n],rd[max_n];intMain () {scanf ("%d%d%d", &n, &m, &x);  for(inti =0; I < m; i++) {        intu, V, c; scanf ("%d%d%d", &u, &v, &c);        G[u].push_back (Edge (V, c));    Rg[v].push_back (Edge (U, c));    } SPFA (x, G, D);  while(Que.size ()) Que.pop ();    SPFA (x, RG, RD); intAns =0;  for(inti =1; I <= N; i++) ans = max (ans, d[i] +Rd[i]); cout<<ans<<Endl; return 0;}

POJ 3268 Silver Cow Party shortest circuit

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.