POJ 2485 Highways(最小生成树,树的最大权值边)

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 28440 Accepted: 12962

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.

Source

POJ Contest,Author:Mathematica@ZSU


题意:

什么也不多说,题意对于英语不好的同学是障碍,我也是看了翻译才懂!

求的是 MST 的最大权值边。


键入数据:

村庄的数目 n

村庄 1 1的距离 ,村庄1 2 的距离 村庄 1 3的距离 ,......村庄 1 n 的距离

村庄 2 1的距离 ,村庄2 2 的距离 村庄 2 3的距离 ,......村庄 2 n 的距离

.................................

村庄 n 1的距离 ,村庄n 2 的距离 村庄 n 3的距离 ,......村庄 n n 的距离

微笑题目没懂,测试数据看了很久,最后运行错误!数组开小了!挂了很多次。。。。。


代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int MYDD=110300;

int set[MYDD];
void init(int x) {//还是有参数好一点
	for(int j=1; j<=x; j++)
		set[j]=j;
}

int find(int x) {
	int t,child=x;
	while(x!=set[x])
		x=set[x];
	while(child!=x) {
		t=set[child];// t 记录当前父节点
		set[child]=x;
		child=t;
	}
	return x;
}

bool combine(int x,int y) {//判断是否有同一个根节点
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy) {
		set[fx]=fy;
		return true;//不成环
	}
	return false;
}

struct EDGE {
	int u,v,w;
} edge[MYDD*8];
bool cmp(EDGE x,EDGE y) {
	return x.w<y.w;
}

int main() {
	bool vis[MYDD];//记录节点的访问状态
	int t;
	scanf("%d",&t);
	while(t--) {
		int n;
		scanf("%d",&n);
		init(n);
		int dd=1;
		for(int j=1; j<=n; j++) {
			for(int k=1; k<=n; k++) {
				edge[dd].u=j;
				edge[dd].v=k;
				scanf("%d",&edge[dd].w);
				dd++;
			}
		}

		sort(edge+1,edge+dd+1,cmp);

		int count=0;//记录选择的边数
		//		int ans=0;//记录生成树的权值
		int max=-1;//记录生成树边的最大权值
		for(int j=1; count<n-1; j++) {//循环的终止条件可以选择节点数目 -1
			if(combine(edge[j].u,edge[j].v)) {
				count++;
				//				ans+=edge[j].w;
				if(edge[j].w>max)
					max=edge[j].w;
			}
		}
		//		printf("%d\n",ans);
		printf("%d\n",max);
	}
	return 0;
}

后:GF来了。

**********************************


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值