程帅霞

不断受挫,不停起身,不断追寻,不止AC~~

导航

Silver Cow Party

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 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.
 
 
两个djk()求出到n和从n离去的dis[]和way[]的值,求和找出最大的值
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 #include<set>
 5 #include<iostream>
 6 #include<map>
 7 #include<stack>
 8 #include<cmath>
 9 #include<algorithm>
10 #define ll long long
11 using namespace std;
12 const int N = 1001;
13 const int inf = 0x3f3f3f3f;
14 int n,m,k,dis[N],way[N],dp[N][N],vis[N];
15 void djk()
16 {
17     memset(vis,0,sizeof(vis));
18     for(int i=1;i<=n;i++)
19     {
20         dis[i]=dp[k][i];
21     }
22     while(1)
23     {
24         int minn=inf,u=-1;
25         for(int j=1;j<=n;j++)
26         {
27             if(!vis[j]&&minn>dis[j])
28             {
29                 minn=dis[j];
30                 u=j;
31             }
32         }
33         if(u==-1) break;
34         vis[u]=1;
35         for(int j=1;j<=n;j++)
36         {
37             if(!vis[j]&&dis[j]>dis[u]+dp[u][j])
38             {
39                 dis[j]=dis[u]+dp[u][j];
40             }
41         }
42     }
43 }
44 
45 
46 int main()
47 {
48     while(~scanf("%d%d%d",&n,&m,&k))
49     {
50         for(int i=1;i<=n;i++)
51         {
52             for(int j=1;j<=n;j++)
53             {
54                 if(i==j) dp[i][j]=0;
55                 else  dp[i][j]=inf;
56             }
57         }
58         while(m--)
59         {
60             int u,v,w;
61             scanf("%d%d%d",&u,&v,&w);
62             dp[u][v]=w;
63         }
64         djk();
65         for(int i=1;i<=n;i++)
66             way[i]=dis[i];
67         for(int i=1;i<=n;i++)
68         {
69             for(int j=i+1;j<=n;j++)
70             {
71                 swap(dp[i][j],dp[j][i]);
72             }
73         }
74         djk();
75         int ans=0;
76         for(int i=1;i<=n;i++)
77         {
78             if(dis[i]+way[i]>ans)
79             {
80                 ans=dis[i]+way[i];
81             }
82         }
83         printf("%d\n",ans);
84     }
85 }

 

posted on 2020-09-21 19:49  程帅霞  阅读(209)  评论(0)    收藏  举报