poj3268 SPFA

Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9111   Accepted: 4097

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.
题意:有n个农场,编号1~N每个农场有一头奶牛,这些奶牛将参加在X号农场的举行的派对。这N个农场之间有M条单向路,通过第i条路将需要花费Ti单位时间。
  每头牛必须走着去参加派对。派对回来,返回自己的农场。选择最短时间的最优路径。注意:单向路
  对于所有的奶牛来说,花费在去派对的路上和返回农场的最长时间是多少?
思路:计算出每头牛去X参加并且回来的最短路径所需要的时间,然后求出这n-1头牛的最大值即可。
两次运用SPFA~~
①计算去X的时间:以X为源点,求出源点各个农场的最短路径;
②计算回来的时间:以X为源点,各顶点的入边作为出边,构成邻接表,求源点到农场的最短路径;
③取和求最大~~
View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <queue>
 6 using namespace std;
 7 const int M=100010;
 8 const int INF=2000000;
 9 struct Node{
10     int v,w;
11     Node *next;
12 }edge[M],redge[M],tmp[M*2];
13 int pos=0;
14 int ecost[M];
15 int n,m,src,Q[M];
16 bool vis[M];
17 void SPFA(int dir){
18     int h,t,u,i;
19     Node *ptr;
20     h=0;t=1;
21     memset(vis,0,sizeof(vis));
22     for(i=0;i<=n;i++) ecost[i]=INF;
23     Q[0]=src;
24     ecost[src]=0;
25     while(h!=t){
26         u=Q[h++];
27         vis[u]=0;
28         if(dir!=0) ptr=edge[u].next;
29         else ptr=redge[u].next;
30         while(ptr){
31             if(ecost[ptr->v]>ecost[u]+ptr->w){
32                 ecost[ptr->v]=ecost[u]+ptr->w;
33                 if(!vis[ptr->v]){
34                     Q[t++]=ptr->v;
35                     vis[ptr->v]=1;
36                 }
37             }
38             ptr=ptr->next;
39         }
40     }
41 }
42 void insert(int x,int y,int w){
43     Node *ptr=&tmp[pos++];
44     ptr->v=y;
45     ptr->w=w;
46     ptr->next=edge[x].next;
47     edge[x].next=ptr;
48     ptr=&tmp[pos++];
49     ptr->v=x;
50     ptr->w=w;
51     ptr->next=redge[y].next;
52     redge[y].next=ptr;
53 }
54 int main(){
55     int i,x,y,w;
56     int ans[M],MaxTime;
57     while(scanf("%d%d%d",&n,&m,&src)!=EOF){
58         pos=0;
59         for(i=0;i<=n;i++){
60             edge[i].next=redge[i].next=NULL;
61         }
62         for(i=0;i<m;i++){
63             scanf("%d%d%d",&x,&y,&w);
64             insert(x,y,w);
65         }
66         MaxTime=0;
67         memset(ans,0,sizeof(ans));
68         SPFA(0);
69         for(i=1;i<=n;i++){
70             if(i!=src) ans[i]+=ecost[i];
71         }
72         SPFA(1);
73         for(i=1;i<=n;i++){
74             if(i!=src){
75                 ans[i]+=ecost[i];
76                 MaxTime=max(MaxTime,ans[i]);
77             }
78         }
79         printf("%d\n",MaxTime);
80     }
81     return 0;
82 }

 

posted @ 2012-10-12 18:53  _sunshine  阅读(834)  评论(0编辑  收藏  举报