码迷,mamicode.com
首页 > 其他好文 > 详细

Silver Cow Party

时间:2018-03-22 01:45:34      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:over   mount   sample   min   最大   ber   string   inpu   font   

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: NM, and X 
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farmBi, 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个农场,m条道路,每个人都要到X农场去,(注意这里的路是单向路)
  所以i->x 和x->i 不一定是同一条路。
  求出i->x 和x->i 的最短路的最大值
  反向跑一下最短路就是回去的路了。
 
 1 #include <cstdio>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 const int INF=100000000;
 6 int tu[1010][1010],vis[1010],d[2][1010];
 7 int n,m,x;
 8 int ans()
 9 {
10     int w=x,mins=INF;
11     memset(vis,0,sizeof(vis));
12     for (int i=1 ;i<=n ;i++){
13         d[0][i]=tu[x][i];
14         d[1][i]=tu[i][x];
15     }
16     vis[x]=1;
17     d[0][x]=0;
18     for (int i=1 ;i<=n ;i++){
19         mins=INF;
20         for (int j=1 ;j<=n ;j++){
21             if (!vis[j] && d[0][j]<mins ) {
22                 w=j;
23                 mins=d[0][j];
24             }
25         }
26         vis[w]=1;
27         for (int j=1 ;j<=n ;j++){
28             if (!vis[j]&& tu[w][j]!=INF && d[0][j]>d[0][w]+tu[w][j]) d[0][j]=d[0][w]+tu[w][j];
29         }
30     }
31     memset(vis,0,sizeof(vis));
32     vis[x]=1;
33     d[1][x]=0;
34     for (int i=1 ;i<=n ;i++){
35         mins=INF;
36         for (int j=1 ;j<=n ;j++){
37             if (!vis[j]&& d[1][j]<mins){
38             w=j;
39             mins=d[1][j];
40             }
41         }
42         vis[w]=1;
43         for (int j=1 ;j<=n ;j++){
44             if (!vis[j] &&tu[j][w]!=INF && d[1][j]>d[1][w]+tu[j][w]) d[1][j]=d[1][w]+tu[j][w];
45         }
46     }
47     mins=-INF,w=x;
48     for (int i=1 ;i<=n ;i++){
49         if (d[0][i]!=INF && d[1][i]!=INF && i!=x && d[0][i]+d[1][i]>mins) {
50             mins=d[0][i]+d[1][i];
51             w=i;
52         }
53     }
54     return (d[0][w]+d[1][w]);
55 }
56 int main()
57 {
58     while(scanf("%d%d%d",&n,&m,&x)!=EOF){
59         for (int i=1 ;i<=n ;i++){
60             for (int j=1 ;j<=n ;j++){
61                 tu[i][j]=INF;
62             }
63         }
64         for (int i=0 ;i<m ;i++){
65             int a,b,c;
66             scanf("%d%d%d",&a,&b,&c);
67             tu[a][b]=c;
68         }
69         printf("%d\n",ans());
70     }
71     return 0;
72 }

 

 
 
 

Silver Cow Party

标签:over   mount   sample   min   最大   ber   string   inpu   font   

原文地址:https://www.cnblogs.com/qldabiaoge/p/8620786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!