1007. Highways

题目

Time Limit: 1sec Memory Limit:32MB
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 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.
Output
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.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.
Sample Input Copy
1

3
0 990 692
990 0 179
692 179 0
Sample Output Copy
692

学习

/*
参考:
代码细读:https://blog.csdn.net/sinat_37668729/article/details/76686772
图解:https://blog.csdn.net/jnu_simba/article/details/8869876
两种算法:https://blog.csdn.net/qq_31975227/article/details/66968820
*/

code

#include
#include
#include
using namespace std;
#define Max 501
#define INFINITE 0x7f
int mat[Max][Max];//连通性 
bool isVisited[Max];//是否在生成树中 
int minn[Max];//每个蓝点与树中白点最小权值 

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int vers;
		cin >> vers;
		{
			for(int i = 1; i <= vers; ++i)
				for(int j = 1; j <= vers; ++j)
				{
					cin >> mat[i][j]; 
				//	mat[j][i] = mat[i][j];
				}
					
		}
		//初始化 
		memset(minn, INFINITE, sizeof(minn));//所有蓝点均为最大 
		memset(isVisited, false, 501 * sizeof(bool));//所有顶点都为进入树中 
		minn[1] = 0;//从1开始遍历 
		
		for(int i = 1; i <= vers; ++i)//vers - 1次连线 
		{
			//找到一个与白点相连的最小权值的未进入树中的蓝点k,因为蓝点未付给正常值,仍是为无穷大
			int k = 0;//min[k] = oo;
			for(int j = 1; j <= vers; ++j)
				if((!isVisited[j]) && (minn[j] < minn[k]))
					k = j;
			//将k加入生成树
			isVisited[k] = true;
			//修改与k相连的所有未进入生成树的点,如果新点与其的距离更小,则改变min[j]的值,方便定位下一个蓝点 
			for(int j = 1; j <= vers; ++j)
			{
				if((!isVisited[j]) && (mat[k][j] < minn[j]))
					minn[j] = mat[k][j];
			} 
		}
		//找出生成树中的最大权值的边 
		int p = -1;
		for(int i = 1; i <= vers; ++i)
			if(p < minn[i]) 
				p = minn[i];
		cout << p << endl;
		
	
	}
	
	return 0;
} 

你可能感兴趣的:(Sicily)