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算 ...
随机推荐
- 【ACM】nyoj_305_表达式求值_201308081018
表达式求值时间限制:3000 ms | 内存限制:65535 KB 难度:3描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20, ...
- 【ACM】hdu_zs1_1003_放大的X _201307271557
放大的X Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)Total Submissio ...
- 洛谷 P1535 游荡的奶牛
P1535 游荡的奶牛 题目描述 Searching for the very best grass, the cows are travelling about the pasture which ...
- 配置db账号和密码时一定注意空格问题、空行问题否则连接报错
#postgresql dbpg.datasource.type=com.alibaba.druid.pool.DruidDataSourcepg.datasource.driverClassName ...
- 4443: [Scoi2015]小秃玩矩阵|二分答案|匈牙利
第K大看成第K小各种WA. .. 第K大也就是第n−K+1小.所以就能够愉快的二分答案了 二分答案找出比当前答案小的数的位置的坐标.推断一下能否够选出满足不在同一行同一列的n−K+1个数,然后就能够愉 ...
- linux下select,poll,epoll的使用与重点分析
好久没用I/O复用了,感觉差点儿相同都快忘完了.记得当初刚学I/O复用的时候花了好多时间.可是因为那会不太爱写博客,导致花非常多时间搞明确的东西,依旧非常easy忘记.俗话说眼过千遍不如手过一遍,的确 ...
- JavaScript 和Ajax跨域问题
json格式: { "message":"获取成功", "state":"1", "result": ...
- hdoj--1045--Fire Net(二分图)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- The Triangle--nyoj 18
The Triangle 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure ...
- 【撸码caffe 三】 caffe.cpp
caffe.cpp文件完成对网络模型以及模型配置参数的读入和提取,提供了网络模型训练的入口函数train和对模型的测试入口函数test.文件中使用了很多gflags和glog指令,gflags是goo ...