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≤X ≤ N). A total of m (1≤ m ≤100,000) unidirectional (one-way roads connects pairs of farms; RoadI req Uires 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.
The other is the forward mapping and the reverse building. is to ask for the largest one round trip away.
Djistra:
#include <iostream> #include <cstring> #include <cstdio> #include <queue>using namespace std; const int inf= 0x3fffffff;int map[2][1003][1003];bool visited[1003];int d[1003];int ans[1003];int max (int a,int b) { Return a > B? A:B;} void Prim (int n,int s,int flag) {memset (visited,false,sizeof (visited)); for (int i = 1;i <= n;i++) {d[i] = Map[flag][s][i] ;//cout<<d[i]<<endl;} Visited[s] = true;for (int i = 1;i < n;i++) {int min = inf;int k;for (int j = 1;j <= n;j++) {if (!visited[j] && m In > D[j]) {min = D[j];k = j;}} if (min = = INF) {break;} Visited[k] = true;for (int j = 1;j <= n;j++) {if (!visited[j] && d[j] > D[k] + map[flag][k][j]) {d[j] = D[k] + M AP[FLAG][K][J];}}} int main () {int n,m,st;while (scanf ("%d%d%d", &n,&m,&st)! = EOF) {for (int i = 1;i <= n;i++) {for (int j = 1;j < = n;j++) {if (i = = j) {Map[0][i][j] = 0;map[1][i][j] = 0;} ELSE{MAP[0][I][J] = inf;map[1][i][j] = INF;}}} int u,v,c;for (int i = 0;i < m;i++) {scanf ("%d%d%D ", &u,&v,&c); Map[0][u][v] = c;map[1][v][u] = c;} Prim (n,st,1); for (int i = 1;i <= n;i++) {ans[i] = d[i];} Prim (n,st,0); int res = 0;for (int i = 1;i <= n;i++) {ans[i] + = D[i];res = Max (Res,ans[i]);} printf ("%d\n", res);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Silver Cow Party