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 就好了... ---------------------------------------------------------------------------- ...
随机推荐
- c++栈管理库TCMalloc、jeMalloc
示例:http://blog.csdn.net/chosen0ne/article/details/9338591
- Javascript对象的声明
JavaScript 对象 对象由花括号分隔.在括号内部,对象的属性以名称和值对的形式 (name : value) 来定义.属性由逗号分隔: var person={firstname:" ...
- css3选择器的比较(三) -- 元素选择器 (+, ~)
元素选择器中w3cschool中有些翻译不太准确 比如 +:其实是与element1元素同级,位于element1元素之后的第一个element2元素 ~:其实是与element1元素同级,位于ele ...
- Problem B The Blocks Problem(vector的使用)
题目链接:Problem B 题意:有n块木块,编号为0~n-1,要求模拟以下4种操作(下面的a和b都是木块编号) 1. move a onto b: 把a和b上方的木块全部归位,然后把a摞在b上面. ...
- Structs
1.服务端的运行程序 2.Servlet的三个方法 init service:抽象方法 destroy 3.步骤 (1).在web.xml中 <servlet> <servlet-n ...
- servlet三种实现方式之一实现servlet接口
servlet有三种实现方式: 1.实现servlet接口 2.继承GenericServlet 3.通过继承HttpServlet开发servlet 第一种示例代码如下(已去掉包名): import ...
- 使用Project进行挣值分析
挣值分析(EV)技术是一种根据项目的成本基准来度量和分析项目绩效的方法.挣值分析的结果可以指出项目在成本和进度方面存在的偏差.EV分析结合了成本,进度,已完成工作等各种因素来衡量项目的绩效,并 ...
- 通往WinDbg的捷径
通往WinDbg的捷径(一) 原文:http://www.debuginfo.com/articles/easywindbg.html译者:arhat时间:2006年4月13日关键词:CDB WinD ...
- CERC 2013 Magical GCD
题目大意如下:给定一个序列,每个序列有值xi,现给定t个数列,对于每个长n的数列,求一段[l,r]使 [r-l+1]*gcd(l,r)最大,gcd(l,r)指的是该连续区间的最大公约数. 不难想到n^ ...
- poj 3335 Rotating Scoreboard - 半平面交
/* poj 3335 Rotating Scoreboard - 半平面交 点是顺时针给出的 */ #include <stdio.h> #include<math.h> c ...