bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 -- 最大生成树
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
Time Limit: 1 Sec Memory Limit: 128 MB
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
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define N 1010
#define M 20010
inline int rd()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct qaz{int a,b;ll v;}e[M];
bool cmp(qaz a,qaz b){return a.v>b.v;}
int cnt,fa[N];
int findf(int x){return x==fa[x]?x:fa[x]=findf(fa[x]);}
int n,m,x,y;
ll ans;
int main()
{
n=rd();m=rd();
int i,j,f1,f2;
for(i=;i<=n;i++) fa[i]=i;
for(i=;i<=m;i++){e[i].a=rd();e[i].b=rd();e[i].v=rd();}
sort(e+,e+m+,cmp);
for(int i=;i<=m;i++)
{
x=findf(e[i].a);y=findf(e[i].b);
if(x!=y)
{
ans+=e[i].v;
fa[y]=x;
}
}
bool f=;x=findf();
for(int i=;i<=n;i++) if(findf(i)!=x){f=;break;}
f?puts("-1"):printf("%lld\n",ans);
return ;
}
bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 -- 最大生成树的更多相关文章
- 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 ...
- 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 就好了... ---------------------------------------------------------------------------- ...
随机推荐
- python小爬虫练手
一个人无聊,写了个小爬虫爬取不可描述图片.... 代码太短,就暂时先往这里贴一下做备份吧. 注:这是很严肃的技术研究,当然爬下来的图片我会带着批判性的眼光审查一遍的.... :) #! /usr/ ...
- Chrome浏览器任意修改网页内容
在Chrome浏览器按F12,打开开发者工具,切换到console选项卡: 在下面的输入行输入下面的命令回车: document.body.contentEditable="true&quo ...
- 再续 virtualenv II
为什么搭建虚拟环境 搭建 Python 虚拟环境,可以方便的解决不同项目中对类库的依赖问题.当然,也可以方便地Python2,Python3 共存.避免包的混乱和版本的冲突.为每个程序单独创建虚拟环境 ...
- python slots源码分析
上次总结Python3的字典实现后的某一天,突然开窍Python的__slots__的实现应该也是类似,于是翻了翻CPython的源码,果然如此! 关于在自定义类里面添加__slots__的效果,网上 ...
- 用sar进行CPU利用率的分析
07:40:17 PM CPU %user %nice %system %iowait %steal %idle07:40:19 PM a ...
- 在Ubuntu上安装Redis MySQL MongoDB memcached Nginx
1.安装Redis sudo apt-get install redis-server 2.安装MySQL sudo apt-get install mysql-server 3.安装MongoDB ...
- C# 解决窗体闪烁
C# 解决窗体闪烁 在Windows窗体上造成“闪烁”的窗体上有很多控制.造成这种闪烁的原因有两个: 1.当控件需要被绘制时,Windows发送一个控件两个消息.第一个(WM_ERASEBKGND)导 ...
- Otto:EventBus
Otto:EventBus 2014年6月20日 星期五 15:14 参考: http://www.mythroad.net/?p=4151 Otto 是Android系统的一个Event Bus模式 ...
- kali&BT安装好之后无法上网(包括Wifi)或者无法获得内网IP解决方法
大家都知道,要想进行内网渗透攻击,你必须要在那个内网里.但是大家在Vmware里安装kali的时候,大多数用户为了方便,未选择桥接模式,而是选择了使用与本机共享的IP网络当然,这样能上网,但是你的虚拟 ...
- bzoj 4518
4518 思路: 斜率优化: 代码: #include <cstdio> #include <cstring> #include <iostream> #inclu ...