3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

Time Limit: 1 Sec  Memory Limit: 128 MB

Description

    奶牛贝茜被雇去建设N(2≤N≤1000)个牛棚间的互联网.她已经勘探出M(1≤M≤
20000)条可建的线路,每条线路连接两个牛棚,而且会苞费C(1≤C≤100000).农夫约翰吝啬得很,他希望建设费用最少甚至他都不想给贝茜工钱. 贝茜得知工钱要告吹,决定报复.她打算选择建一些线路,把所有牛棚连接在一起,让约翰花费最大.但是她不能造出环来,这样约翰就会发现.

Input

  第1行:N,M.
  第2到M+1行:三个整数,表示一条可能线路的两个端点和费用.
 

Output

 
    最大的花费.如果不能建成合理的线路,就输出-1

Sample Input

5 8
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

42

连接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牛的报复 -- 最大生成树的更多相关文章

  1. BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    题目 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 53  Solve ...

  2. BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(最大生成树)

    这很明显就是最大生成树= = CODE: #include<cstdio>#include<iostream>#include<algorithm>#include ...

  3. bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复【最大生成树】

    裸的最大生成树,注意判不连通情况 #include<iostream> #include<cstdio> #include<algorithm> using nam ...

  4. 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 69  Solved:  ...

  5. 【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3390 .. #include <cstdio> #include <cstring ...

  6. BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 43  Solved:  ...

  7. BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description      ...

  8. Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 218  Solved: ...

  9. BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )

    因为是棵树 , 所以直接 dfs 就好了... ---------------------------------------------------------------------------- ...

随机推荐

  1. JS 判断是否是微信浏览器 webview

    原理很简单,就是判断 ua 中是否有字段 “micromessenger" 代码如下: function isWechat () { var ua = window.navigator.us ...

  2. 统计oracle表中字段的个数

    select count(column_name) from user_tab_columns where table_name='emp' dba权限对应的视图是dba_tab_columns 和a ...

  3. weblogic 开启注意问题

    1.关闭防火墙 service iptables stop chkconfig iptables off 2.weblogic unable to get file lock问题 我的解决办法是ps ...

  4. python并发编程之Queue线程、进程、协程通信(五)

    单线程.多线程之间.进程之间.协程之间很多时候需要协同完成工作,这个时候它们需要进行通讯.或者说为了解耦,普遍采用Queue,生产消费模式. 系列文章 python并发编程之threading线程(一 ...

  5. MySQL Warning: Using a password on the command line interface can be insecure.解决办法

    转自 http://www.cnblogs.com/sunss/p/6256706.html  被一个小朋友问到,直接公布答案: If your MySQL client/server version ...

  6. Groovy 与 DSL

    一:DSL 概念 指的是用于一个特定领域的语言(功能领域.业务领域).在这个给出的概念中有 3个重点: 只用于一个特定领域,而非所有通用领域,比如 Java / C++就是用于通用领域,而不可被称为 ...

  7. 深度学习方法(十三):卷积神经网络结构变化——可变形卷积网络deformable convolutional networks

    上一篇我们介绍了:深度学习方法(十二):卷积神经网络结构变化--Spatial Transformer Networks,STN创造性地在CNN结构中装入了一个可学习的仿射变换,目的是增加CNN的旋转 ...

  8. Binary Tree Zigzag Level Order Traversal——关于广度优先的经典面试题

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  9. MySQL增删改数据

    1.增加数据 ,); /*插入所有字段.一定依次按顺序插入--字符串与日期需要加单引号,数字不需要,各个字段之间用逗号分隔*//*注意不能少或者多字段值*/ ,) /*按字段名插入数据,中间用逗号隔开 ...

  10. 5 Linux网络编程基础API

    5.1   socket地址API 大端字节序(网络序):高位在低址,低位在高址 小端字节序(主机序):低位在低址,高位在高址 判断,利用联合的特性: #include <iostream> ...