POJ
来源:互联网 发布:plc网络名称 编辑:程序博客网 时间:2024/06/16 14:02
虽说是复习,但还是要贴上,文章+1
#include <stdio.h>#include <string.h>#include <iostream>#include<algorithm>#include <vector>#include <queue>#include <string>#include <math.h>#include <stdlib.h>using namespace std;#define INF 0x3f3f3f3f#define mem(arr,a) memset(arr,a,sizeof(arr))#define N 5000+5#define pow(a) ((a)*(a))struct edge{ int u, v, w;};int par[N];edge es[N];int sum;int n, m;bool cmp(edge a, edge b){ return a.w < b.w;}void init(){ for (int i = 1; i <= n; i++)par[i] = i;}int find(int x){ if (x == par[x])return x; return par[x] = find(par[x]);}bool same(int a, int b){ a = find(a); b = find(b); if (a==b)return true; return false;}void unite(int a, int b){ a = find(a); b = find(b); par[a] = b;}int kruskal(){ sort(es+1, es + m+1, cmp); for (int i = 1; i <= m; i++){ edge e = es[i]; if (!same(e.u, e.v)){ unite(e.u, e.v); sum += e.w; } } return sum;}int main(){ while (~scanf("%d", &n)){ if (n == 0)break; scanf("%d", &m); init(); sum = 0; for (int i = 1; i <= m; i++){ scanf("%d%d%d", &es[i].u, &es[i].v, &es[i].w); } printf("%d\n", kruskal()); }}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- leetcode115. Distinct Subsequences
- 内连接 外连接(left join/ right join)
- 【业界】基于腾讯Angel的LDA*入选VLDB,超越微软LightLDA
- Ubuntu16.04下MongoDB的安装和使用
- bzoj 2321: [BeiJing2011集训]星器 乱搞
- POJ
- 剑指offer——29.最小的k个数
- 随便写写
- Executor框架简介
- Java
- 演讲实录|马晓宇:When TiDB Meets Spark
- js中json的使用
- CSS-同一个li下,图片和文字一起,如何使得全部垂直居中
- Oracle数据库--序列/索引/触发器(一.序列)