#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include<vector>
#include<string>
using namespace std;
int N,M;
struct graph{
int u,v;
int w;
}p[10010];
bool cmp(graph m,graph n){
return m.w<n.w;
}
int father[2020];
int Find(int x){
if(father[x]==x)
return x;
return father[x]=Find(father[x]);
}
bool Union(int x,int y){
x=Find(x);
y=Find(y);
if(x==y)
return true;//祖先相同,是同一个集合;
if(x>y)
father[x]=y;
if(x<y)
father[y]=x;
return false;
}
int main(){
while(cin>>N>>M){
for(int i=0;i<M;i++){
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);
}
for(int i=0;i<N;i++)
father[i]=i;
sort(p,p+M,cmp);
int count=0;
int k;
for(int i=0;i<M;i++){
if(!Union(p[i].u,p[i].v))
{
count++;
k=i;
}
if(count==N-1)
break;
}
cout<<p[k].w<<endl;
}
}
poj 2395 Kruskal
最新推荐文章于 2020-03-28 20:57:06 发布
