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;
    }
  • 相关阅读:
    JVM(随手笔记)
    linux常用操作(个人笔记)
    MySQL学习笔记(个人随手笔记)
    jquery对象和Dom对象的转化([0])
    函数防抖和函数节流
    数据持久化分析
    day.js处理相对时间
    外链资源403的处理
    前端实现图片预览的两种方式及使用
    监听器标准写法
  • 原文地址:https://www.cnblogs.com/WArobot/p/7050170.html
Copyright © 2011-2022 走看看