POJ 2485-Highways(并查集)

原创 2015年11月20日 23:52:35
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;
}


版权声明:本文为博主原创文章,转载必须声明出处,thank。

poj-2485-Highways-最小生成树的最大权边

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17064   Acce...
  • yujuan_Mao
  • yujuan_Mao
  • 2013年01月10日 22:20
  • 1155

POJ 1182 食物链(经典并查集)

Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。  现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道...
  • gemire
  • gemire
  • 2014年03月05日 20:27
  • 4237

POJ 1272 小希的迷宫【并查集+无向图判断是否有环 PS:和前一篇判断入度是否为1的很像】

小希的迷宫   Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先...
  • Cfreezhan
  • Cfreezhan
  • 2013年04月07日 20:58
  • 1221

文章标题 POJ 2485 : Highways(最小生成树--kruskal+并查集)

HighwaysDescription The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no...
  • Wang_SF2015
  • Wang_SF2015
  • 2016年11月13日 21:53
  • 145

poj 1751 -- Highways (并查集+最小生成树)

题目大意:给定n个城市,标记1-n,每个城市给定坐标,但城市间已经有一些道路,要再修路使所有城市相通并使所修的路最短,输出每一条路的编号 思路:先用并查集处理已有的路,然后prim #...
  • dlut_ju
  • dlut_ju
  • 2013年01月06日 20:14
  • 359

POJ2485-Highways【Prim】

  • 2011年07月30日 18:35
  • 8KB
  • 下载

POJ 1988 简单并查集,

  • 2011年03月18日 18:00
  • 2KB
  • 下载

poj 2485 Highways 测试数据

  • 2013年03月01日 18:08
  • 3.61MB
  • 下载

1090. Highways(用并查集找出最小生成树,输出最小生成树中最长的边

/*1090. Highways(用并查集找出最小生成树,输出最小生成树中最长的边 */ #include #include #include #include using na...
  • xiehaoyun2012
  • xiehaoyun2012
  • 2013年01月09日 01:11
  • 283

【POJ 2485】Highways(最小生成树)

点击打开题目 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissio...
  • Manton_dong
  • Manton_dong
  • 2017年08月03日 23:13
  • 66
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 2485-Highways(并查集)
举报原因:
原因补充:

(最多只允许输入30个字)