C - Heavy Transportation
Posted jaydenouyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C - Heavy Transportation相关的知识,希望对你有一定的参考价值。
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 maxn = 1000+5; 9 using namespace std; 10 11 const int INF = 0xfffff; 12 13 int mp[maxn][maxn]; 14 bool v[maxn]; 15 int n, m; 16 int dis[maxn]; 17 18 void dij(int be){ 19 int Min, p; 20 v[be] = false; 21 for(int i = 1;i <= n;i++){ 22 dis[i] = mp[i][be]; 23 } 24 dis[be] = 0; 25 26 for(int i = 1;i <= n;i++){ 27 Min = 0; 28 for(int j = 1;j <= n;j++){ 29 if(v[j] && Min < dis[j]){ 30 p = j; 31 Min = dis[j]; 32 } 33 } 34 if(Min == 0) 35 break; 36 v[p] = false; 37 for(int j = 1;j <= n;j++){ 38 dis[j] = max(dis[j], min(dis[p],mp[p][j])); 39 } 40 } 41 } 42 43 int main(){ 44 Faster; 45 int t; 46 int cnt = 0; 47 cin >> t; 48 while(t--){ 49 cnt++; 50 cin >> n >> m; 51 for(int i = 0;i <= n;i++){ 52 dis[i] = 0; 53 v[i] = true; 54 for(int j = 0;j <= n;j++){ 55 mp[i][j] = 0; 56 } 57 } 58 for(int i = 0;i < m;i++){ 59 int x, y, z; 60 cin >> x >> y >> z; 61 if(mp[x][y] < z){ 62 mp[x][y] = mp[y][x] = z; 63 } 64 } 65 66 dij(1); 67 cout << "Scenario #" << cnt << ":"<< endl; 68 cout << dis[n] << endl << endl; 69 } 70 return 0; 71 }
以上是关于C - Heavy Transportation的主要内容,如果未能解决你的问题,请参考以下文章