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 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

USACO 2007 February Silver

题解:

1.可知最短路分为两段, 各个点到X的最短距离,以及X到各个点的最短距离。

2.先求出X到各个点的最短距离, 记为dis1。

3.将各条边的方向取反, 然后再求出X到各个点的最短距离,记为dis2。因为边取反了,所以dis2实际是各个点到X的最短距离。

4.取dis1+dis2的最大值,即为答案。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
13 #define ms(a,b) memset((a),(b),sizeof((a)))
14 using namespace std;
15 typedef long long LL;
16 const double EPS = 1e-8;
17 const int INF = 2e9;
18 const LL LNF = 9e18;
19 const int MOD = 1e9+7;
20 const int MAXN = 1e3+10;
21
22 int n, m, X;
23 int g[MAXN][MAXN];
24
25 int dis1[MAXN], dis2[MAXN];
26 bool vis[MAXN];
27 void dijkstra(int st, int dis[])
28 {
29     memset(vis, 0, sizeof(vis));
30     for(int i = 1; i<=n; i++)
31         dis[i] = (i==st?0:INF);
32
33     for(int i = 1; i<=n; i++)
34     {
35         int k, minn = INF;
36         for(int j = 1; j<=n; j++)
37             if(!vis[j] && dis[j]<minn)
38                 minn = dis[k=j];
39
40         vis[k] = 1;
41         for(int j = 1; j<=n; j++)
42             if(!vis[j] && g[k][j]!=INF)
43                 dis[j] = min(dis[j], dis[k]+g[k][j]);
44     }
45 }
46
47 int main()
48 {
49     while(scanf("%d%d%d", &n, &m, &X)!=EOF)
50     {
51         for(int i = 1; i<=n; i++)
52             for(int j = 1; j<=n; j++)
53                 g[i][j] = INF;
54         for(int i = 1; i<=m; i++)
55         {
56             int u, v, w;
57             scanf("%d%d%d", &u, &v, &w);
58             g[u][v] = w;
59         }
60         dijkstra(X, dis1);  //第一次跑最短路,计算X到各点的最短距离
61
62         for(int i = 1; i<=n; i++)   //将边取反
63             for(int j = i+1; j<=n; j++)
64                 swap(g[i][j], g[j][i]);
65         dijkstra(X, dis2);  //第二次跑最短路,计算X到各点的距离,但因为边取反了,所以实际上是各点到X的最短距离。
66
67         int ans = 0;
68         for(int i = 1; i<=n; i++)   //取两段距离之和的最大值
69             ans = max(ans, dis1[i]+dis2[i]);
70         printf("%d\n", ans);
71     }
72 }

时间: 2024-08-13 21:43:48

POJ3268 Silver Cow Party —— 最短路的相关文章

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

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party 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

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 

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 ≤ X

poj3268——Silver Cow Party(最短路+godv之力)

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 re

【POJ3268】Silver Cow Party 最短路

题意:一堆奶牛去某个地方,去了又回,然后求去回和的最大值. 题解:两遍最短路,结束,邻接矩阵存边可以避免建反图. #include <cstdio> #include <cstring> #include <algorithm> #define N 1005 #define inf 0x3f3f3f3f using namespace std; int map[N][N],n,m,s; int dist1[N],dist2[N]; bool visit[N]; void

poj3268 Silver Cow Party(最短路)

非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了. 题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> using namespace std; const int

poj3268 Silver Cow Party (SPFA求最短路)

其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #

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