POJ 3268 Silver Cow Party

来源:互联网 发布:js 产生16位随机数 编辑:程序博客网 时间:2024/05/22 12:06
链接:http://poj.org/problem?id=3268

Silver Cow Party

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15081 Accepted: 6808

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

大意——n个牧场的牛要去其中一个牧场参加聚会。现在给你要去的那个牧场,以及m条各牧场之间的单向通路及所需要花费的时间。问:每个牛以最短的时间来回,求出所有牛中花费时间最大的那一个牛的时间值。

思路——此问题可以转化为一个有向图,具体思路是:从一个点出发,到达目标点花费最小时间,显然可以用dijkstra算法来解决问题。最终只需把从一个点出发到达目标点来回所花费时间最大的那一个求出即为答案。

复杂度分析——时间复杂度:O(n^2),空间复杂度:O(n^2)

具体算法参见:http://baike.baidu.com/link?url=4VjBWwHeIDNXhTIml6nSuVhiM_oV0Td3dmOATzC2DUqCsJk7U7LfiYKY9PQcT5iePrHTUzI3ljyGufuUHH1qRq

附上AC代码:


#include <iostream>#include <cstdio>#include <string>#include <cmath>#include <iomanip>#include <ctime>#include <climits>#include <cstdlib>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;typedef unsigned int UI;typedef long long LL;typedef unsigned long long ULL;typedef long double LD;const double pi = acos(-1.0);const double e = exp(1.0);const int maxn = 1005;const int inf = 0x3f3f3f3f;int n, m, obj; // 分别表示牧场数,通路数,举行聚会的牧场int map[maxn][maxn]; // 有向赋权图int dist[maxn]; // 去时的距离int dist_back[maxn]; // 回时的距离bool visited[maxn]; // 标记是否来过void dijkstra(int * r_dist, bool flag);int main(){ios::sync_with_stdio(false);int ai, bi, ti; // 表示从ai到bi有一条路,需花费时间tiwhile (scanf("%d%d%d", &n, &m, &obj) != EOF){for (int i=1; i<=n; i++)for (int j=1; j<=n; j++){if (i != j)map[i][j] = inf;elsemap[i][j] = 0;}for (int i=0; i<m; i++){scanf("%d%d%d", &ai, &bi, &ti);map[ai][bi] = ti;}for (int i=1; i<=n; i++){dist[i] = map[obj][i];dist_back[i] = map[i][obj]; // 行列调换}dijkstra(dist, 0);dijkstra(dist_back, 1);int maxpath = -inf;for (int i=1; i<=n; i++)if (maxpath < dist[i]+dist_back[i])maxpath = dist[i]+dist_back[i];printf("%d\n", maxpath);}return 0;}void dijkstra(int * r_dist, bool flag){memset(visited, 0, sizeof(visited));for (int i=1; i<=n; i++){int index = 0, min_dist = inf;for (int j=1; j<=n; j++)if (!visited[j] && r_dist[j]<min_dist){min_dist = r_dist[j];index = j;}visited[index] = 1;for (int j=1; j<=n; j++){if (!visited[j] && !flag && r_dist[j]>r_dist[index]+map[index][j])r_dist[j] = r_dist[index]+map[index][j];else if (!visited[j] && flag && r_dist[j]>r_dist[index]+map[j][index])r_dist[j] = r_dist[index]+map[j][index];}}}


1 0
原创粉丝点击