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算 ...
随机推荐
- 奇妙的go语言(基本的语法)
[ 声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 学习一门新的语言无非就是从主要的语法開始的.通过语法书来学习语言毕竟是很枯燥的,所以我们最好还 ...
- java5核心基础之泛型(3)-泛型作用于编译阶段-怎样将String对象传入Integer类型的泛型对象中?
泛型作用于编译阶段: 泛型是作用于编译阶段,在编译阶段控制类型,以确保在编写代码的时候仅仅能传入指定类型数据到泛型集合对象中去. 怎样验证呢,贴代码例如以下: package highBasic.ge ...
- 关于Mysql Enterprise Audit plugin的使用
正如之前看到的一篇文章,假设想要知道是谁登陆了你的数据库server,干了什么东西,那么你须要使用Mysql Enterprise Audit plugin. 以下介绍一下Mysql Enterpri ...
- [2-SAT] poj 3207 Ikki's Story IV - Panda's Trick
题目链接: id=3207">http://poj.org/problem? id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1 ...
- iOS模仿微信的那个视频眼睛动画
咳咳,费死老劲把这个动画搞出来了. 欢迎批评指正 github URL: https://git.oschina.net/momochao/WeChatEyeDemo
- Swift Int to String
最近在项目中用到 swift, 涉及到 Int 转 String 类型,需要保留两位数,所以去研究了一下,做个记录 1.通常情况下1.1 Int转 String let intValue1 = 2 l ...
- 国外物联网平台初探(三) ——IBM Watson IoT
平台定位 提供全面管理的云托管服务,旨在简化并从 IoT 设备中获得价值. Watson IoT Platform 提供对 IoT 设备和数据的强大应用程序访问,可快速编写分析应用程序.可视化仪表板和 ...
- 谷歌浏览器(chrome) —— 扩展应用程序
工具的使用--谷歌浏览器(chrome) (二) 1. 设置和下载方法 右上角菜单按钮 ⇒ 更多工具(more tools) ⇒ 扩展(Extensions) 打开该页面之后,会首先进入扩展(已安装应 ...
- [POI 2007] 堆积木
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1109 [算法] DP [代码] #include<bits/stdc++.h& ...
- Java-Maven:Maven清单
ylbtech-Java-Maven:Maven清单 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http ...