标签:nbsp 进入 mat sam algo java 工作 结束 \n
C/C++:
1 #include <map> 2 #include <queue> 3 #include <cmath> 4 #include <string> 5 #include <cstring> 6 #include <cstdio> 7 #include <climits> 8 #include <algorithm> 9 #define INF 0xffffff 10 using namespace std; 11 12 int n, m, my_map[110][110]; 13 14 int my_dijkstra() 15 { 16 int my_dis[110], my_book[110] = {0, 1}; 17 for (int i = 1; i <= n; ++ i) 18 my_dis[i] = my_map[i][1]; 19 while (1) 20 { 21 int my_pos = -1, my_min = INF; 22 for (int i = 1; i <= n; ++ i) 23 { 24 if (!my_book[i] && my_dis[i] < my_min) 25 { 26 my_min = my_dis[i]; 27 my_pos = i; 28 } 29 } 30 if (my_pos == -1) break; 31 my_book[my_pos] = 1; 32 for (int i = 1; i <= n; ++ i) 33 if (!my_book[i]) 34 my_dis[i] = min(my_dis[i], my_dis[my_pos] + my_map[my_pos][i]); 35 } 36 return my_dis[n]; 37 } 38 39 int main() 40 { 41 while (~scanf("%d%d", &n, &m), n || m) 42 { 43 /** 44 Initialize 45 */ 46 for (int i = 1; i <= n; ++ i) 47 for (int j = 1; j <= n; ++ j) 48 my_map[i][j] = i == j ? 0 : INF; 49 50 /** 51 Date Input 52 */ 53 for (int i = 1; i <= m; ++ i) 54 { 55 int a, b, a_b_dis; 56 scanf("%d%d%d", &a, &b, &a_b_dis); 57 my_map[a][b] = my_map[b][a] = min(my_map[a][b], a_b_dis); 58 } 59 printf("%d\n", my_dijkstra()); 60 } 61 return 0; 62 }
标签:nbsp 进入 mat sam algo java 工作 结束 \n
原文地址:https://www.cnblogs.com/GetcharZp/p/9436664.html