标签:end cst 长度 memory 整数 while BMI span mit
C/C++:
1 #include <cstdio> 2 #include <climits> 3 using namespace std; 4 5 int n, my_map[110][110]; 6 7 int my_prim() 8 { 9 int my_pos = 1, my_book[110] = {0, 1}, my_dis[110] = {0, INT_MAX}, my_ans = 0; 10 for (int i = 2; i <= n; ++ i) 11 my_dis[i] = my_map[my_pos][i]; 12 for (int i = 1; i < n; ++ i) 13 { 14 int my_temp = INT_MAX; 15 for (int j = 1; j <= n; ++ j) 16 { 17 if (!my_book[j] && my_dis[j] < my_temp) 18 { 19 my_temp = my_dis[j]; 20 my_pos = j; 21 } 22 } 23 my_ans += my_temp; 24 my_book[my_pos] = 1; 25 for (int j = 1; j <= n; ++ j) 26 if (my_dis[j] > my_map[j][my_pos]) 27 my_dis[j] = my_map[j][my_pos]; 28 } 29 return my_ans; 30 } 31 32 int main() 33 { 34 while (scanf("%d", &n), n) 35 { 36 int my_line = n * (n-1) / 2; 37 for (int i = 0; i < my_line; ++ i) 38 { 39 int a, b, a_b_dis; 40 scanf("%d%d%d", &a, &b, &a_b_dis); 41 my_map[a][b] = my_map[b][a] = a_b_dis; 42 } 43 printf("%d\n", my_prim()); 44 } 45 return 0; 46 }
标签:end cst 长度 memory 整数 while BMI span mit
原文地址:https://www.cnblogs.com/GetcharZp/p/9426834.html