POJ 2395 Out of Hay( 最小生成树 )


**链接:****传送门 **

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


/*************************************************************************
    > File Name: poj2395.cpp
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年06月19日 星期一 19时00分25秒
 ************************************************************************/

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int MAX_N = 2010;
const int MAX_M = 10010;
struct edge{
	int from , to ,cost;
}E[MAX_M];

int n , m , par[MAX_N];

void init_union_find_set()		{	for(int i = 0 ; i <= n ; i++) par[i] = i;	}
int  find(int x)				{	return x == par[x] ? x : par[x] = find(par[x]);	}
bool same(int x,int y)			{	return find(x) == find(y);	}
void union_set(int x,int y)		{	x = find(x); y = find(y); if(x!=y) par[y] = x;	}

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

int Kruskal(){
	init_union_find_set();
	sort(E,E+m,cmp);
	int ret = 0;
	for(int i = 0 ; i < m ; i++){
		if( !same(E[i].from , E[i].to) ){
			union_set(E[i].from , E[i].to);
			ret = max( ret , E[i].cost );
		}
	}
	return ret;
}

int main(){
	int from , to , cost;
	while(~scanf("%d%d",&n,&m)){
		for(int i = 0 ; i < m ; i++){
			scanf("%d%d%d",&E[i].from,&E[i].to,&E[i].cost);
		}
		int ret = Kruskal();
		printf("%d\n",ret);
	}
	return 0;
}
posted @ 2017-06-19 19:18  ojnQ  阅读(279)  评论(0编辑  收藏  举报