D - Silver Cow Party

 1 //两次dijkstra
 2 #include <iostream>
 3 #include <algorithm>
 4 
 5 #define Faster ios::sync_with_stdio(false),cin.tie(0)
 6 #define Read freopen("in.txt","r",stdin),freopen("out.txt","w",stdout)
 7 #define Close fclose(stdin),fclose(stdout)
 8 const int INF = 0xffffff;
 9 const int maxn = 1005;
10 using namespace std;
11 
12 int mp[maxn][maxn];
13 bool v[maxn];
14 bool v2[maxn];
15 int dis[maxn];
16 int dis2[maxn];
17 
18 int n;
19 
20 void dijkstra(int x){
21     for(int i = 1;i <= n;i++){
22         dis[i] = mp[x][i];
23         dis2[i] = mp[i][x];
24         v[i] = true;
25         v2[i] = true;
26     }
27     dis[x] = dis2[x] = 0;
28     v[x] = v2[x] = false;
29 
30     for(int i = 1;i < n;i++){
31         int Min = INF;
32         int p;
33         for(int j = 1;j <= n;j++){
34             if(v[j] && dis[j] < Min){
35                 Min = dis[j];
36                 p = j;
37             }
38         }
39         if(Min == INF)
40             break;
41         v[p] = false;
42         for(int j = 1;j <= n;j++){
43             if(v[j] && dis[j] > dis[p] + mp[p][j])
44                 dis[j] = dis[p] + mp[p][j];
45         }
46     }
47 
48     for(int i = 0;i < n;i++){
49         int Min = INF;
50         int p;
51         for(int j = 1;j <= n;j++){
52             if(v2[j] && dis2[j] < Min){
53                 Min = dis2[j] ;
54                 p = j;
55             }
56         }
57         if(Min == INF)
58             break;
59         v2[p] = false;
60         for(int j = 1;j <= n;j++){
61             if(v2[j] && dis2[j] > dis2[p] + mp[j][p])
62                 dis2[j] = dis2[p] + mp[j][p];
63         }
64     }
65 }
66 
67 int main(){
68     Faster;
69     int m, k;
70     cin >> n >> m >> k;
71     for(int i = 0;i <= n;i++)
72     for(int j = 0;j <= n;j++){
73         mp[i][j] = INF;
74         if(i == j)
75             mp[i][j] = 0;
76     }
77     for(int i = 0;i < m;i++){
78         int x, y, z;
79         cin >> x >> y >> z;
80         if(mp[x][y] > z)
81             mp[x][y] = z;
82     }
83 
84     dijkstra(k);
85 
86     int ans = 0;
87     for(int i = 1;i <= n;i++){
88         ans = max(ans, dis[i]+dis2[i]);
89         //cout << "go: " << dis2[i] << " , ovel : " << dis[i] << endl;
90     }
91     cout << ans << endl;
92     return 0;
93 }

 

posted @ 2018-05-10 10:01  ouyang_wsgwz  阅读(187)  评论(0编辑  收藏  举报