poj2485 - Highways

                                   想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410
                                     转载请注明出处:
http://blog.csdn.net/wangjian8006

 

/*
prim
Memory 644K
Time  157MS 
*/
#include <iostream>
using namespace std;
#define MAXV 600
#define MAXINT INT_MAX

int a[MAXV][MAXV];
int d[MAXV],discover[MAXV];

int prime(int n){
	int i,j;
	int t;
	for(i=1;i<=n;i++){
		d[i]=a[1][i];
		discover[i]=0;
	}
	
	for(i=1;i<=n;i++){
		int min=MAXINT;
		for(j=1;j<=n;j++)
			if(!discover[j] && d[j]<min){
				min=d[j];
				t=j;
			}
			
			discover[t]=1;
			
		for(j=1;j<=n;j++){
			if(!discover[j] && a[t][j]<d[j])
				d[j]=a[t][j];
		}
	}
	int max=-1;
	for(i=1;i<=n;i++)
		if(d[i]>max) max=d[i];
	return max;
}

int main(){
	int n,t,i,j;
	scanf("%d",&n);
	while(n--){
		scanf("%d",&t);
		for(i=1;i<=t;i++){
			for(j=1;j<=t;j++)
				scanf("%d",&a[i][j]);
		}
		printf("%d\n",prime(t));
	}
	return 0;
}


 

 

你可能感兴趣的:(poj2485 - Highways)