51 nod 1212 基础MST
来源:互联网 发布:淘宝品牌调性分怎么查 编辑:程序博客网 时间:2024/05/18 00:37
N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树。
直接上代码:裸的最小生成树.
#include<iostream>#include<algorithm>#include<stdio.h>#include<cmath>#define max1 1005#define max2 50005using namespace std;struct edge{ int be,en,cost;};edge e[max2];int node[max1];int sum=0;int find(int x){ while(x!=node[x]) x=node[x]; return x;}bool cmp(edge a,edge b){ return a.cost<b.cost;}int main(){ int n,m; cin>>n>>m; for(int x=1;x<=n;x++) node[x]=x; for(int x=0;x<m;x++) scanf("%d%d%d",&e[x].be,&e[x].en,&e[x].cost); sort(e,e+m,cmp); for(int x=0;x<m;x++) { int bx=find(e[x].be); int by=find(e[x].en); if(bx!=by) { sum+=e[x].cost; node[bx]=by; } } cout<<sum;}
阅读全文
0 0
- 51 nod 1212 基础MST
- 51nod 1264 基础
- 51nod 1051【基础】
- 51nod 动态规划基础
- 51nod 基础题小结
- 51nod 基础题-1459
- 51nod--1298 (计算几何基础)
- 51nod 基础题(2)
- 51nod--基础题大数加法
- 51nod 1002 数塔取数问题(基础dp)
- 51nod 1021 石子归并(基础dp)
- 51 nod 动态规划基础篇 3
- 51nod 动态数组基础题 4
- 51nod 数塔取数问题 基础dp
- 51nod 动态规划基础篇 6
- 51 nod 动态规划 基础篇 1
- 51nod 动态规划 基础篇 2
- 51nod 基础题 1005 大数加法
- git常用命令
- jstl fmt标签笔记
- 华为机试——明明的随机数
- Linux常用命令清单
- 用多线程进行的序列快速排序
- 51 nod 1212 基础MST
- Service的两种启动方式
- 小船过河问题
- EdgeRouter ER-X 定时自动重启设置
- 谁“偷”走了SSD的容量?
- ”^“运算符重载实现幂指数运算
- 玲珑杯 1146(dp)
- java定时器和线程组
- 驱动框架1——什么是驱动框架?