【POJ】 3268 Silver Cow Party
来源:互联网 发布:采集电脑数据 编辑:程序博客网 时间:2024/05/21 21:43
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
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
Sample Input
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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;}
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ-3268-Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- POJ-3268-Silver Cow Party
- poj 3268--Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ-3268 Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- 使用curl进行s3服务操作
- 判断素数原理
- Ajax发送请求的编码问题
- C语言中堆和栈的区别
- namespace关键字和__NAMESPACE__常量
- 【POJ】 3268 Silver Cow Party
- iOS 改变storyboard默认ViewController大小
- JAVA类
- 分析curl错误原因的函数curl_error
- 小知识点积累文档
- 性能测试之tomcat线程池设置原理
- mysql 下 计算 两点 经纬度 之间的距离
- Android Fragment详解(三):实现Fragment 界面
- 常用正则表达式