zoukankan      html  css  js  c++  java
  • 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
    ",ret);
    	}
    	return 0;
    }
  • 相关阅读:
    17-canvas绘制扇形
    16-canvas绘制圆弧
    15-canvas渐变色
    14-canvas绘制柱状图
    13-绘制矩形的简写方式
    12-es6类的方式封装折线图
    11-canvas绘制折线图
    10-canva绘制数据点
    jenkins 环境部署 (yum安装方式)
    BerkeleyDB安装
  • 原文地址:https://www.cnblogs.com/WArobot/p/7050170.html
Copyright © 2011-2022 走看看