POJ3258——Silver Cow Party
来源:互联网 发布:安卓一键改串号软件 编辑:程序博客网 时间:2024/04/30 01:39
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
模板题,不多解释了,用的是spfa。
#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<iterator>#include<vector>#include<queue>#include<list>#include<stack>#include<map>#include<set>#define inf 0x3f3f3f3fusing namespace std;const int maxn=100010;struct node{int to;int weight;int next;}edge1[100010],edge2[100010];int head1[maxn],head2[maxn];int tot1,tot2;void addedge1(int from,int to,int weight){edge1[tot1].to=to;edge1[tot1].weight=weight;edge1[tot1].next=head1[from];head1[from]=tot1++;}void addedge2( int from,int to,int weight){edge2[tot2].to=to;edge2[tot2].weight=weight;edge2[tot2].next=head2[from];head2[from]=tot2++;}int dist1[1010],dist2[1010];void spfa_1(int n,int v){memset(dist1,inf,sizeof(dist1));queue<int>qu;dist1[v]=0;while(!qu.empty()) qu.pop(); qu.push(v); while(!qu.empty()) { int cur=qu.front(); qu.pop(); for(int i=head1[cur];i!=-1;i=edge1[i].next) { if(dist1[edge1[i].to] > dist1[cur]+edge1[i].weight) { dist1[edge1[i].to] = dist1[cur]+edge1[i].weight; qu.push(edge1[i].to); } } }}void spfa_2(int n,int v){memset(dist2,inf,sizeof(dist1));queue<int>qu;dist2[v]=0;while(!qu.empty()) qu.pop(); qu.push(v); while(!qu.empty()) { int cur=qu.front(); qu.pop(); for(int i=head2[cur];i!=-1;i=edge2[i].next) { if(dist2[edge2[i].to] > dist2[cur]+edge2[i].weight) { dist2[edge2[i].to] = dist2[cur]+edge2[i].weight; qu.push(edge2[i].to); } } }}int main(){int n,m,x;while(~scanf("%d%d%d",&n,&m,&x)){memset(head1,-1,sizeof(head1));memset(head2,-1,sizeof(head2));tot1=0;tot2=0;int s,t,w;while(m--){scanf("%d%d%d",&s,&t,&w);addedge1(s,t,w);addedge2(t,s,w);}int maxs=0;spfa_1(n,x);spfa_2(n,x);for(int i=1;i<=n;i++) maxs=max(maxs,dist1[i]+dist2[i]); printf("%d\n",maxs);}return 0;}
- POJ3258——Silver Cow Party
- POJ3268—Silver Cow Party
- POJ3268—Silver Cow Party
- 刷题——Silver Cow Party POJ
- 北大ACM3268——Silver Cow Party~~最短路径
- POJ 3268 Silver Cow Party——dijkstra
- POJ3268—Silver Cow Party(反向最短路)
- Silver Cow Party
- POJ3268:Silver Cow Party
- poj3268 - Silver Cow Party
- POJ3268 Silver Cow Party
- POJ3268 Silver Cow Party
- Silver Cow Party
- poj_3268 Silver Cow Party
- POJ3268 Silver Cow Party
- K - Silver Cow Party
- poj3268-Silver Cow Party
- Silver Cow Party
- Hibernate *;ver() as rownumber_,;<select_sublist>
- 黑马程序员 java学习笔记——交通灯管理系统
- 首页商城18商城大的折扣手机
- 收拾一个浪漫的野餐篮惊喜你的爱人
- 搜索引擎优化基础知识
- POJ3258——Silver Cow Party
- Qt编写串口通信程序全程图文讲解
- int/byte/byte[]转换 c#
- 面向对象总结(上)
- 面试技巧:16个经典面试问题回答思路
- 手机
- 手机下载宇宙
- 一些大牛的博客推荐,排名不分先后
- 共模电压和差模电压