sicily 1090. Highways

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int map[1000][1000],cost[1000],longest;
bool ever[1000];
void prim(int v,int n){
	longest = 0;
	for(int i=0;i<n;++i){
		cost[i] = map[v][i];
		ever[i] = 0;
	}
	ever[v] = 1;
	for(int i=0;i<n;++i){
		int min = 65536,next = 0;
		for(int j=0;j<n;++j){
			if(cost[j] < min && !ever[j]){
				min = cost[j];
				next = j;
			}
		}
		ever[next] = 1;
		if(cost[next] > longest) longest = cost[next];
		for(int j=0;j<n;++j){
			if(map[next][j]< cost[j]&&!ever[j]){
				cost[j] = map[next][j];
			}
		}
	}
}
int main(){
	int T,n;
	cin>>T;
	while(T--){
		cin>>n;
		for(int i=0;i<n;++i)
			for(int j=0;j<n;++j)
				scanf("%d",&map[i][j]);
		prim(0,n);
		cout<<longest<<endl;
		if(T)
		    cout<<endl;
	}
	return 0;
}

你可能感兴趣的:(sicily 1090. Highways)