POJ 2377 Bad Cowtractors( 最小生成树转化 )
**链接:****传送门 **
题意:给 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( 最小生成树转化 )的更多相关文章
- poj 2377 Bad Cowtractors
题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...
- poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)
http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...
- poj 2377 Bad Cowtractors(最大生成树!)
Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...
- poj 2377 Bad Cowtractors (最大生成树prim)
Bad Cowtractors Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- POJ - 2377 Bad Cowtractors Kru最大生成树
Bad Cowtractors Bessie has been hired to build a cheap internet network among Farmer John's N (2 < ...
- POJ 2377 Bad Cowtractors (Kruskal)
题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-1. 思路:将边权降序再Kruskal,再检查一下是否只有一棵树即可,即根节点只有一个 #include <cstdio> ...
- POJ:2377-Bad Cowtractors
传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- MST:Bad Cowtractors(POJ 2377)
坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...
随机推荐
- 洛谷——P1094 纪念品分组
https://www.luogu.org/problem/show?pid=1094#sub 题目描述 元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作.为使得参加晚会的同学所获得 的纪念品价 ...
- print puts p 用法
print puts p 用法 print "a\n" puts "a" p "a\n" 输出为 a a "a\n" p ...
- HDU 1788 Chinese remainder theorem again 中国剩余定理
题意: 给定n,AA 以下n个数m1,m2···mn 则有n条方程 res % m1 = m1-AA res % m2 = m2-AA 问res的最小值 直接上剩余定理,嘿嘿 #include< ...
- 36岁IT老人再次随笔——程序员的门槛其实并不高,但却是一个易学难精的行当——IT的快车很快,我常看到不少人摔落下去,但又有不少身手敏捷的人跳了上来 good
36岁的我,还在IT里面留恋着技术.我不是什么技术牛人,只是不愿离开.搞硬件的朋友对我说:“我以为你是搞硬件的,没想到你软件方面这么厉害?”,搞软件的朋友对我说:“我以为你只是搞软件的,没想到你硬件方 ...
- 怎样才是一个基本水平的java程序员?
怎样才是一个基本水平的java程序员? 熟悉常用的数据结构,包括数组,链表,树,哈希表等. 熟悉结构化编程和面向对象编程. 能够阅读UML设计图,根据UML语义进行编码 了解RDBMS和SQL的使用, ...
- poj 1321(DFS)
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...
- 29. Divide Two Integers[M]两数相除
题目 Given two integers dividend and divisor, divide two integers without using multiplication, divisi ...
- 从谷歌官网下载android 6.0源码、编译并刷入nexus 6p手机
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fuchaosz/article/details/52473660 1 前言 经过一周的奋战,终于从谷 ...
- Queue 与List、LinkedList与 ArrayList 区别
List 是一个接口,不能实例化,通过实例化ArrayList 或者LinkedList来调用:List list = new ArrayList(); |--List: 元素是有序的(怎么存的就怎么 ...
- C - Ilya and Sticks(贪心)
Problem description In the evening, after the contest Ilya was bored, and he really felt like maximi ...