# POJ 2485-Highways（并查集）

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;
}


• 本文已收录于以下专栏：

## 文章标题 POJ 2485 ： Highways（最小生成树--kruskal+并查集）

HighwaysDescription The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no...

## POJ 2485 Highways 最小生成树 （Kruskal）

Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public ...

## poj 2485 Highways 测试数据

• 2013-03-01 18:08
• 3.61MB
• 下载

## POJ 2485 Highways

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26693   Acce...

## POJ 2485 Highways【最小生成树】

//9188632 ylwh 2485 Accepted 632K 188MS G++ 1066B 2011-08-18 17:15:42 #include #include #include ...

## poj 2485 Highways

Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11071   Acce...

## POJ2485 Highways

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27173   Accepted: 12421 ...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)