BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(最大生成树)
这很明显就是最大生成树= =
CODE:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 1010
#define maxm 20010
struct edges{
int x,y,z;
}edge[maxm];
bool cmp(edges a,edges b){return (a.z>b.z);}
int f[maxn];
int fin(int x){if (f[x]!=x) f[x]=fin(f[x]);return f[x];}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) f[i]=i;
for (int i=1;i<=m;i++) scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z);
sort(edge+1,edge+1+m,cmp);
int sum=0,ans=0;
for (int i=1;i<=m;i++){
int x=fin(edge[i].x),y=fin(edge[i].y);
if (x!=y) { f[x]=y;sum++;ans+=edge[i].z;if (sum==n-1) {printf("%d",ans);return 0;}}
}
printf("-1\n");
return 0;
}
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牛的报复
题目 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 53 Solve ...
- 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 就好了... ---------------------------------------------------------------------------- ...
随机推荐
- myeclipse的常用快捷键
创建一个类 Alt+Shift+N,C,输入Demo,回车 创建类属性 按3次下方向键,回车,输入String name;,回车 创建构造器 Alt+Shift+S,O,回车 创建getter/set ...
- Swift迁入第三方库时的版本错误解决
我的swift的项目用的是swift 2.3的版本,但是用CocoaPods迁入一个第三方:ObjectMapper后,编译会出现这样一个问题: Use Legacy Swift Language V ...
- Object修改链表
以前学习过链表的时候由于类型的接收不同,每次要重写链表 下面修改可用链表 class Link{ private class Node{ private Object data ; private N ...
- UVa 908 - Re-connecting Computer Sites
题目大意:有n个网站,由m条线路相连,每条线路都有一定的花费,找出连接所有线路的最小花费. 最小生成树问题(Minimal Spanning Tree, MST),使用Kruskal算法解决. #in ...
- HTML5+MUI+HBuilder 之初探情人
07,08年那会儿正当Java火爆,C/C++仍是广泛运用的一门语言的时候,所以 我的大学都献给了C/C++和Java.当诺基亚的倒闭成为按键机时代衰落的标志时,移动APP的开发也如破堤之洪,爆炸式的 ...
- Angular - - $http请求服务
$http $http是Angular的一个核心服务,它有利于浏览器通过XMLHttpRequest 对象或者 JSONP和远程HTTP服务器交互. $HTTP API 是基于 $q服务暴露的defe ...
- Eclipse tomcat插件
1. 下载 http://www.eclipsetotale.com/tomcatPlugin.html 2. 解压 解压到Eclipse_Home/dropins 3. 重启Eclipse
- .net 设置版本号信息
1.AssemblyInfo.cs [assembly: AssemblyVersion("1.3.170116")] [assembly: AssemblyFileVersion ...
- Delphi 中的常用事件
OnActive 焦点称到窗体或控件时发生 OnClick 鼠标单击事件 OnDbClick 鼠标双击事件 OnClose和OnCloseQuery 当关闭一个窗体时就会响应OnClose和OnClo ...
- origin从图中获得数据
有Origin的原图,即利用Origin的"copy page"功能直接拷贝到文字处理软件的数据图,对于这种图,双击用Origin打开后,双击要导出数据的权限,打开"pl ...