【POJ】 3268 Silver Cow Party
2016-01-08 15:47
459 查看
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.
题解:这一题就是做两次单元最短路径,不同的就是把所有边反向就好。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16290 | Accepted: 7460 |
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.
题解:这一题就是做两次单元最短路径,不同的就是把所有边反向就好。
#include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; struct Edge { int from,to,d,next; }; struct Edge e[100005]; int n,l,m,x,ans[1005],d[1005],vis[1005],pos[1005]; queue<int> q; int spfa(int s) { for (int i=1;i<=n;i++) d[i]=10000000; d[s]=0; int x; q.push(s); vis[s]=1; while (!q.empty()) { x=q.front(); q.pop(); vis[x]=0; for (int i=pos[x];i!=0;i=e[i].next) if (d[e[i].to]>d[e[i].from]+e[i].d) { d[e[i].to]=d[e[i].from]+e[i].d; if (!vis[e[i].to]) { vis[e[i].to]=1; q.push(e[i].to); } } } return 1; } void reset() { memset(pos,0,sizeof(pos)); for (int i=1;i<=m;i++) { int x; x=e[i].from; e[i].from=e[i].to; e[i].to=x; e[i].next=pos[e[i].from]; pos[e[i].from]=i; } } int main() { scanf("%d%d%d",&n,&m,&x); for (int i=1;i<=m;i++) { scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].d); e[i].next=pos[e[i].from]; pos[e[i].from]=i; } spfa(x); for (int i=1;i<=n;i++) ans[i]+=d[i]; reset(); spfa(x); for (int i=1;i<=n;i++) ans[i]+=d[i]; for (int i=1;i<=n;i++) l=max(ans[i],l); printf("%d",l); return 0; }
相关文章推荐
- 如何实现android手机摄像头的的自动对焦
- PROC基础知识之指示变量变量
- 小知识点积累文档
- 一条进程的栈区、堆区、数据区和代码区在内存中的映射
- python 列表(Lists)
- 大数据教父Michael Stonebraker告诉你大数据的秘密
- 访问量到1万之后再写
- Android架构设计演变史
- Android SDK在线更新镜像服务器
- Select2下拉框总结
- Databricks Scala 编程风格指南
- 隐藏文件的链接地址
- Dynamic和Var的区别及dynamic使用详解
- 分析curl错误原因的函数curl_error
- Navicat Premium数据库管理工具
- QT creator 调试问题
- jQuery Ajax通用js封装
- 网络配置必要代码段
- 2015年十大热门Android开源新项目
- 归并排序 分治法