poj 1861 network
来源:互联网 发布:自己皮肤类型知乎 编辑:程序博客网 时间:2024/04/28 06:20
点击打开链接poj 1861
思路:最小生成树+并查集+kruskal
分析:
1 模板题,只要按照kruskal的思路即可。
2 题目要求输出的是最小生成树中最大边的最小值,以及多少条边和边的点
代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;#define MAXN 15010int n , m;int num[MAXN];int father[MAXN];int rank[MAXN];struct Edge{ int x; int y; int value;}e[MAXN];bool cmp(Edge e1 , Edge e2){ return e1.value < e2.value;}void init_Set(){ for(int i = 0 ; i <= n ; i++){ father[i] = i; rank[i] = 0; }}int find_Set(int x){ if(father[x] != x) father[x] = find_Set(father[x]); return father[x];}void union_Set(int x, int y){ if(rank[x] > rank[y]) father[y] = x; else{ if(rank[x] == rank[y]) rank[y]++; father[x] = y; }}void kruskal(){ init_Set(); sort(e , e+m , cmp); int cnt = 0; for(int i = 0 ; i < m ;i++){ int root_x = find_Set(e[i].x); int root_y = find_Set(e[i].y); if(root_x != root_y){ union_Set(root_x , root_y); num[cnt++] = i; } } printf("%d\n%d\n" , e[num[cnt-1]].value , cnt); for(int i = 0 ; i < cnt ; i++) printf("%d %d\n" , e[num[i]].x , e[num[i]].y);}int main(){ while(scanf("%d%d" , &n , &m) != EOF){ for(int i = 0 ; i < m ; i++) scanf("%d%d%d" , &e[i].x , &e[i].y , &e[i].value); kruskal(); } return 0;}
- POJ 1861 Network
- poj 1861 Network
- POJ 1861 Network
- poj 1861 network
- poj 1861 network
- poj 1861 network
- POJ 1861 Network(Kruskal)
- poj 1861 Network MST
- POJ 1861 Network
- POJ 1861 Network
- POJ 1861 Network
- poj 1861 Network
- POJ-1861-Network
- POJ 1861 Network
- POJ 1861 Network(Kruskal)
- POJ 1861 Network
- poj 1861 Network
- POJ:1861 Network
- STL之vector的使用
- 解决 ant Junit testcase failed 自动检测类名
- POJ 1269 Intersecting Lines
- [hibernate]hibernate 随记
- 安装SOFTICE后电脑蓝屏解决方法
- poj 1861 network
- 客户端用https连接服务器证书问题
- 八皇后问题
- 在jquery下如何使用event对象(jquery插件——提示框)
- LPC3250的nor flash地址线接法
- 关于cache buffers chain latch
- linux 向应用层提供分钟级时区接口方案
- 完善:HTML5表单新特征简介与举例
- 欣博阅G10 开机老是停止于开机界面