Silver Cow Party
| Time Limit:2000 MS |
|
Memory Limit:65536 K |
| Total Submissions:13100 |
|
Accepted:5881 |
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 ≤X≤N). 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
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 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. given n points, each point has a cow. Then, click goal to open a party. The Ox in each vertex must arrive at goal, and then return the original vertex from goal. Which cow has the largest cost? First, you need to know the shortest path from each point to one point x. In fact, it is to construct an inverse graph and run dij (or spfa) at the source point x. This is to run dij once in a forward direction and dij in a reverse direction. Take the two shortest distance and max of each vertex as an undirected graph at first. After a long tangle, I found that it was a forward question ~
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <vector> # include <queue> using namespace std; typedef long LL; const int N = 1010; const int M = 200010; const int inf = 1e9 + 7; int n, m, goal; struct node {int v, w; node () {} node (int a, int B) {v = a, w = B;} bool operator <(const node & a) const {return w>. w ;}}; vector <node> g [2] [N ]; Int dis [2] [N]; bool vis [N]; void dij (int which) {priority_queue <node> que; que. push (node (goal, 0); while (! Que. empty () {int u = que. top (). v, cost = que. top (). w; que. pop (); if (dis [which] [u] <= cost) continue; dis [which] [u] = cost; for (int I = 0; I <g [which] [u]. size (); ++ I) {int v = g [which] [u] [I]. v, w = g [which] [u] [I]. w; if (dis [which] [u] + w <dis [which] [v]) {que. push (node (v, dis [which] [u] + w) ;}}} void init () {for (int I = 0; I <2; ++ I) {for (int j = 1; j <= n; ++ j ){ G [I] [j]. clear (); dis [I] [j] = inf ;}} void run () {int u, v, w; init (); while (m --) {scanf ("% d", & u, & v, & w); g [0] [u]. push_back (node (v, w); g [1] [v]. push_back (node (u, w);} dij (0), dij (1); int ans = 0; for (int I = 1; I <= n; ++ I) ans = max (ans, dis [0] [I] + dis [1] [I]); printf ("% d \ n", ans );} int main () {# ifdef LOCAL freopen ("in.txt", "r", stdin); # endif // LOCAL int _, cas = 1; while (s Canf ("% d", & n, & m, & goal )! = EOF) run ();}View Code
POJ 3268 Silver Cow Party