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 就好了... ---------------------------------------------------------------------------- ...
随机推荐
- 基于最简单的FFmpeg包封过程:视频和音频分配器启动(demuxer-simple)
===================================================== 基于最简单的FFmpeg封装工艺的系列文章上市: 最简单的基于FFmpeg的封装格式处理:视 ...
- webservice主流框架Axis、Axis2、XFire、CXF的比较
http://ws.apache.org/axis/ http://axis.apache.org/axis2/java/core/ http://xfire.codehaus.org/ http:/ ...
- 1.1. chromium源代码分析 - chromiumframe - 介绍
本人能力有效,面对chromium庞大的代码就头大.还是先由前辈的chromiumFrame入手. 1. chromeFrame概貌 chromiumFrame是前辈的心血之作,以最小化的方式抽出ch ...
- CDH 无法查看history log
1.配置(core-site.xml) <property> <name>hadoop.http.staticuser.user</name> <valu ...
- mysql读写分离
严格意义上讲,MySQL 读.写分离确实存在上述情况,这是由Master-Slave 异步复制存在延迟所导致的,且Master binlog的写入为多线程,而Slave同步的sql_thread为单线 ...
- 目前网络上大部分的网站都是由ASP或PHP开发,并且java平台的软件购买成本不适合中小企业客户,一般适用于银行、国家安全等行业领域
目前网络上大部分的网站都是由ASP或PHP开发,并且java平台的软件购买成本不适合中小企业客户,一般适用于银行.国家安全等行业领域. 要求建设开发大型复杂的网站,但仅有一个idea,不能够提供网站详 ...
- C++头文件的包含顺序研究
一.<Google C++ 编程风格指南>里的观点 公司在推行编码规范,领导提议基本上使用<Google C++ 编程风格指南>.其中<Google C++ 编程风格指南 ...
- Spring 面试复习
1 singleton 和 prototype singleton作用域:当把一个Bean定义设置为singleton作用域是,Spring IoC容器中只会存在一个共享的Bean实例,并且所有对 ...
- Java-线程间通信
Java-线程间通信 一 线程通讯 就是多个线程操作同一个资源,可是操作的动作不同 二 停止线程: 控制住run的循环就能够控制线程结束 当线程处于冻结状态,就不会读取标记,线程就不会结束 inter ...
- [置顶] js综合应用:表格的四则运算
在做调查问卷的过程中,遇到一个表格的统计问题,算是需要些js方面的综合知识,所以记录下来. 在上次完成了基本的求和的基础上,添加了基本的加减乘除四则运算. 基本需求简化后如下: 对应的htm了为: & ...