POJ 1861
来源:互联网 发布:阮维希淘宝店铺 编辑:程序博客网 时间:2024/04/27 13:02
找出最小生成树中最长的分支链路,以及输出没对节点间的距离。克鲁斯卡尔算法比较合适,因为克鲁斯卡尔算法首先按照没对节点间的距离排序,在找到最小生成树的时候最后加入生成树的那对节点间的的距离就是生成树中最长的。
#include<iostream>
#include<algorithm>
using namespace std;
int pre[1001];
int n,m;
struct node{
int x,y,w;
};
node out[15001];
bool cmp(const node&a,const node&b){
return a.w<b.w;
}
int find(int x){
while(x!=pre[x])
x=pre[x];
return x;
}
int main(){
int i,f1,f2,k=0,cnt=0,ch[1001];
cin>>n>>m;
for(i=0;i<m;i++)
cin>>out[i].x>>out[i].y>>out[i].w;
for(i=1;i<=n;i++)
pre[i]=i;
sort(out,out+m,cmp);
for(i=0;i<m;i++){
f1=find(out[i].x);
f2=find(out[i].y);
if(f1!=f2){
cnt++;
pre[f1]=f2;
ch[k++]=i;
if(cnt==n-1)
break;
}
}
printf("%d\n%d\n",out[i].w,cnt);
for(i=0;i<k;i++)
printf("%d %d\n",out[ch[i]].x,out[ch[i]].y);
return 0;
}
- poj 1861
- POJ 1861
- poj 1861
- POJ 1861
- poj 1861
- poj 1861
- poj-1861
- POJ 1861
- poj 1861
- POJ 1861
- POJ 1861
- POJ 1861 Networks
- POJ 1861 Network
- poj 1861 Network
- POJ 1861 Network
- poj 1861 network
- poj 1861 network
- poj 1861 network
- POJ 1679
- Java有包名的类不能引用默认包中的类?用反射机制解决!
- POJ 1797
- POJ 1847
- java.util.regex.PatternSyntaxException
- POJ 1861
- POJ 1961
- POJ 1975
- POJ 1989
- POJ 2251
- POJ 2253
- Chrome 浏览器快捷键
- Shell 脚本 Debug 方法
- JAVA 泛型的好处