【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
Silver
【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)相关推荐
- BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(最大生成树)
这很明显就是最大生成树= = CODE: #include<cstdio> #include<iostream> #include<algorithm> #incl ...
- bzoj3390[Usaco2004 Dec]Bad Cowtractors牛的报复*
bzoj3390[Usaco2004 Dec]Bad Cowtractors牛的报复 题意: 最大生成树. 题解: 最大生成树. 代码: 1 #include <cstdio> 2 #in ...
- 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 ...
- poj 2376 bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 462 Solved ...
- bzoj 3391: [Usaco2004 Dec]Tree Cutting网络破坏
3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 296 Solved: 2 ...
- BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏(搜索)
这道直接遍历一遍求出每个点的子节点数目就行了= = CODE: #include<cstdio> #include<iostream> #include<algorith ...
- 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树
[BZOJ3387][Usaco2004 Dec]Fence Obstacle Course栅栏行动 Description 约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[ ...
- bzoj3391[Usaco2004 Dec]Tree Cutting网络破坏*
bzoj3391[Usaco2004 Dec]Tree Cutting网络破坏 题意: 给一棵树,问去掉哪个点后可以使剩下的每个子树大小都小于等于节点总数的一半.n≤10000. 题解: dfs的时候 ...
- 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 ...
最新文章
- 电子系统动态范围的理解
- C语言将字符串转换成对应的数字(十进制、十六进制)【转】
- 头文件的包含以及命名空间的引入尽量写在cpp里
- boost::range_reference相关的测试程序
- 数据库连接池_DataSource_数据源(简单介绍C3P0和Druid)
- CCNP-防火墙-上一代防火墙-下一代防火墙
- c++ cdi+示例_C ++“或”关键字示例
- jeecg怎么样好用吗?
- C/C++常见标准头文件前格式探讨
- java 对象equals_浅谈Java对象的equals方法
- python 网络请求框架_python 框架
- IT群侠传第一回内外兼修
- 轻松构建并发送ICMP数据包
- dell笔记本外接显示器_戴尔笔记本怎么连接外接显示器
- bw_mem 内存_TCP:内存不足—考虑调整tcp_mem
- 【windows——高效工作】写作篇
- Shiro实现logout操作
- 搭建网校平台的具体方法
- Windows安装Mysql并配置(安装版)
- latex怎么给图片命名_[Latex] Beamer 入门