poj 3268 Silver Cow Party
2012-11-20 22:35
351 查看
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.
Source
USACO 2007 February Silver
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9222 | Accepted: 4156 |
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.
Source
USACO 2007 February Silver
和上一题一样的做法 只是题目要求的输出结果不同 #include <iostream> #include <stdio.h> #include <queue> #include <stack> #include <set> #include <vector> #include <math.h> #include <string.h> #include <algorithm> using namespace std; #define N 100002 #define Max 1000000002 struct node { int to; int next; int value; }Eg ; struct Record { int x,y; int value; }rc ; int dis1[1002],dis2[1002]; int v[1002]; bool b[1002]; int n,m,x; void SFPA(int dis[]) { memset(b,0,sizeof(b)); queue<int> Q; Q.push(x); b[x]=1; int e,i,k; while(!Q.empty()) { i=Q.front(); Q.pop(); b[i]=0; for(e=v[i];e!=-1;e=Eg[e].next) { k=Eg[e].to; if(dis[k]>dis[i]+Eg[e].value) { if(!b[k]) { Q.push(k); b[k]=1; } dis[k]=dis[i]+Eg[e].value; } } } } int main() { int i; while(scanf("%d %d %d",&n,&m,&x)!=EOF) { for(i=1;i<=n;i++) v[i]=-1; for(i=0;i<m;i++) { scanf("%d %d %d",&rc[i].x,&rc[i].y,&rc[i].value); Eg[i].value=rc[i].value; Eg[i].to=rc[i].y; Eg[i].next=v[rc[i].x]; v[rc[i].x]=i; } for(i=1;i<=n;i++) dis1[i]=Max; dis1[x]=0; SFPA(dis1); __int64 ans=0; for(i=1;i<=n;i++) v[i]=-1; for(i=0;i<m;i++) { Eg[i].value=rc[i].value; Eg[i].to=rc[i].x; Eg[i].next=v[rc[i].y]; v[rc[i].y]=i; } for(i=1;i<=n;i++) dis2[i]=Max; dis2[x]=0; SFPA(dis2); for(i=1;i<=n;i++) { dis1[i]+=dis2[i]; ans=ans>dis1[i]?ans:dis1[i]; } printf("%I64d\n",ans); } 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 (往返最短路,Floyd,Dijkstra 2次优化)
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party(最短路径之迪杰斯特拉算法)
- poj3268——Silver Cow Party(最短路+godv之力)
- 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(最短路+SPFA)
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party (最短路)
- poj 3268 Silver Cow Party & zoj 2008 Invitation Cards
- POJ - 3268 Silver Cow Party解题报告(dijkstra分别求单源起点和单源终点的最短路)