BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 43 Solved: 29
[Submit][Status]
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
题解:
裸MST,呵呵。
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 20005
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
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;
}
int n,m,fa[maxn];
struct rec{int x,y,w;}a[maxn];
inline bool cmp(rec a,rec b)
{
return a.w>b.w;
}
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();m=read();
for1(i,m)a[i].x=read(),a[i].y=read(),a[i].w=read();
sort(a+,a+m+,cmp);
for1(i,n)fa[i]=i;
int j=,i,ans=;
for(i=;i<n;i++)
{
while(j<=m&&find(a[j].x)==find(a[j].y))j++;
if(j>m)break;
fa[find(a[j].x)]=find(a[j].y);
ans+=a[j].w;
j++;
}
printf("%d\n",i<n?-:ans);
return ;
}
BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复的更多相关文章
- 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牛的报复
题目 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 53 Solve ...
- 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牛的报复(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=3390 .. #include <cstdio> #include <cstring ...
- 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 ...
- 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树
[BZOJ3387][Usaco2004 Dec]Fence Obstacle Course栅栏行动 Description 约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[ ...
- Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 218 Solved: ...
- BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏
3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 47 Solved: 37[ ...
随机推荐
- Asp.Net读取服务器EXE文件的方法!(超简单实例)
Asp.Net读取服务器EXE文件的方法!(超简单实例) Process process = new Process(); process.StartInfo.FileName = "d:\ ...
- POJ1505:Copying Books(区间DP)
Description Before the invention of book-printing, it was very hard to make a copy of a book. All th ...
- Ubuntu上安装QQ2015
先不卖关子直接上图:Ubuntu 14.04.5 LTS Deb包下载地址: http://www.longene.org/download/WineQQ7.8-20151109-Longene.de ...
- jsp if else c标签 总结
JSTL标签使用方法 keyword:JSTL标签.<c:choose>.<c:forEach>.<c:forTokens>.<c:if>.<c: ...
- HibernateProxy异常处理 java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
这里使用google的Gson包做JSON转换,因为较早的1.4版本的FieldAttributes类中没有getDeclaringClass()这个方法,这个方法是获取field所属的类,在我的排除 ...
- 窗口 对话框 Pop Dialog 示例
两者的区别 PopupWindow和AlertDialog最大的区别: AlertDialog是非阻塞线程的,AlertDialog弹出的时候,后台可以做其他事情(也即弹出对话框后程序会继续向下执行) ...
- Wcf资料收集
1.简介 http://www.tuicool.com/articles/mqYB32 使用规范 http://blog.51cto.com/zt/219 2.教程系列 http://www.cnbl ...
- CSS3 3D转换
CSS3允许你使用3D转换来对元素进行格式化. 3D转换方法: rotateX() rotateY() 浏览器支持 属性 浏览器支持 transform IE10和Firefox支 ...
- ViewPager Indicator的使用方法
原文:http://my.oschina.net/u/1403288/blog/208402 项目源码:https://github.com/wangjing0311/ViewPagerIndicat ...
- PHP E-mail
PHP E-mail 注入 首先,请看上一章中的 PHP 代码: <html><body> <?phpif (isset($_REQUEST['email']))//if ...