POJ
来源:互联网 发布:淘宝千牛店铺名怎么改 编辑:程序博客网 时间:2024/06/08 13:29
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int fa[1000];struct edge{ int come, to, cost;}d[3000];bool cmp(edge p1, edge p2){ return p1.cost < p2.cost;}int getf(int x){ if(x == fa[x]) return x; return fa[x] = getf(fa[x]);}void init(){ for(int i = 0; i < 1000; i++) fa[i] = i;}void Union(int x, int y){ x = getf(x); y = getf(y); if(x == y) return; fa[x] = y;}bool same(int x, int y){ if(getf(x) == getf(y)) return 0; return 1;}int main(){ int m, n; while(scanf("%d%d", &n, &m) == 2) { int sum = 0; init(); for(int i = 0; i < m; i++) cin >> d[i].come >> d[i].to >> d[i].cost; sort(d, d + m, cmp); for(int i = 0; i < m; i++) { if(same(d[i].come, d[i].to)) { Union(d[i].come, d[i].to); sum += d[i].cost; } } cout << sum << endl; } return 0;}
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- stm32工程需要哪些文件
- 13年山东省赛——The number of steps
- jQuery检查DOM元素是否绑定某些事件
- strspn()函数
- TLD算法
- POJ
- 重温HTML
- tensorflow34《TensorFlow实战》笔记-06-02 TensorFlow实现VGGNet code
- java getParameter方法
- 44. Wildcard Matching
- Linux安装redis时报gcc:未找到命令错误
- C++求“四位水仙花数”
- 背包六问
- 【Java基础知识】IO流 详解