bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复【最大生成树】
裸的最大生成树,注意判不连通情况
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=20005;
int n,m,f[N],con;
long long ans;
struct qwe
{
int u,v,w;
}a[N];
bool cmp(const qwe &a,const qwe &b)
{
return a.w>b.w;
}
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
int zhao(int x)
{
return x==f[x]?x:f[x]=zhao(f[x]);
}
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++)
a[i].u=read(),a[i].v=read(),a[i].w=read();
sort(a+1,a+1+m,cmp);
for(int i=1;i<=n;i++)
f[i]=i;
for(int i=1;i<=m&&con<n-1;i++)
{
int fu=zhao(a[i].u),fv=zhao(a[i].v);
if(fu!=fv)
{
con++,ans+=a[i].w;
f[fu]=fv;
}
}
printf("%lld\n",con==n-1?ans:-1ll);
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牛的报复(最大生成树)
这很明显就是最大生成树= = CODE: #include<cstdio>#include<iostream>#include<algorithm>#include ...
- 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 就好了... ---------------------------------------------------------------------------- ...
随机推荐
- 字符串String对象构造方法的创建和直接赋值的区别
/* * 通过构造方法创建的字符串对象和直接赋值方式创建的字符串对象有什么区别呢? * 区别是:通过构造方法创建的字符串对象是在堆内存.通过赋值创建的字符串对象是在方法区的常量池 * * == * 基 ...
- numpy——基础数组与计算
In [1]: import numpy as np In [11]: # 创建数组 a = np.array([1,2,3,4,5]) In [12]: a Out[12]: array([1, 2 ...
- 全文检索(AB-1)-相关领域
信息检索 认知科学 管理科学
- 1001. A+B Format (20) (%0nd)
1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...
- j简单的分类实现-K近邻
dataSetSize=dataSet.shape[0] voteIlabel=labels[sortedDistIndicies[i]] ...
- POJ3669 Meteor Shower
http://poj.org/problem?id=3669 类似于迷宫的一道题 但是并没有 给出迷宫具体什么样 但是题目已说在坐标轴的第一象限 然后障碍就是 流星雨所砸范围 安全位置:永远不会发生危 ...
- msp430入门学习21
msp430的USART的SPI模式 msp430入门学习
- [bzoj4278][ONTAK2015]Tasowanie_后缀数组_贪心
Tasowanie bzoj-4278 ONTAK-2015 题目大意:给定两个字符串,求两个字符串二路归并之后生成的字典序最小的字符串是什么. 注释:$1\le len_1,len_2\le 2\c ...
- Java :面向对象
Java :面向对象 直面Java 第001期 什么是面向过程 面向过程是以函数为中心,要解决一个问题,需要把问题分解为一个个的步骤,然后定义一系列的流程,用特定的输入经过函数的处理,最终输出特定的结 ...
- JVM定位程序假死或cpu占用高的线程
linux系统: 参考:https://blog.csdn.net/qq_40197576/article/details/80287515 1>使用top命令查看占用cpu进程情况,得到jav ...