[sicily online]1090. Highways

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

题目分析;

刚开始错误的分析:

//把所有数据从大到小排序
//然后把最大的去掉,其所在的行和列各减1
//直到是该行或者该列最后一个元素(除了对角线)  我觉得这个就是结果

以上这种分析,没有考虑到连通性的问题,比如四个点,最后可能得到 两两相连,而不是全连通的

正确做法:最小生成树,prim算法

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<iomanip>
#include<list>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <stack>
#include<queue>
#include<string.h>
using namespace std;


int data[501][501];
bool flag[501];

int main()
{
	int geshu;
	cin>>geshu;
	for(int xx=0;xx<geshu;xx++)
	{
		memset(data,0,sizeof(data));
		memset(flag,0,sizeof(flag));
		int n;
		cin>>n;
		int m=n*n;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
				cin>>data[i][j];
		}
		vector<int> muster;
		flag[0]=true;
		muster.push_back(0);
		int result=0;
		while(muster.size()<n)
		{
			int index=0,max=99999;
			for(vector<int>::size_type i=0;i<muster.size();i++)
			{
				for(int j=0;j<n;j++)
				{
					if(data[muster[i]][j]!=0&&flag[j]==false&&data[muster[i]][j]<max)
					{
						max=data[muster[i]][j];
						index=j;
					}
				}//end for j
			}//end for i
			flag[index]=true;
			muster.push_back(index);
			if(result<max)
				result=max;
		}//end while
		cout<<result<<endl;
		if(xx!=geshu-1)
			cout<<endl;
	}//end for xx
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值