poj Silver Cow Party 3268 (来回单向最短路)好题
2016-03-27 21:13
411 查看
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 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
Sample Output
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头牛要去第x头牛的住处参加party。。参加完还要返回。。
先输入n,m,x;分别表示有n头牛,m条路单向,单向的,单向的路(重要的说三遍),x表示要去的终点。因为牛都比较懒,所以他们都会采取最短路的方式去和返回,现在问这n头牛中要花费最长时间的牛的时间。
//思路:
来回单向最短路,因为n的范围是(n<1000),所以用笛杰斯特拉就行了。平常我们写的都是去的,这块就要新学到点东西了,这里直接求得来回的最短路(很机智)。
具体看代码:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16996 | Accepted: 7756 |
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头牛要去第x头牛的住处参加party。。参加完还要返回。。
先输入n,m,x;分别表示有n头牛,m条路单向,单向的,单向的路(重要的说三遍),x表示要去的终点。因为牛都比较懒,所以他们都会采取最短路的方式去和返回,现在问这n头牛中要花费最长时间的牛的时间。
//思路:
来回单向最短路,因为n的范围是(n<1000),所以用笛杰斯特拉就行了。平常我们写的都是去的,这块就要新学到点东西了,这里直接求得来回的最短路(很机智)。
具体看代码:
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #define INF 0x3f3f3f3f #define ull unsigned long long #define ll long long #define IN__int64 #define N 1010 #define M 1000000007 using namespace std; int map ; int a ; int vis ,dis ;//回去的最短距离 int vis1 ,dis1 ;//去的时候的最短距离 int n,m; int djs(int y) { memset(vis,0,sizeof(vis)); memset(dis,INF,sizeof(dis)); memset(vis1,0,sizeof(vis1)); memset(dis1,INF,sizeof(dis1)); dis[y]=0;dis1[y]=0; int i,j,k,k1; for(j=1;j<=n;j++) { k=-1;k1=-1; for(i=1;i<=n;i++) { if(!vis[i]&&(k==-1||dis[i]<dis[k])) k=i; if(!vis1[i]&&(k1==-1||dis1[i]<dis1[k1])) k1=i; } vis[k]=1;vis1[k1]=1; for(i=1;i<=n;i++) { dis[i]=min(dis[i],dis[k]+map[k][i]);//回去 dis1[i]=min(dis1[i],dis1[k1]+map[i][k1]);//来 } } } int main() { int i,j,k,x; while(scanf("%d%d%d",&n,&m,&x)!=EOF) { int u,v,w; memset(map,INF,sizeof(map)); memset(a,0,sizeof(a)); for(i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); if(map[u][v]>w) map[u][v]=w; } djs(x); int mm=0; for(i=1;i<=n;i++) { if(i==x) continue; mm=max(mm,dis[i]+dis1[i]); } printf("%d\n",mm); } return 0; }
相关文章推荐
- jqueryEasyUi 后台页面结构设计
- SVN服务器搭建和使用(三)
- Scala对象
- Java语言特点
- linux内核分析第五周学习笔记
- 求总和
- poj--3268--Silver Cow Party(最短路)(建反向图)
- 2016-3-27 Python学习
- java虚拟机 jvm java堆 方法区 java栈
- 聊聊android界面适配
- MVP模式在Android开发中的最佳实践
- STM32F407学习记录2:SysTick嘀嗒定时器学习
- SVN服务器搭建和使用(二)
- java虚拟机 jvm java堆 方法区 java栈
- Intent隐式启动
- 迭代器类型
- 介绍new Thread的弊端及Java四种线程池的使用
- 卷积神经网络Convolutional Neural Networks
- 20145327 《Java程序设计》第四周学习总结
- java中String类、StringBuffer类、StringBuilder类的区别(未完待续)