Highways POJ - 2485 (最小生成树)

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.

 

题意:求生成的最小生成树中的最大边

解题方法:克鲁斯卡尔(Kruskal)或者普里姆(Prim)算法 哪个都行

这道题就是找最小生成树的最大边,裸的最小生成树,输入数据是邻接矩阵表示图,稍微做个转换就好。

另外需要注意两件事儿,第一题中输入数据可能较大,不要用C++输出流,第二,这道题我用C++提交会超时,G++提交AC,是因为选择C++的话,则用的是最标准的编译方式(ANSI C++)编译。但是你用的是G++的话,则你用的是GNU项目中最平凡适用人群最多的编译器(也就是Code::Blocks自带的编译器,Windows环境里一般是MinGW下的gcc,Linux中的和前者基本是一个东西,类似的还有选择C和GCC)进行编译。由于两者实质的编译器不同,也就意味着对代码的优化也会不同。以这道题为例,会出现C++超时G++AC的情况,但是在别的题中,由于题目本身对数据精度要求较高,会出现G++直接WA的情况也是很可能的,此时需要在具体情况选择合适的提交方式交题,以避免不必要的罚时。

代码:

#include<iostream>
#include<map>
#include<algorithm>
#include<cmath>
#include<cstdio>

using namespace std;

int n, maps[505][505], point[505];

struct edge{
	int u;
	int v;
	int w;
}e[100001];

bool cmp(edge a, edge b){
	return a.w < b.w;
}

int getf(int x){
	if(x == point[x])
		return x;
	else{
		point[x] = getf(point[x]);
		return point[x];
	}
}

int merge(int v, int u){
	int t1, t2;
	t1 = getf(v);
	t2 = getf(u);
	if(t1 != t2){
		point[t2] = t1;
		return 1;
	}
	return 0;
}

int main(){
	int m;
	scanf("%d", &m);
	while(m--){
		scanf("%d", &n);
		int sum = 0, count = 0;
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				scanf("%d", &maps[i][j]);
		int t = 1;		
		for(int i = 1; i <= n; i++)
			for(int j = i; j <= n; j++){
				if(i != j){
					e[t].u = i;
					e[t].v = j;
					e[t].w = maps[i][j];
					t++;
				}
			}
		for(int i = 1; i <= n; i++)
			point[i] = i;	
		sort(e, e + t, cmp);
		for(int i = 1; i <= t; i++){
			if(merge(e[i].u, e[i].v)){
				count++;
				sum = max(sum, e[i].w);
			}
			if(count == n - 1)
				break;
		}	
		printf("%d\n", sum);
	}		
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值