[有向图Dijkstra模板]Silver Cow Party
来源:互联网 发布:老铁稳是什么意思网络 编辑:程序博客网 时间:2024/06/07 16:09
/*
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status Practice 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 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.
题目大意:给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出
*/
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status Practice 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 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.
题目大意:给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出
*/
# include <iostream># include <cstdio># include <cstring>using namespace std;const int NN = 1005;const int INF = 65535;int N, M, X;int map[NN][NN];int vis[NN];int dis1[NN];int dis2[NN];void disj(){ int min, k; memset(vis, 0, sizeof(vis)); vis[X] = 1; for (int i=1; i<=N; i++) { dis1[i] = map[X][i]; dis2[i] = map[i][X]; } for (int i=1; i<=N; i++) { min = INF; for (int j=1; j<=N; j++) { if (!vis[j] && dis1[j]<min) { min = dis1[k = j]; } } vis[k] = 1; for (int j=1; j<=N; j++) { if (!vis[j] && dis1[j]>dis1[k]+map[k][j]) { dis1[j] = dis1[k]+map[k][j]; } } } memset(vis, 0, sizeof(vis)); vis[X] = 1; for (int i=1; i<=N; i++) { min = INF; for (int j=1; j<=N; j++) { if (!vis[j] && dis2[j]<min) { min = dis2[k = j]; } } vis[k] = 1; for (int j=1; j<=N; j++) { if (!vis[j] && dis2[j]>dis2[k]+map[j][k])//与上边不一样 { dis2[j] = dis2[k]+map[j][k]; } } } min = -1; for (int i=1; i<=N; i++) { if (dis1[i]+dis2[i] > min) { min = dis1[i] + dis2[i]; } } printf("%d\n", min);}int main(){ scanf("%d %d %d", &N, &M, &X); memset(map, INF, sizeof(map)); 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=1; i<=M; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); map[a][b] = c; } disj(); return 0;}
0 0
- [有向图Dijkstra模板]Silver Cow Party
- POJ-3268 Silver Cow Party (Dijkstra求有向图最短路)
- Silver Cow Party Dijkstra
- poj 3268 Silver Cow Party 【最短路,有向图】
- POJ3268 Silver Cow Party 【Dijkstra】
- POJ3268 Silver Cow Party(dijkstra)
- poj3268 Silver Cow Party (dijkstra)
- Silver Cow Party (Dijkstra)
- Silver Cow Party (dijkstra)
- poj-3268-Silver Cow Party dijkstra模板题
- POJ 3268 Silver Cow Party (双图往返dijkstra)
- Poj 3268 Silver Cow Party【dijkstra+逆向建图】
- POJ 3268 Silver Cow Party(Dijkstra算法)
- POJ 3268 - Silver Cow Party(dijkstra)
- POJ 3268 Silver Cow Party (Dijkstra~)
- (Dijkstra+bellon_ford+floyd) Silver Cow Party (P3268)
- POJ 3268 Silver Cow Party(dijkstra)
- poj 3268 Silver Cow Party , spfa , dijkstra
- android点击邮箱链接跳转发送
- jar包重复:com.android.build.api.transform.transformexception
- oracle用户密码过期与进程数超限
- C# EPPlus和NPOI组件导出Excel学习总结
- Activity的launchMode:singleTop,singleTask与singleInstance
- [有向图Dijkstra模板]Silver Cow Party
- URL中特殊字符处理
- DDR原理详解
- 博为峰Java面试题-异常处理之Throwable类
- 冒泡排序
- shc对shell脚本加密
- JSOUP实践:解析和遍历HTML文档
- css3 Transition属性(平滑过渡菜单栏案例)
- 9. Palindrome Number