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

Silver

【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)相关推荐

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

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

  2. bzoj3390[Usaco2004 Dec]Bad Cowtractors牛的报复*

    bzoj3390[Usaco2004 Dec]Bad Cowtractors牛的报复 题意: 最大生成树. 题解: 最大生成树. 代码: 1 #include <cstdio> 2 #in ...

  3. bzoj 3388: [Usaco2004 Dec]Cow Ski Area雪场缆车(Tarjan)

    3388: [Usaco2004 Dec]Cow Ski Area雪场缆车 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 108  Solved: 4 ...

  4. poj 2376 bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)

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

  5. bzoj 3391: [Usaco2004 Dec]Tree Cutting网络破坏

    3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 296  Solved: 2 ...

  6. BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏(搜索)

    这道直接遍历一遍求出每个点的子节点数目就行了= = CODE: #include<cstdio> #include<iostream> #include<algorith ...

  7. 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树

    [BZOJ3387][Usaco2004 Dec]Fence Obstacle Course栅栏行动 Description 约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[ ...

  8. bzoj3391[Usaco2004 Dec]Tree Cutting网络破坏*

    bzoj3391[Usaco2004 Dec]Tree Cutting网络破坏 题意: 给一棵树,问去掉哪个点后可以使剩下的每个子树大小都小于等于节点总数的一半.n≤10000. 题解: dfs的时候 ...

  9. bzoj 3383: [Usaco2004 Open]Cave Cows 4 洞穴里的牛之四(set+BFS)

    3383: [Usaco2004 Open]Cave Cows 4 洞穴里的牛之四 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 44  Solved ...

最新文章

  1. 电子系统动态范围的理解
  2. C语言将字符串转换成对应的数字(十进制、十六进制)【转】
  3. 头文件的包含以及命名空间的引入尽量写在cpp里
  4. boost::range_reference相关的测试程序
  5. 数据库连接池_DataSource_数据源(简单介绍C3P0和Druid)
  6. CCNP-防火墙-上一代防火墙-下一代防火墙
  7. c++ cdi+示例_C ++“或”关键字示例
  8. jeecg怎么样好用吗?
  9. C/C++常见标准头文件前格式探讨
  10. java 对象equals_浅谈Java对象的equals方法
  11. python 网络请求框架_python 框架
  12. IT群侠传第一回内外兼修
  13. 轻松构建并发送ICMP数据包
  14. dell笔记本外接显示器_戴尔笔记本怎么连接外接显示器
  15. bw_mem 内存_TCP:内存不足—考虑调整tcp_mem
  16. 【windows——高效工作】写作篇
  17. Shiro实现logout操作
  18. 搭建网校平台的具体方法
  19. Windows安装Mysql并配置(安装版)
  20. latex怎么给图片命名_[Latex] Beamer 入门

热门文章

  1. 密码找回功能可能存在的问题
  2. [译]Stack View 自定义间隙
  3. ceph对象存储折腾记
  4. Ubuntu关闭ipv6
  5. UltraEdit正则表达式介绍及实例
  6. Java 数据类型:对人的描述
  7. php获取分辨率赋值,php如何获取客户端分辨率?实例教程
  8. Xara Designer Pro中文版
  9. 修改Eclipse中项目在Apache Tomcat中的部署路径
  10. 2016 版 Laravel 系列入门教程(一)