最小生成树-kruskal

经典的最小生成树算法(基于并查集)
首先,要对所有边按权从小到大排序,
其次,要淘汰一些边,即使它们的权很小,但在生成树中它们是多余的(即构成环),
这里我们可以用并查集来解决。每选取一条边,我们就把这条边的两个顶点放进同一个连通分量
通过一个能够寻找连通分量根结点的函数find(v)来判断。如果find(v1)==find(v2),那么v1和v2
已经属于同一连通分量,那么v1与v2间的边就不能再添加到生成树中。第三,什么时候结束算法呢?

可以设置一个计数器,当已经添加了n-1条边时,生成树就构造好了。

1090. Highways

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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 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.

Output

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.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692
#include<iostream>
#include<algorithm>

using namespace std;

const int MAX=500;
int longest=0;
int arc[MAX][MAX];
//边集结构
struct edge{
	int u;
	int v;
	int w;
}E[130000]; 

bool cmp(edge a,edge b){
	return a.w<b.w;
}
int father[MAX];

//并查集
int find(int x){
	if(father[x]==x)
		return x;
	else{
		father[x]=find(father[x]);
		return father[x];
	}
}

void Kruskal(int road){
	//初始化并查集
	for(int i=0;i<MAX;i++)
		father[i]=i;
	longest=0;
	 for(int i=0;i<road;i++){
	 	int x=find(E[i].u);
	 	int y=find(E[i].v);
	 	if(x!=y){
	 		if(longest<E[i].w)	
	 			longest=E[i].w;
	 		father[x]=y;
	 	}
	 }
}

int main(){
	int t,city;
	cin>>t;
	while(t--){		
		cin>>city;
		for(int i=0;i<city;i++){
			for(int j=0;j<city;j++){
				cin>>arc[i][j];
			}
		}
		//转化为边集结构 
		int e=0;
		for(int i=0;i<city;i++){
			for(int j=i+1;j<city;j++){
				E[e].u=i;
				E[e].v=j;
				E[e].w=arc[i][j];
				e++;
			}
		}
		//排序 
		sort(E,E+e,cmp);
		Kruskal(e);
		cout<<longest<<endl;
		if(t)
			cout<<endl;	
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值