BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
题目
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 53 Solved: 37
[Submit][Status]
Description
Input
Output
Sample Input
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17
Sample Output
连接4和5,2和5,2和3,1和3,花费17+8+10+7=42
题解
这道题目就是最大生成树!-1的情况就是无法组成一棵树的情况。
代码
/*Author:WNJXYK*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int Maxn=;
int father[Maxn+];
inline void initFather(){
for (int i=;i<=Maxn;i++) father[i]=i;
} inline int getFather(int x){
return father[x]=father[x]==x?x:getFather(father[x]);
} inline void mergeFather(int x,int y){
int lx=getFather(x),ly=getFather(y);
if (lx<ly){
father[ly]=lx;
}else{
father[lx]=ly;
}
} struct Edge{
int x,y;
int w;
Edge(){}
Edge(int a,int b,int c){
x=a;
y=b;
w=c;
}
}; const int Maxm=;
Edge e[Maxm+]; bool cmp(Edge a,Edge b){
if (a.w>b.w) return true;
return false;
} int cnt,ans; int n,m;
int main(){
scanf("%d%d",&n,&m);
initFather();
for (int i=;i<=m;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
e[i]=Edge(x,y,z);
}
sort(e+,e+m+,cmp);
cnt=n;
for (int i=;i<=m;i++){
int x=e[i].x,y=e[i].y,w=e[i].w;
if (getFather(x)!=getFather(y)){
mergeFather(x,y);
cnt--;
ans+=w;
}
if (cnt==) break;
}
if (cnt!=){
printf("-1\n");
return ;
}
printf("%d\n",ans);
return ;
}
BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复的更多相关文章
- bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 -- 最大生成树
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MB Description 奶牛贝 ...
- BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(最大生成树)
这很明显就是最大生成树= = CODE: #include<cstdio>#include<iostream>#include<algorithm>#include ...
- bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复【最大生成树】
裸的最大生成树,注意判不连通情况 #include<iostream> #include<cstdio> #include<algorithm> using nam ...
- 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 69 Solved: ...
- 【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=3390 .. #include <cstdio> #include <cstring ...
- BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 43 Solved: ...
- BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班
题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MB Description ...
- Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 218 Solved: ...
- BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )
因为是棵树 , 所以直接 dfs 就好了... ---------------------------------------------------------------------------- ...
随机推荐
- jquery表格可编辑修改表格里面的值,点击td变input无刷新更新表格
td点击后变为input可以输入,更新数据,无刷新更新 演示 XML/HTML Code <table border="0" cellpadding="0" ...
- 联想S720/S720i通刷刷机包 Vibe V1.0
ROM介绍 基于官方最新S116底包制作,保证足够的稳定性. 增加VIBE元素,看起来更加大气.美观. 首次增加VIBE元素,720i执行起来无压力,720可能会有点卡.自行酌情刷入. 有bug请文明 ...
- 移动互联与大数据之美-逐浪CMS2 X1.1发布
北京时间2013年7月1日: 领先的CMS研发软件厂商--上海逐浪CMS软件科技有限公司发布其年中重要更新,并以Zoomla!逐浪CMS2 X1.1为版本号向全球用户投递新版软件. 此次更新包括: 1 ...
- 关于iOS8上本地通知接收不到的问题
在iOS8系统开发使用本地通知时,会出现如下的相关提示语: 1 Attempting to schedule a local notification2 with an alert but haven ...
- 【微信公众号】WeixinJSBridge.call('closeWindow')无效
公众号上面使用iframe嵌套子页面,然而子页面无法使用WeixinJSBridge.call('closeWindow') 这时候必须在函数前面加上parent.,比如parent.WeixinJS ...
- 移动端纯原生JS不依赖ajax后台服务器实现省市县三级联动
最近好多天没有更新文章,是因为公司的项目忙的不行.今天有点时间,就突然想起在移动端项目中遇到三级联动的问题,网上查了很多资料,都是依赖各种插件,或者晦涩难于理解.于是,自己决定写一个出来. 当然,没有 ...
- Android应用开发基础篇(13)-----GestureDetector(手势识别)
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/05/2381025.html 一.概述 GestureDetector是一个用于识别手势的类,这 ...
- 【Java】 实现一个简单文件浏览器(1)
学习Java的Swing的时候写的一个超简单文件浏览器 效果如图: 项目结构: 这里面主要用了两个控件,JTree和JTable 下面先说下左侧的文件树如何实现: 首先是FileTree类,继承于JT ...
- BZOJ 1189: [HNOI2007]紧急疏散evacuate( BFS + 二分答案 + 匈牙利 )
我们可以BFS出每个出口到每个人的最短距离, 然后二分答案, 假设当前答案为m, 把一个出口拆成m个表示m个时间, 点u到出口v的距离为d, 那么u->v的[d, m]所有点连边, 然后跑匈牙利 ...
- codility上的练习(5)
codility出了lesson 5了. (1) 合法括号序列,包括( [ { ) ] }这6种字符的字符串,长度N在[0..200000]范围内,为其是否合法. 要求时间复杂度O(N),空间复杂度O ...