POJ 1861
来源:互联网 发布:单片机开发系统 编辑:程序博客网 时间:2024/03/28 16:46
/*
总结一下kruskal算法。
1.新建图G,G中拥有原图中相同的节点,但没有边2.将原图中所有的边按权值从小到大排序3.从权值最小的边开始,如果这条边连接的两个节点于图G中不在同一个连通分量中(使用了并查集),则添加这条边到图G中 同时可以记录路径record4.重复3,直至图G中所有的节点都在同一个连通分量中收获,思路很清晰,需要很小心的写
*/
#include<iostream>#include<algorithm>#include<cstdio>using namespace std;class kruskal{public: int a,b,value;}edge[15005],record[15005];int n,m;int f[10000];//并查集int s[10000];//根的数目int Max; //最大边int pos; //记录遍历的边数目int sum; //最小生成树的值bool cmp(kruskal a,kruskal b){ return a.value < b.value;}int Find(int x){ if(f[x] == x) return x; else f[x] = Find(f[x]); return f[x];}int Union(int x,int y){ int a = Find(x); int b = Find(y); if(a == b) return 0; else if(s[a] <= s[b]){ //使用了优化并查集 f[a] = b; s[b] += s[a]; } else { f[b] = a; s[a] += s[b]; } return 1;}int main(){ // freopen("in.txt","r",stdin); cin>>n>>m; for(int i = 1;i <= m; i++) cin>>edge[i].a>>edge[i].b>>edge[i].value; for(int i = 1;i <= n; i++) { f[i] = i; s[i] = 1; } sum = 0;//初始化 pos = 1; Max = 0; sort(edge+1,edge+m+1,cmp); for(int i = 1;i <= m; i++){ if(Union(edge[i].a,edge[i].b) == 1) { if(edge[i].value > Max) Max = edge[i].value; record[pos].a = edge[i].a; record[pos].b = edge[i].b; pos++; } if(pos - 1 == n - 1) break; } cout<<Max<<endl<<pos-1<<endl; for(int i = 1;i <= pos-1; i++){ cout<<record[i].a<<" "<<record[i].b<<endl; } return 0;}
0 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
- CodeForces Canada Cup 2016 A、B水题 _(:з」∠)_
- linux下用gdb调试c程序
- ecshop数据库表9(ecs_ad_position)广告位表
- 如何让div 里的img正好被覆盖
- Java 设计模式 行为型模式 + 案例
- POJ 1861
- javascript中0.1 + 0.2 != 0.3?
- IE8以下浏览器不支持document.getElementsByClassName() 访问节点;怎么解决?---封装自己的类名
- 数据段、代码段、堆栈段、BSS段的区别
- LeetCode 363 Max Sum of Rectangle No Larger Than K (二分)
- 【Hadoop入门学习系列之二】HDFS架构和编程
- 欢迎使用CSDN-markdown编辑器
- mysql5.6GTID手动跳过复制错误
- 使用div和js脚本弹出对话框