poj2485

Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18655   Accepted: 8638

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

Input

The first line of input is an integer T, which tells how many test cases followed.
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.

Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
using namespace std;

const __int64 inf=9999999;          //无穷大(两点间边权最大为7)
const __int64 large=2001;

int n;  //truck types
char str[large][8];
__int64 dist[large][large]={0};

/*Compute Weight*/



/*Prim Algorithm*/

__int64 prim(void)
{
	int s=1;       //源点(最初的源点为1)
	int m=1;       //记录最小生成树的顶点数
	bool u[large]; //记录某顶点是否属于最小生成树
	__int64 prim_w=0,max=0;  //最小生成树的总权值
	__int64 min_w;     //每个新源点到其它点的最短路
	int flag_point;
	__int64 low_dis[large];  //各个源点到其它点的最短路

	memset(low_dis,inf,sizeof(low_dis));
	memset(u,false,sizeof(u));
	u[s]=true;

	while(1)
	{
		if(m==n)      //当最小生成树的顶点数等于原图的顶点数时,说明最小生成树查找完毕
			break;

		min_w=inf;
		for(int j=2;j<=n;j++)
		{
			if(!u[j] && low_dis[j]>dist[s][j])
				low_dis[j] = dist[s][j];
			if(!u[j] && min_w>low_dis[j])
			{
				min_w=low_dis[j];
				flag_point=j;      //记录最小权边中不属于最小生成树的点j
			}
		}
		s=flag_point;       //顶点j与旧源点合并
		u[s]=true;          //j点并入最小生成树(相当于从图上删除j点,让新源点接替所有j点具备的特征)
		prim_w+=min_w;      //当前最小生成树的总权值
		if(min_w>max)
			max=min_w;
		m++;                
	}
	return max;
}

int main(void)
{
	int i,j;
	int T;
	scanf("%d",&T);
	while(T--)
	{
	scanf("%d",&n);
	
		/*Input*/
		
	

		/*Structure Maps*/

		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				scanf("%I64d",&dist[i][j]);

		//Prim Algorithm & Output

		printf("%I64d\n",prim());

	
	}
	return 0;
}



你可能感兴趣的:(poj2485)