标签:cout break space 注意 iostream std code div amp
因为这个图比较稠密,所以用prim算法
每次选到最小边的时候判断,保存最小边里面最大的边
注意要用scanf不然会超时
#include<iostream> #include<cstdio> #include<cstring> #include<string> using namespace std; int main() { int n; cin >> n; while (n--) { int m; scanf("%d", &m); int map[501][501] = { 0 }; for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { scanf("%d", &map[i][j]); } } int max = 0; int pos[501] = { 0 }; int book[501] = { 0 }; for (int i = 1; i <= m; i++) { pos[i] = map[1][i]; } book[1] = 1; for(int k=1;k<m;k++){ int min = 99999999; int t = 0; for (int i = 1; i <= m; i++) { if (pos[i] < min && book[i] == 0) { min = pos[i]; t = i; } } if (t == 0) { break; } book[t] = 1; if (min > max) { max = min; } for (int i = 1; i <= m; i++) { if (map[t][i] < pos[i] && book[i] == 0) { pos[i] = map[t][i]; } } } cout << max << endl; } return 0; }
标签:cout break space 注意 iostream std code div amp
原文地址:https://www.cnblogs.com/Vetsama/p/12234823.html