码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3268 Silver Cow Party(dijkstra+矩阵转置)

时间:2015-08-04 12:39:54      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15156   Accepted: 6843

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 ≤ XN). 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: 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 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 #include <cstdio>
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <ctime>
 6 #include <cmath>
 7 #include <string>
 8 #include <cstring>
 9 #include <stack>
10 #include <queue>
11 #include <list>
12 #include <vector>
13 #include <map>
14 #include <set>
15 using namespace std;
16 
17 const int INF=0x3f3f3f3f;
18 const double eps=1e-10;
19 const double PI=acos(-1.0);
20 const int maxn=1000+10;
21 
22 int w[maxn][maxn];
23 int v[maxn], d[maxn], c[maxn];
24 int n, m, x;
25 
26 void dijkstra(int k)
27 {
28     memset(v, 0, sizeof(v));
29     for(int i = 1; i <= n; i++) d[i] = (i==k ? 0 : INF);
30     for(int i = 1; i <= n; i++){
31         int x1, m = INF;
32         for(int y = 1; y <= n; y++) if(!v[y] && d[y]<=m) m = d[x1=y];
33         v[x1] = 1;
34         for(int y = 1; y <= n; y++) d[y] = min(d[y], d[x1]+w[x1][y]);
35     }
36 }
37 void  tran() {
38     for(int i = 1; i <= n; i++) {
39         for(int j = 1; j <= i; j++)
40             swap(w[i][j], w[j][i]);
41     }
42 }
43 int main()
44 {
45     while(~scanf("%d%d%d", &n, &m, &x)) {
46         int a, b, t;
47         memset(w, INF, sizeof(w));
48         for(int i = 0; i < m; i++) {
49             scanf("%d%d%d", &a, &b, &t);
50             w[a][b] = min(w[a][b], t);
51         }
52         memset(c, 0, sizeof(c));
53         dijkstra(x);
54         for(int i = 1; i <= n; i++) c[i] = d[i];
55         tran();
56         dijkstra(x);
57         int ans = -1;
58         for(int i = 1; i <= n; i++) {
59             c[i] += d[i];
60             ans = max(ans,c[i]);
61         }
62        printf("%d\n", ans);
63     }
64 }

 

POJ3268 Silver Cow Party(dijkstra+矩阵转置)

标签:

原文地址:http://www.cnblogs.com/ZP-Better/p/4701288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!