标签:多少 while 之间 mat mes space out NPU 道路
C\C++:
1 #include <cmath> 2 #include <cstdio> 3 #include <climits> 4 #include <algorithm> 5 #define INF 0xffffff 6 using namespace std; 7 8 int n, m, my_map[210][210], my_start, my_end; 9 10 int my_dijkstra() 11 { 12 int my_dis[210], my_book[210] = {0}; 13 my_book[my_start] = 1; 14 for (int i = 0; i < n; ++ i) 15 my_dis[i] = my_map[i][my_start]; 16 17 while (1) 18 { 19 int my_min = INF, my_temp_pos = -1; 20 for (int i = 0; i < n; ++ i) 21 { 22 if (!my_book[i] && my_dis[i] < my_min) 23 { 24 my_min = my_dis[i]; 25 my_temp_pos = i; 26 } 27 } 28 if (my_temp_pos == -1) break; 29 my_book[my_temp_pos] = 1; 30 31 for (int i = 0; i < n; ++ i) 32 { 33 if (!my_book[i] && my_dis[i] > my_map[i][my_temp_pos] + my_dis[my_temp_pos]) 34 my_dis[i] = my_map[i][my_temp_pos] + my_dis[my_temp_pos]; 35 } 36 } 37 if (my_dis[my_end] == INF) return -1; 38 return my_dis[my_end]; 39 } 40 41 int main() 42 { 43 /** 44 Date Input Initialize 45 */ 46 while (~scanf("%d%d", &n, &m)) 47 { 48 for (int i = 0; i < n; ++ i) 49 for (int j = 0; j < n; ++ j) 50 my_map[i][j] = i == j ? 0 : INF; 51 52 for (int i = 0; i < m; ++ i) 53 { 54 int a, b, a_b_dis; 55 scanf("%d%d%d", &a, &b, &a_b_dis); 56 if (my_map[a][b] > a_b_dis) 57 my_map[a][b] = my_map[b][a] = a_b_dis; 58 } 59 scanf("%d%d", &my_start, &my_end); 60 printf("%d\n", my_dijkstra()); 61 } 62 return 0; 63 }
hdu 1874 畅通工程续 (dijkstra(不能用于负环))
标签:多少 while 之间 mat mes space out NPU 道路
原文地址:https://www.cnblogs.com/GetcharZp/p/9434289.html