**链接:****传送门 **

题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1

思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了


/*************************************************************************
> File Name: poj2377.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月19日 星期一 18时38分35秒
************************************************************************/ #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int MAX_N = 1010;
const int MAX_M = 20010;
struct edge{
int from , to ,cost;
}E[MAX_M]; int n , m;
int par[MAX_N]; void init_union_find_set() { for(int i = 0 ; i < MAX_N ; i++) par[i] = i; }
int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
bool same(int x,int y) { return find(x) == find(y); }
void union_set(int x,int y) { x = find(x); y = find(y); if(x!=y) par[y] = x; } bool cmp(edge a,edge b){
return a.cost < b.cost;
} int Kruskal(){
init_union_find_set();
sort(E,E+m,cmp);
int ret = 0;
for(int i = 0 ; i < m ; i++){
if( !same(E[i].from , E[i].to)){
union_set(E[i].from,E[i].to);
ret += E[i].cost;
}
}
int cnt = 0;
for(int i = 1 ; i <= n ; i++){ if( par[i] == i ) cnt++; }
if( cnt > 1 ) ret = 1;
return ret;
}
int main(){
int from , to , cost;
while(~scanf("%d%d",&n,&m)){
for(int i = 0 ; i < m ; i++){
scanf("%d%d%d",&from,&to,&cost);
E[i].from = from , E[i].to = to , E[i].cost = -cost;
}
int ret = Kruskal();
printf("%d\n",-ret);
}
return 0;
}

POJ 2377 Bad Cowtractors( 最小生成树转化 )的更多相关文章

  1. poj 2377 Bad Cowtractors

    题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...

  2. poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)

    http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...

  3. poj 2377 Bad Cowtractors(最大生成树!)

    Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...

  4. poj 2377 Bad Cowtractors (最大生成树prim)

    Bad Cowtractors Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  5. POJ - 2377 Bad Cowtractors Kru最大生成树

    Bad Cowtractors Bessie has been hired to build a cheap internet network among Farmer John's N (2 < ...

  6. POJ 2377 Bad Cowtractors (Kruskal)

    题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-1. 思路:将边权降序再Kruskal,再检查一下是否只有一棵树即可,即根节点只有一个 #include <cstdio> ...

  7. POJ:2377-Bad Cowtractors

    传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...

  8. POJ 2485 Highways【最小生成树最大权——简单模板】

    链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  9. MST:Bad Cowtractors(POJ 2377)

    坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...

随机推荐

  1. mongodb--分片架构【待填的坑】

    首先有一个问题没有搞懂:什么是自动分片?用脚本吗? 一: 多机方式中的另一种方式[分片 => sharding] 分片的对象的谁? 对一个[集合 => 表]进行拆分,把一个大数据拆分成多个 ...

  2. 洛谷——P1616 疯狂的采药

    https://www.luogu.org/problem/show?pid=1616#sub 题目背景 此题为NOIP2005普及组第三题的疯狂版. 题目描述 LiYuxiang是个天资聪颖的孩子, ...

  3. kettle 递归循环

    var i = new Number(parent_job.getVariable(; parent_job.setVariable("i",i); true;

  4. [Drupal]主题教程

    drupal6和drupal7的主题开发有很大不同,本指南包含了这些不同 drupal7的默认主题是Bartik,6的是Garland drupal的主题系统是如何工作的 这部分内容主要讲述的是dru ...

  5. HDU 3537

    翻硬币游戏,纯.. 注意要判重 #include <iostream> #include <cstdio> #include <cstring> #include ...

  6. 关于SharePoint讨论板的一些知识(2)--视图中的栏目

    关于SharePoint讨论板的一些知识(2)--视图中的栏目         新建讨论后,默认显示四个栏目:主题.创建者.答复和上次更新时间.         从功能区的当前视图能够看出这是默认的主 ...

  7. 【有奖征资源,分享有内涵】贡献你的CSDN博文和下载资源,不断更新中

    我们收集了CSDN热心博主的博文 和相关下载资源.这些可爱博主上传了免积分的CSDN资源,并贡献了相关的用法,改进策略,进行了翔实的分析.感谢博主的贡献.并期待有很多其它这种好人! 我们特推出活动&q ...

  8. 怎样使用Eclipse PDT调试PHP程序

    本文主要介绍的是怎样用eclipse pdt调试PHP 代码. 1. 下载eclipse.从官网上找就能够了,并确认当前系统中有java环境,即jdk和jre. 2. 安装pdt了,採用的是在线安装. ...

  9. pod install fatal: unable to access 'https://chromium.googlesource.com/webm/libwebp/'

    1. 前往文件夹 /用户/aki/.cocoapods/repos/master/1/9/2/libwebp 2.编辑libwebp.podspec.json 将source git 改为 https ...

  10. Binding Enum to ComboBox

    1.添加MarkupExtension public class EnumToSourceExtension : MarkupExtension { private Type _type; publi ...