POJ
来源:互联网 发布:java内部类实例化 编辑:程序博客网 时间:2024/06/05 10:16
#include<stdio.h> //Kruskal算法 #include<algorithm>using namespace std;struct node{int e,s,w;}; node rode[10010];struct bigger{ bool operator()( const node &a1, const node &a2 ) { return a1.w < a2.w;}};int f[10010];int getf( int n ){ // 为并查集的内容 if( f[n]==n ) return n;else f[n]=getf(f[n]);return f[n];}int Uion( int v, int u ){ // 连接两个结点 联合 int fv,fu;fv=getf(v);fu=getf(u);if( fv==fu ) return 0;else{ f[fu]=fv; return 1;} }int kurall(int n, int m){int i,cn=0;for( i=1; i<=n; ++i ) f[i]=i;for( i=0; i<m; ++i ){if( Uion(rode[i].s,rode[i].e) ) cn++;if( cn == n-1 )// n-1 条边构成最小生成树 {return rode[i].w; // 输出最小生成树中最长的一条边 }}} int main(){int n,m,i; while( ~scanf("%d%d",&n,&m)){for( i=0; i<m; ++i ) scanf("%d%d%d",&rode[i].s,&rode[i].e,&rode[i].w);sort(rode,rode+m,bigger());// 根据路径的长度来排序 int ans=kurall(n,m); //Kruskal算法 printf("%d\n",ans); }return 0;}
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- MD5加密
- 《Objective-C编程全解》 读书笔记 第十七章 实例:简单图像视图
- Android 代码混淆
- HTTP in iOS你看我就够
- Burpsuite与sqlmap结合进行sql注入渗透测试
- POJ
- 深入解析MySQL分区(Partition)功能
- 网络流问题:最大流及其算法
- Mysql 索引机制
- 三种adapter的区别和用法
- 四大组件之Broadcast简介
- 读书笔记-抽象工厂模式
- 这些悬浮动效的常见玩法你都知道吗?
- 463. Island Perimeter的C++解法