Fellow me on GitHub

POJ3268(KB4-D spfa)

Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23426   Accepted: 10691

Description

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be 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) 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: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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 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 10 time units.

Source

 
 1 //2017-08-08
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 
 9 using namespace std;
10 
11 const int N = 1010;
12 const int INF = 0x3f3f3f3f;
13 struct Edge{
14     int v, w;
15     Edge(int _v = 0, int _w = 0):v(_v), w(_w){}
16 };
17 vector<Edge> E[2][N];
18 bool vis[N];
19 int dis[2][N], cnt[N], n, m, x;
20 
21 bool spfa(int s, int n, int time){
22     memset(vis, false, sizeof(vis));
23     memset(dis[time], INF, sizeof(dis));
24     memset(cnt, 0, sizeof(cnt));
25     vis[s] = true;
26     dis[time][s] = 0;
27     cnt[s] = 1;
28     queue<int> q;
29     q.push(s);
30     while(!q.empty()){
31         int u = q.front();
32         q.pop();
33         vis[u] = false;
34         for(int i = 0; i < E[time][u].size(); i++){
35             int v = E[time][u][i].v;
36             int w = E[time][u][i].w;
37             if(dis[time][v] > dis[time][u] + w){
38                 dis[time][v] = dis[time][u] + w;
39                 if(!vis[v]){
40                     vis[v] = true;
41                     q.push(v);
42                     if(++cnt[v] > n)return false;
43                 }
44             }
45         }
46     }
47     return true;
48 }
49 
50 int main()
51 {
52     while(scanf("%d%d%d", &n, &m, &x)!=EOF){
53         int a, b, c;
54         while(m--){
55             scanf("%d%d%d", &a, &b, &c);
56             E[0][a].push_back(Edge(b, c));
57             E[1][b].push_back(Edge(a, c));
58         }
59         spfa(x, n, 0);
60         spfa(x, n, 1);
61         int ans = 0;
62         for(int i = 1; i <= n; i++)
63             ans = max(ans, dis[0][i]+dis[1][i]);
64         printf("%d\n", ans);
65     }
66 
67     return 0;
68 }

 

posted @ 2017-08-08 09:09  Penn000  阅读(223)  评论(0编辑  收藏  举报