ZOJ 1542 / POJ 1861 Network (kruskal )
2010-09-15 13:48
411 查看
又是一个MST得水题,受不了。
对于让最大边最小,显然可以用kruskal从最小边慢慢添加。
真的没有什么可以解释的了。。。。
对于让最大边最小,显然可以用kruskal从最小边慢慢添加。
真的没有什么可以解释的了。。。。
#include<stdio.h> #include<string.h> #include<stdlib.h> int p[1001]; int find(int i) { return i==p[i]?i:(p[i]=find(p[i]));} struct edge { int from,to; int w; } a[15001],t; int cmp( const void *a,const void *b) { struct edge aa = *(struct edge *)a; struct edge bb = *(struct edge *)b; if( aa.w > bb.w ) return 1; if( aa.w < bb.w ) return -1; return 0; } int main(void) { int n,m,max,count; int i,j,from,to; int jilu[1001]; while( scanf("%d%d",&n,&m) != EOF ) { for( i = 1; i <= n; i++ ) p[i] = i; for( i = 1; i <= m; i++ ) { scanf("%d%d%d",&t.from,&t.to,&t.w); a[i] = t; } qsort(a+1,m,sizeof( a[0] ) ,cmp); for( i = 1,count = 0,max=-1; i <= m; i++ ) { //printf("%d/n",a[i].w); from = find(a[i].from); to = find(a[i].to); if( from == to ) continue; p[from] = to; jilu[++count] = i; if( a[i].w > max ) max = a[i].w; } printf("%d/n%d/n",max,count); for( i = 1; i <= count; i++) { printf("%d %d/n",a[ jilu[i] ].from,a[ jilu[i] ].to); } } return 0; }
相关文章推荐
- zoj 1542 || poj 1861 Network(MST = = Kruskal~)
- network 最小生成树 Kruskal 算法 poj 1861 zoj 1542
- POJ 1861 && ZOJ 1542--Network 【最小生成树 && kruscal && 水题】
- zoj 1542 Network【kruskal】
- POJ 1861||ZOJ1542 kruskal求最小生成树(POJ秒杀啊)
- POJ 1861 & ZOJ 1542 Network(最小生成树之Krusal)
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- ZOJ 1542 poj 1861 Network(并查集+最小树)
- POJ 1861||ZOJ1542 kruskal求最小生成树(POJ秒杀啊)
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- ZOJ 1542 poj 1861 Network(并查集+最小树)
- POJ 1861/ZOJ 1542 Network
- 【最小生成树】 ZOJ 1542 Network
- ZOJ 2048 / POJ 2485 Highways ( kruskal )
- POJ 1861:Network(最小生成树&&kruskal)
- poj1861 Network(kruskal求最小生成树)
- ZOJ-1586(QS Network) Kruskal
- POJ 1861-Network(最小生成树-Kruskal)
- pku 1861 Network(并查集实现Kruskal)
- ZOJ 1372 / POJ 1287 Networking ( kruskal+prim )