Silver Cow Party
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 14384 |
|
Accepted: 6490 |
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 OfM (1≤m≤100,000) unidirectional (one-way roads connects pairs of farms; Roadi Requiresti (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, andx
Lines 2..m+1:line i+1 describes road I with three space-separated Integers:ai,bi, andti. The described road runs from Farmai to Farmbi and Requiringti time units to traverse.
Output line 1:one integer:the maximum of 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
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
Title Link: http://poj.org/problem?id=3268
The main problem: cows gathering, to ask all cows from their farms to the target farm to return to their own farm the shortest possible maximum, the road is one-way
Title analysis: Similar to POJ 1511, from their own to the target farm to N-1 times SPFA, record each dis[x], and then once SPFA (x), record each dis[i], the last two values add up to take large, complexity O (nm) feel not, but 300ms+ over
Code:
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue&
Gt
using namespace Std;
int const INF = 0X3FFFFFFF;
int const MAX = 1005;
int Dis[max], Re1[max], Re2[max];
BOOL Vis[max];
int n, m, X; struct EGDE {int u, V, t;}
E[max * MAX/2];
struct NODE {int V, t;
NODE (int vv, int tt) {v = vv;
t = TT;
}
};
Vector <NODE> Vt[max];
void SPFA (int v0) {for (int i = 1; I <= n; i++) dis[i] = INF;
Dis[v0] = 0;
Queue <int> q;
Q.push (V0);
Memset (Vis, false, sizeof (VIS));
while (!q.empty ()) {int u = q.front ();
Q.pop ();
Vis[u] = false;
int sz = Vt[u].size ();
for (int i = 0; i < sz; i++) {int v = VT[U][I].V;
int t = vt[u][i].t;
if (Dis[v] > Dis[u] + t) {dis[v] = Dis[u] + t;
if (!vis[v]) { VIS[V] = true;
Q.push (v);
}}}} return;
} int main () {for (int i = 0; I <= N; i++) vt[i].clear ();
scanf ("%d%d%d", &n, &m, &x);
for (int i = 0; i < m; i++) scanf ("%d%d%d", &e[i].u, &E[I].V, &e[i].t);
for (int i = 0; i < m; i++) Vt[e[i].u].push_back (NODE (E[I].V, e[i].t));
for (int i = 1; I <= n; i++) {if (i = = x) {continue;
Re1[i] = 0;
} SPFA (i);
Re1[i] = dis[x];
} SPFA (x);
for (int i = 1; I <= n; i++) re2[i] = dis[i];
int ans = 0;
for (int i = 1; I <= n; i++) ans = max (ans, re1[i] + re2[i]);
printf ("%d\n", ans);
}