POJ 3268 Silver Cow Party
2015-08-13 21:45
387 查看
链接:http://poj.org/problem?id=3268
Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15081 Accepted: 6808
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?
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.
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
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代码:
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 XLines 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 21 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
10Hint
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有一条路,需花费时间ti while (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; else map[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]; } } }
相关文章推荐
- 对现有Hive的大表进行动态分区
- hive中的静态分区与动态分区
- 静态与非静态的区别
- iOS开发 -- UILable详解
- 判断一个java文件和邮箱格式是否合法
- 静态与非静态的区别
- 如何在Linux操作系统之上的MySQL数据库操作环境中使用清屏命令?
- 调优 PHP
- [笔记][Java7并发编程实战手册]第三章-线程同步辅助类-3.1概要
- OC第三天(内存管理)
- Go 并发
- strcmp,strlen,strcmp,strcat实现
- POJ 3026 Borg Maze BFS和prim
- 调优 PHP
- C++字符串string类提供的处理字符串的函数
- 黑马程序员——泛型
- const(一):(转) 一个函数名后面加const表示什么意思
- gcc简单命令记录
- CodeForces-554B Ohana Cleans Up
- JS学习十三天----子集和扩展