ZOJ 2966 Build The Electric System【最小生成树】
来源:互联网 发布:finale2011汉化软件 编辑:程序博客网 时间:2024/05/22 07:46
//2632030 2011-08-18 16:57:56 Accepted 2966 C++ 0 1652 ylwh@Unknown#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <algorithm>using namespace std;#define N 501struct edge{int x, y, cost;}e[N * N / 2];int root[N];int find_root(int x){int temp = x;while(temp != root[temp])temp = root[temp];root[x] = temp;return temp;}int cmp(struct edge a, struct edge b){return a.cost < b.cost;}int main(){int t, n, E, i, j, a, b, x, y, cnt, ans;scanf("%d", &t);while(t--){cnt = -1;ans = 0;scanf("%d%d", &n, &E);for(i=0; i<n; i++)root[i] = i;for(i=0; i<E; i++){scanf("%d%d%d", &x, &y, &j);if(j){e[++cnt].x = x;e[ cnt].y = y;e[ cnt].cost = j;}else{a = find_root(x);b = find_root(y);if(a > b)root[a] = b;else if(a < b)root[b] = a;}}sort(e, e + cnt + 1, cmp);for(i=0; i<=cnt; i++){a = find_root(e[i].x);b = find_root(e[i].y);if(a != b){ans += e[i].cost;if(a > b)root[a] = b;elseroot[b] = a;}}printf("%d\n", ans);} return 0;}
- ZOJ 2966 Build The Electric System【最小生成树】
- 【最小生成树】ZOJ 2966 Build The Electric System
- ZOJ 2966 Build The Electric System 最小生成树
- ZOJ 2966 Build The Electric System【最小生成树】
- ZOJ 2966 Build The Electric System(最小生成树)
- ZOJ 2966 Build The Electric System 最小生成树 Kruscal && Prim
- zoj 2966 Build The Electric System【最小生成树 Kruskal && prim】
- ZOJ-2966-Build The Electric System【5th浙江省赛】【最小生成树】
- ZOJ 2966 Build The Electric System (kruskal)
- ZOJ 2966 Build The Electric System MST
- ZOJ 2966 Build The Electric System
- zoj 2966 Build The Electric System
- ZOJ 2966Build The Electric System
- zoj 2966 Build The Electric System ( prim || kruskal~ )
- zoj 2966 Build The Electric System(MST)
- zoj 2966 Build The Electric System(并查集)
- ZOJ 1965 Build The Electric System
- Build The Electric System
- Gossip
- 2011 阿里巴巴 1003
- 开发QQ桌球瞄准器(2):创建项目添加主窗体
- oracle 函数数值型字符判断
- Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的
- ZOJ 2966 Build The Electric System【最小生成树】
- XML解析器(TinyXML)的使用
- c#调用数据库的WebService
- Android入门第十五篇之ActivityGroup + GridView 实现Tab分页标签
- 〖中华人民共和国国家标准 GB 11643-1999〗 身份证升位
- Matlab和C联合编程
- 关于线程的sleep函数
- [PHP]身份证校验位的计算/ISO 7064:1983.MOD 11-2 算法
- Hadoop集群中增加新节点