PKU1861Kruskal算法
2014-07-16 10:53
330 查看
原题http://poj.org/problem?id=1861
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <limits.h> #include <ctype.h> #include <math.h> #include <string.h> #include <string> #include <algorithm> #include <iostream> #include <set> #include <stack> #include <queue> #include <vector> #include <deque> using namespace std; #define N 1005 int father ; struct node{ int x; int y; int len; }map[15111]; int m,n; struct node1{ int xx; int yy; }map1[15111]; bool cmp(node a,node b){ return a.len < b.len; } int find(int x){ int r = x; while(father[r] != r){ r = father[r]; } int i=x,k; while(i != r){ k = father[r]; father[i] = r; i = k; } return r; } int main(){ int i; while(~scanf("%d%d",&n,&m)){ for(i=0;i<m;i++){ scanf("%d%d%d",&map[i].x,&map[i].y,&map[i].len); } sort(map,map+m,cmp); memset(father,0,sizeof(father)); for(i=1;i<=n;i++){ father[i] = i; } int ans=0; int max = -1000; int count = 0; for(i=0;i<m;i++){ int f1 = find(father[map[i].x]); int f2 = find(father[map[i].y]); if(f1 != f2){ father[f1] = f2; ans+=map[i].len; if(map[i].len > max){ max = map[i].len; } map1[count].xx = map[i].x; map1[count].yy = map[i].y; count++; } } printf("%d\n",max); printf("%d\n",count); for(i=0;i<count;i++){ printf("%d %d\n",map1[i].xx,map1[i].yy); } } return 0; }
相关文章推荐
- PKU1287裸的Kruskal算法低空飘过
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
- pku 1797利用kruskal算法的过程
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- POJ 1861 Network (模版kruskal算法)
- Kruskal算法解决POJ 1861
- Network POJ 1861 ZOJ 1542 Kruskal算法 提问方式有点变化而已
- POJ 1861 Network (Kruskal算法)
- PKU 1861 最小生成树之KK算法
- poj1861最小生成树(并查集)-kruskal算法
- PKU1611-2485-1861使用并查集实现Kruskar算法求最小生成树。
- Pku acm 1861 NetWork 数据结构题目解题报告(二) ----最小生成树:prim算法&amp;amp;Kruskal算法
- POJ 1861 NetWork (Kruskal算法)
- pku 1861 Network(并查集实现Kruskal)
- POJ1861-Kruskal算法
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- Stirling公式(pku1423)
- uva1395 Kruskal算法求最小生成树
- tju的队伍在pku比赛中~~~
- PKU 2406:Power Strings 【KMP】