标签:bre bool include int turn ffffff fine name col
1 //Dijkstra 2 #include <iostream> 3 #include <algorithm> 4 #define Faster ios_base::sync_with_stdio(false),cin.tie(0) 5 #define Read freopen("in.txt","r",stdin),freopen("out.txt","w",stdout) 6 #define Close fclose(stdin),fclose(stdout) 7 const int maxn = 1000+5; 8 const int INF = 0xfffffff; 9 using namespace std; 10 11 int g[maxn][maxn]; 12 bool v[maxn]; 13 int dis[maxn]; 14 15 int t, n; 16 17 //x 为起点 18 void dij(int x){ 19 for(int i = 0;i <= n;i++){ 20 dis[i] = g[x][i]; 21 v[i] = true; 22 } 23 v[x] = false; 24 25 int Min, p; 26 for(int i = 1;i <= n;i++){ 27 Min = INF; 28 for(int j = 1;j <= n;j++){ 29 if(v[j] && dis[j] < Min){ 30 Min = dis[j]; 31 p = j; 32 } 33 } 34 if(Min == INF) 35 break; 36 v[p] = false; 37 for(int j = 1;j <= n;j++){ 38 if(v[j] && dis[p] + g[p][j] < dis[j]){ 39 dis[j] = dis[p] + g[p][j]; 40 } 41 } 42 } 43 } 44 45 int main(){ 46 Faster; 47 cin >> t >> n; 48 for(int i = 0;i <= n;i++){ 49 for(int j = 0;j <= n;j++) 50 g[i][j] = INF; 51 } 52 for(int i = 0;i < t;i++){ 53 int x, y, z; 54 cin >> x >> y >> z; 55 if(g[x][y] > z) 56 g[x][y] = g[y][x] = z; 57 } 58 dij(1); 59 cout << dis[n] << endl; 60 return 0; 61 }
标签:bre bool include int turn ffffff fine name col
原文地址:https://www.cnblogs.com/jaydenouyang/p/9010635.html