POJ 3268 Silver Cow Party (Shortest way)

Source: Internet
Author: User

POJ 3268 Silver Cow Party

Description
One cow from each of N farms (1≤n≤1000) conveniently numbered 1..N are going to attend the big Cow party to being held at Farm #X (1≤x≤n). A total of M (1≤m≤100,000) unidirectional (one-way roads connects pairs of farms; Road I requires Ti (1≤ti≤100) UN Its 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 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 the time any one cow must walk.
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10

The main topic: There are N farms, M Road, and on the X Farm Open party. Now in addition to the other farms on the X farm, there are cattle to the X farm open Party,party after the end of each home, of course, the shortest way to go. This n-1 the largest short-circuit of the return in a cow. Problem-solving ideas: first to ask for the X as the starting point of the shortest, and then the side of the reverse read again, and then to the X as the starting point of the shortest. Add up to two d arrays and find the maximum value, which is the answer.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;typedef Long LongllConst intINF =0x3f3f3f3f;Const intN =1005;Const intM =500005;intN, M, s;structEdge {intFrom, to, Dist; }; vector<Edge>edges, edgess; vector<int>G[M], g2[m];intVis[n], D[n], rec[n];intL[n];voidInit () { for(inti =0; I <= N; i++) {L[i] =0; Vis[i] =0; } for(inti =0; I < (n/2) * N; i++) g[i].clear (); for(inti =0; I < (n/2) * N;      i++) g2[i].clear ();      Edges.clear (); Edgess.clear ();}voidAddedge (intFromintTo,intDist) {Edges.push_back (Edge) {from, To, Dist}); Edgess.push_back (Edge) {To, from, Dist});intpos = Edges.size (); G2[to].push_back (POS-1); G[from].push_back (POS-1);}voidInput () {intU, V, Len; for(inti =0; I < m; i++) {scanf(" %d%d%d", &u, &v, &len);     Addedge (U, V, Len); }}voidSPFA ( vector<int>*gg, vector<Edge>E) {memset(Vis,0,sizeof(VIS)); for(inti =0; i < N; i++) D[i] = INF; for(inti =0; i < N;    i++) Rec[i] = INF; D[s] =0; Rec[s] =0; Vis[s] =1; Queue<int>Q; Q.push (s); while(! Q.empty ()) {intU = Q.front ();          Q.pop (); Vis[u] =0; for(inti =0; I < gg[u].size (); i++) {intv = e[gg[u][i]].to;if(D[v] > D[u] + e[gg[u][i]].dist)                  {D[v] = D[u] + e[gg[u][i]].dist; REC[V] = Rec[u] +1;if(!vis[v]) {Vis[v] =1;                  Q.push (v); }              }Else if(D[v] = = D[u] + e[gg[u][i]].dist) {if(Rec[v] > Rec[u] +1) {Rec[v] = Rec[u] +1;if(!vis[v]) {Vis[v] =1;                      Q.push (v); }                  }              }          }      }  }intMain () {intx, Max; while(scanf(" %d%d%d", &n, &m, &x) = =3) {Max =0;             Init ();        Input ();        s = x; SPFA (G, edges); for(inti =1; I <= N; i++) {if(i = = x)Continue;        L[i] + = D[i]; } SPFA (G2, edgess); for(inti =1; I <= N; i++) {if(i = = x)Continue;        L[i] + = D[i]; } for(inti =1; I <= N;        i++) {max = max (l[i], max); }printf("%d\n", Max); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3268 Silver Cow Party (Shortest way)

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.