BZOJ 3390 Bad Cowtractors牛的报复
2016-04-25 22:47
399 查看
HAHAHA奶牛题。。。。最大生成树。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxv 2050 #define maxe 40050 using namespace std; struct edge { int u,v,w; }e[maxe]; int n,m,father[maxv],ans=0; bool cmp(edge x,edge y) { return x.w>y.w; } int getfather(int x) { if (x!=father[x]) father[x]=getfather(father[x]); return father[x]; } void kruskal() { for (int i=1;i<=n;i++) father[i]=i; sort(e+1,e+m+1,cmp); for (int i=1;i<=m;i++) { int u=e[i].u,v=e[i].v,w=e[i].w; int f1=getfather(u),f2=getfather(v); if (f1!=f2) {father[f1]=f2;ans+=w;} } } bool check() { for (int i=2;i<=n;i++) { if (getfather(i)!=getfather(i-1)) return false; } return true; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); kruskal(); if (check()) printf("%d\n",ans); else printf("-1\n"); return 0; }
相关文章推荐
- 第9课 计算器界面代码重构
- 欧拉筛
- 【剑指offer-Java版】11数值的整数次方
- git创建版本库
- hdu-1086 You can Solve a Geometry Problem too(线段是否相交)
- c++创建对象的两种方式
- 51nod1010 只包含因子2 3 5的数
- 数据结构学习笔记05图(最小生成树 Prim Kruskal)
- 微信支付:curl出错,错误码:60
- spring框架学习笔记(七)
- 排序算法--面试总结分析
- 一种高效无锁内存队列的实现
- (转)大型网站架构系列:负载均衡详解(4)
- linux升级OpenSSL
- 构建之法阅读笔记4
- ASP.NET中IsPostBack详解
- for循环输出三角形
- SDAU 练习三 1006 最大节点之和问题
- (转)大型网站架构系列:负载均衡详解(3)
- RxJS入门(2)---Observable的介绍