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 就好了... ---------------------------------------------------------------------------- ...
随机推荐
- 一个基于ATMEGA128的直流电机抱死程序(转)
源:一个基于ATMEGA128的直流电机抱死程序 先说一下我的硬件情况:一块ATMEGA128实验板:一个带编码器的80:1的变速电机,编码器的输出端连接到单片机的PD4和PD5引脚:一块电机驱动电路 ...
- UVa 900 - Brick Wall Patterns
题目大意:用1*2的砖头建n*2的墙,问有多少种不同的砖头排列方式?与斐波那契序列相似. #include <cstdio> #define MAXN 60 #define N 50 un ...
- C# 计算文件的HASH
/// <summary> /// 提供用于计算指定文件哈希值的方法 /// <example>例如计算文件的MD5值: /// <code> /// String ...
- Windows显示不了磁盘
ps:当我的磁盘插电脑上却显示不了磁盘信息.终于最后将我的200G资料,搞没了,也不能恢复了..刚刚磁盘显示了,为了让更多人能不走弯路,我结合了网络能让磁盘显示的几个经验,也便大家方便参考... ...
- ios页面跳转
reference:http://blog.csdn.net/engandend/article/details/11706323 目前,就我所学到的内容,页面跳转有三种方法 一.直接推到下一个页面 ...
- mysql5.7创建账户并授权
CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass'; GRANT ALL ON db1.* TO 'jeffrey'@'localhost ...
- 如何在Windows Server 2016启用或关闭Internet Explorer增强的安全配置
一般我们安装完服务器后,开启 Internet Explorer 会发现无法上网或者上网内容被屏蔽掉了 问题的发生原因 在 Windows Server 2016 通常扮演重要的服务器角色,不应该用来 ...
- iOS 之 线程和进程
进程是系统调度单位,拥有自己的资源 线程是CPU调度的基本单位 进程的同步机制: 原子操作.信号量机制.自旋锁.分布式系统
- 《JAVA语言程序设计》上课笔记
教学目标:1.使学生了解JAVA课程的性质.定位.作用:为什么要学习JAVA?让学生知道如何学好JAVA: 教学内容: 一. 问几个问题 1. 你们到这里来干什么 ...
- 使用原始XML资源——使用原始XML文件
下面为示例程序添加一个原始的XML文件,将该XML文件放到/res/xml目录下,该XML文件的内容很简单.XML资源的内容如下. 程序清单: \res\xml\books.xml文件 <?x ...