【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=3390
。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=20005;
int p[N], n, m, ans;
struct ED { int x, y, w; }e[N];
bool cmp(const ED &a, const ED &b) { return a.w>b.w; }
const int ifind(const int &x) { return x==p[x]?x:p[x]=ifind(p[x]); } int main() {
read(n); read(m);
for1(i, 1, m) read(e[i].x), read(e[i].y), read(e[i].w);
sort(e+1, e+1+m, cmp);
int tot=0;
for1(i, 1, n) p[i]=i;
for1(i, 1, m) {
int fx=ifind(e[i].x), fy=ifind(e[i].y);
if(fx!=fy) {
p[fx]=fy;
ans+=e[i].w;
++tot;
}
}
if(tot<n-1) puts("-1");
else print(ans);
return 0;
}
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
HINT
Source
【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)的更多相关文章
- 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牛的报复 -- 最大生成树
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: ...
- 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 就好了... ---------------------------------------------------------------------------- ...
随机推荐
- Solidworks如何添加齿轮 运动副
建立下面的齿轮装配关系(注意装配体不要先拖入齿轮,因为我们第一个齿轮是要手动让他转的,所以不能固定) 分别在两个齿轮中绘制两条直线,一个从圆心到齿顶圆,一个从圆心到齿根圆(在零件中绘图完成之后要退 ...
- Python描写叙述符(descriptor)解密
Python中包括了很多内建的语言特性,它们使得代码简洁且易于理解.这些特性包括列表/集合/字典推导式,属性(property).以及装饰器(decorator).对于大部分特性来说,这些" ...
- 排序(2)---------简单插入排序(C语言实现)
插入排序(Insertion Sort)的算法描写叙述是一种简单直观的排序算法. 它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到对应位置并插入.插入排序在实现上,通常 ...
- 使用ipmitool 命令添加IPMI 界面的SMTP邮件服务器地址
目前要通过ipmitool工具在IPMI的界面上添加邮件服务器地址,该脚本如下 SMTP.sh #!/bin/bash ipmitool raw 0x32 0x78 0x01 0x01 0x00 0x ...
- iOS UITableView获取特定位置的cell
代码地址如下:http://www.demodashi.com/demo/13307.html 一.tableView双级联动 以上两种效果比较类似,实现的关键在于都是需要获得在滑动过程中滑动到tab ...
- excel 的几个函数使用
=IF(表1[[#此行],[state]]="修改", IF( ISBLANK( VLOOKUP( ...
- Redis Key过期通知
概述 键空间通知使得客户端可以通过订阅频道或模式, 来接收那些以某种方式改动了 Redis 数据集的事件.如Redis数据库中键的过期事件也是通过订阅功能实现.本文主要基于Azure PaaS Red ...
- java的学习之路01
[原创 - 尚学堂科技 - 马士兵老师] JAVA自学之路 一:学会选择 [转载请注明出处:http://www.bjsxt.com/zixue/zixuezhilu_1.html] 为了就业,不少同 ...
- vs2017搭建自己的nuget服务器
准备环境 vs2017 第一步 创建一个新的asp.net 空网站 .net框架使用4.6以上版本 (或者在第二部中使用低版本的nuget server) 第二步 打开nuget包管理器 搜索nu ...
- SUSE11SP3--安装svn
SUSE上安装新版本的subversion是一件很麻烦的事情,涉及到的依赖太多,包括需要更新部分工具的版本. 为了以较小的代价安装subversion,我决定在SUSE11下安装 subversion ...