POJ-1287(裸的Krustra)
来源:互联网 发布:蒙古作文软件 编辑:程序博客网 时间:2024/06/06 02:44
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <map>using namespace std;int V,E;const int maxn = 10000+10;int father[maxn];void Init(){ for(int i = 0; i < maxn ;i++) { father[i] = i; }}int Find(int x){ return father[x] == x?x:father[x] = Find(father[x]);}bool same(int a,int b){ a = Find(a); b = Find(b); return a==b;}void Union(int a , int b){ a=Find(a); b=Find(b); if(a != b) father[a] = b;}struct Edge{ int from,to,cost;} es[maxn];bool cmp(const Edge &t1 , const Edge &t2){ return t1.cost < t2.cost;}int Krustra(){ sort(es , es+E , cmp); Init(); int sum = 0 ; for(int i = 0 ;i < E;i++) { Edge e = es[i]; if(!same(e.from,e.to)) { Union(e.from,e.to); sum += e.cost; } } return sum;}int main(){ while(cin >> V && V) { cin >> E; int from,to,cost; int pos = 0; for(int i = 0; i < E ; i++) { cin >> from >> to >> cost ; es[i].from = from; es[i].to= to; es[i].cost = cost; } cout << Krustra() << endl; } return 0;}
阅读全文
0 0
- POJ-1287(裸的Krustra)
- Poj 2031 Building a Space Station ( 最小生成树Krustra
- nysit 38 Krustra算法
- POJ 2387 裸的dijkstra
- POJ 1986 裸的LCA
- [博弈] poj 1067 #裸的Wythoff Game
- poj 3624 裸的01背包
- POJ 2976 裸的01分数规划
- POJ 1061 裸的扩展欧几里得
- POJ 1287
- poj 1287
- POJ 1287
- POJ 1287
- POJ 1287
- 2道裸的深度优先搜索, poj 1979, poj 2386
- 【POJ】【今日刷的POJ水题合集】【#1】
- poj 2533(裸的LIS,很少见的水题)
- 强大的POJ
- 【HNOI2016模拟4.4】Stage
- navicat连接oracle
- 【表单】illuminate/html与laravelcollective/html【原创】
- Java分层思想:Action层, Service层 ,modle层 和 Dao层的功能区分
- Introduction to Vectors answer
- POJ-1287(裸的Krustra)
- python 环境搭建。
- python学习(7)——面向对象编程
- CSS中清除浮动的方式
- JSON最佳实践
- (4.4.1)使用storyboard实现页面跳转,简单的数据传递
- 《剑指offer》笔记-第4章(1)
- I
- ThreadLocal源码简析