赤裸裸的MST问题。
#include<iostream>
#include<cstdio>
#define FOR(i, n) for(int i = 0; i < (n); i++)
using namespace std;
int cost[500][500];
bool inTree[500];
void init(int N){
FOR(i, N)
FOR(j, N)
scanf("%d", &cost[i][j]);
FOR(i, N)
inTree[i] = false;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
int N;
scanf("%d", &N);
init(N);
int index;
int result = -1;
inTree[0] = true;
FOR(a, N - 1){
int MIN = 100000;
FOR(i, N){
if(inTree[i])
FOR(j, N){
if(!inTree[j] && cost[i][j] < MIN){
index = j;
MIN = cost[i][j];
}
}
}
if(result < MIN)
result = MIN;
inTree[index] = true;
}
printf("%d\n", result);
}
}