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 就好了... ---------------------------------------------------------------------------- ...
随机推荐
- Android实现左右滑动指引效果
本文介绍Android中实现左右滑动的指引效果. 关于左右滑动效果,我在以前的一篇博文中提到过,有兴趣的朋友可以查看:http://www.cnblogs.com/hanyonglu/archive/ ...
- 微信jsapi支付的坑
1.显示 redirect_uri 参数错误 因为手机支付是需要微信授权的,所以检查配置授权域名地址是否正确填写(登陆微信公众平台,地址:https://mp.weixin.qq.com/cgi-b ...
- c/c++ double的数字 转成字符串后 可以有效的避免精度要求不高的数
char n[100]; sprintf(n,"%lf",db);
- redis在java项目中的使用
在上一篇文章中已经讲了redis的spring配置,这篇将会描述redis在java项目中的使用. redis存储形式都是key-value(键值对),按照存储的内容分为两种,一种是存简单数据,即数字 ...
- Android应用开发基础篇(8)-----SurfaceView
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/25/2368069.html 一.概述 SurfaceView也是一个用来画图的部件,不过由于它 ...
- 驴吃胡萝卜问题——牛客/FEI
一个商人骑一头驴要穿越1000公里长的沙漠,去卖3000根胡萝卜.已知驴一次性可驮1000根胡萝卜,但每走1公里又要吃掉1根胡萝卜.问:商人最多可卖出多少胡萝卜? 一个商人骑一头驴要穿越1000公里长 ...
- 利用fitnesse实现api接口自动化测试
上午在园子里乱逛,看了不少小伙伴们分享的接口测试方面的知识,仔细想想,我做接口测试也有几个年头了,大家所叙述到的一些经验或多或少,我也曾遇到过,突然意识到知识的点滴积累是多么的重要,我记得我最早接触接 ...
- JS 寻找孩子并打印路径
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- discuz默认模板文件结构详解-模板文件夹介绍
| — template — default 系统内置风格模板(默认风格)| — template — default – discuz_style_default.xml 风格安装文件,可用 ...
- 转: angularjs学习总结(~~很详细的教程)
1 前言 前端技术的发展是如此之快,各种优秀技术.优秀框架的出现简直让人目不暇接,紧跟时代潮流,学习掌握新知识自然是不敢怠慢. AngularJS是google在维护,其在国外已经十分火热,可是国内的 ...