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

    奶牛贝茜被雇去建设N(2≤N≤1000)个牛棚间的互联网.她已经勘探出M(1≤M≤
20000)条可建的线路,每条线路连接两个牛棚,而且会苞费 C(1≤C≤100000).农夫约翰吝啬得很,他希望建设费用最少甚至他都不想给贝茜工钱. 贝茜得知工钱要告吹,决定报复.她打算选择建一些线路,把 所有牛棚连接在一起,让约翰花费最大.但是她不能造出环来,这样约翰就会发现.

Input

  第1行:N,M.
  第2到M+1行:三个整数,表示一条可能线路的两个端点和费用.
 

Output

 
    最大的花费.如果不能建成合理的线路,就输出-1

Sample Input

5 8
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

42

连接4和5,2和5,2和3,1和3,花费17+8+10+7=42

HINT

Source

【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)的更多相关文章

  1. BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    题目 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 53  Solve ...

  2. bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 -- 最大生成树

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MB Description     奶牛贝 ...

  3. BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(最大生成树)

    这很明显就是最大生成树= = CODE: #include<cstdio>#include<iostream>#include<algorithm>#include ...

  4. bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复【最大生成树】

    裸的最大生成树,注意判不连通情况 #include<iostream> #include<cstdio> #include<algorithm> using nam ...

  5. 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 69  Solved:  ...

  6. BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 43  Solved:  ...

  7. BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description      ...

  8. Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 218  Solved: ...

  9. BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )

    因为是棵树 , 所以直接 dfs 就好了... ---------------------------------------------------------------------------- ...

随机推荐

  1. 说说C#之父——安德斯·海尔斯伯格

    安德斯·海尔斯伯格(Anders Hejlsberg,1960.12~),丹麦人,Turbo Pascal编译器的主要作者,Delphi和.NET之父! 看到照片的那一刹那儿,我就觉得帅爆了,53岁的 ...

  2. poj 1274 The Perfact Stall

    click here ~~ ***The Perfect Stall*** Description Farmer John completed his new barn just last week, ...

  3. C++ STL中允许重复key的multimap

    在实际的项目中可能会碰到key重复的情况,正常的MAP类型是不允许重复的key,所以就要使用multimap了,multimap的使用和map基本类似,可以无缝对接 #include <map& ...

  4. Python多线程问题的资料查找与汇总

    Python多线程问题的资料查找与汇总 声明: 1)本报告由博客园bitpeach撰写,版权所有,免费转载,请注明出处,并请勿作商业用途. 2)若本文档内有侵权文字或图片等内容,请联系作者bitpea ...

  5. highcharts 坐标轴 数值 格式化

    以Y轴为示例: yAxis: { min: 0, gridLineColor: '#ececee', gridLineWidth: 1, lineColor: '#ececee', lineWidth ...

  6. eclipse 编译JAVA 项目导入的WEB项目 无法编译问题

    右击你的项目 选择properties  ---->java Build Path--->Default output folder新建一个classes目录就好了 watermark/2 ...

  7. python selenium --一些常用方法

    · text  获取该元素的文本 · submit  提交表单 · get_attribute  获得属性值 text   用于获取元素的文本信息 下面把百度首页底部的声明打印输出 #coding=u ...

  8. 改动文件后缀的C语言实现

    ,其他配置项保持一致.         step 3: 在"Old2New"目录下新建名为"update.bat"的批处理文件,该文件的内容为: ChangeS ...

  9. linux 安装 登录 centos7

    常用资源下载 r.aminglinux.com centos7.aminglinux.com http://www.apelearn.com/study_v2/ 认识linux Debian Slac ...

  10. DS18B20 crc 算法

    http://blog.csdn.net/pengrui18/article/details/24740973 https://www.maximintegrated.com/cn/app-notes ...