UVa 11747 - Heavy Cycle Edges
来源:互联网 发布:cin c语言怎么用 编辑:程序博客网 时间:2024/05/29 13:49
题目:计算最小生成树的kruskal算法,有两种途径,方法一是我们常用的不断的把边加到目标集合上。
方法二是从图中不断的删边,最后剩下的就是所求集合。
分析:最小生成树。利用kruskal算法的方法一,求剩下的边就是删掉的边,即为所求。
注意:数组别开小了,特别是并查集的。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>using namespace std;typedef struct d_node{int point1;int point2;int weight;}enode;enode edge[25005];//union_setint sets[1001];int rank[1001];void set_inital( int a, int b ){for ( int i = a ; i <= b ; ++ i ) {rank[i] = 0;sets[i] = i;}}int set_find( int a ){if ( a != sets[a] )sets[a] = set_find( sets[a] );return sets[a];}void set_union( int a, int b ){if ( rank[a] < rank[b] )sets[a] = b;else {if ( rank[a] == rank[b] )rank[a] ++;sets[b] = a;}}//end_union_setint cmp_e( enode a, enode b ){return a.weight < b.weight;}int kruskal( int n, int m ){sort( edge, edge+m, cmp_e );set_inital( 0, n );int sum = 0;for ( int i = 0 ; i < m ; ++ i ) {int A = set_find( edge[i].point1 );int B = set_find( edge[i].point2 );if ( A != B ) set_union( A, B );else {if ( sum ++ ) printf(" ");printf("%d",edge[i].weight);}}return sum;}int main(){int n,m,a,b,c; while ( scanf("%d%d",&n,&m) && n+m ) {for ( int i = 0 ; i < m ; ++ i ) {scanf("%d%d%d",&a,&b,&c);edge[i].point1 = a;edge[i].point2 = b;edge[i].weight = c;}if ( !kruskal( n, m ) )printf("forest");printf("\n");}return 0;}
0 0
- UVa 11747 - Heavy Cycle Edges
- uva 11747 Heavy Cycle Edges
- UVA 11747 - Heavy Cycle Edges(MST)
- UVA 11747 Heavy Cycle Edges(MST)
- (beginer) 最小生成树 UVA 11747 Heavy Cycle Edges
- uva 11747 - Heavy Cycle Edges(生成树)
- UVa 11747 Heavy Cycle Edges(最小生成树)
- UVa11747 Heavy Cycle Edge (Kruskal)
- UVa 544 - Heavy Cargo
- Going in Cycle!! uva
- uva 544 - Heavy Cargo(生成树)
- Uva 11090 Going in Cycle!!
- Uva-11090-Going in Cycle!!
- UVA 11090 - Going in Cycle!!
- uva 11090 Going in Cycle!!
- uva 1561 - Cycle Game(推理)
- UVA - 11090-Going in Cycle!!
- UVA 11090 Going in Cycle!!
- IOS开发 textView输入文字时键盘切换为中文输入法后,第一个输入的字母变成英文
- javaEE api .chm
- 学习英语电台mms地址
- iOS软件更新
- java设计模式初学笔记(一)Iterator
- UVa 11747 - Heavy Cycle Edges
- android适配器
- John's trip
- 配置自己的vim-常用插件
- access "update语句的语法错误"解决
- 九度OJ 1015
- hdu 1325 Is It A Tree?
- 动态规划——最长公共子序列(LCS)&最长递增子序列(LIS)
- 电子商务商业模式实训及思考 第一问