poj 3268 Silver Cow Party

Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9222   Accepted: 4156

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 ≤ XN). 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

和上一题一样的做法
只是题目要求的输出结果不同

#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[N]; struct Record { int x,y; int value; }rc[N]; 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; }

posted on 2012-11-20 22:35  江财小子  阅读(350)  评论(0编辑  收藏  举报