关闭

POJ 2485-Highways(并查集)

标签: 并查集poj
120人阅读 评论(0) 收藏 举报
分类:
H - Highways
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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.


思路:
   这次不是求最小生成树,而是求最小生成树中的最大边,模板题。



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
typedef long long ll;
using namespace std;
#define T 55000
#define inf 0x3f3f3f3f
int n,c;
struct node
{
	int u,v,val;
	bool operator<(const node& a)const{
		return val<a.val;
	}
	node(){}
	node(int _u,int _v,int _val):u(_u),v(_v),val(_val){}
}v[T];
int par[T];
void Init(int n)
{
	for(int i=0;i<=n;++i){
		par[i] = i;
	}
}
int find(int x)
{
	int xx = x;
	while(par[xx]!=xx)
	{
		xx = par[xx];
	}
	while(xx!=x)
	{
		int tmp = par[x];
		par[x] = xx;
		x = tmp;
	}
	return xx;
}
int kruskal()
{
	Init(c);
	int ma = 0,cnt=0;
	int tx,ty,i;
	for(i=0;i<c;++i){
		tx = find(v[i].u);
		ty = find(v[i].v);
		if(tx!=ty){
			cnt++;ma = max(ma,v[i].val);
			par[tx] = ty;
		}	
	}
	return ma;
}
int main()
{
#ifdef zsc
 freopen("input.txt","r",stdin);
#endif
 int N,i,j,k;
 scanf("%d",&N);
 while(N--)
 {
	 scanf("%d",&n);
	 c = 0;
	 for(i=0;i<n;++i){
		 for(j=0;j<n;++j){
		   scanf("%d",&k);
		   if(i<j){
			   v[c++] = node(i,j,k);
		   }
		 }
	 }
	 sort(v,v+c);
	 printf("%d\n",kruskal());
 }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:235528次
    • 积分:10260
    • 等级:
    • 排名:第1642名
    • 原创:777篇
    • 转载:186篇
    • 译文:1篇
    • 评论:37条
    博客专栏