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

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

USACO 2007 February Silver

 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 }
时间: 2024-10-09 07:01:08

POJ3268 Silver Cow Party(dijkstra+矩阵转置)的相关文章

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

POJ 3268 Silver Cow Party(dijkstra+矩阵转置)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15460   Accepted: 7004 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 

POJ3268 Silver Cow Party 【Dijkstra】

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13183   Accepted: 5932 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 

poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14900   Accepted: 6746 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 

POJ3268 Silver Cow Party —— 最短路

题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24527   Accepted: 11164 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co

POJ-3268 Silver Cow Party---正向+反向Dijkstra

题目链接: https://vjudge.net/problem/POJ-3268 题目大意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号X,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求所有牛从开始到回家的时间是多少? 思路: 所有牛都回到了家所花费的时间就是这些牛中花费时间的最大者,可以正向的Dijkstra求出从X到每个点的最短时间,然后一个骚操作:将所有边反向(swap(Map[i][j], Map[j][i])),再从x用dijkstra求出X到每个点的最

POJ 3268 Silver Cow Party (Dijkstra)

题目链接:POJ 3268 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

POJ3268 Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18713   Accepted: 8561 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 

POJ-3268 Silver Cow Party( 最短路 )

题目链接:http://poj.org/problem?id=3268 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 road