poj2377Bad Cowtractors (最小生成树变形之——最大生成树)
题目链接:http://poj.org/problem?id=2377
Description
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Input
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
Output
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 直接利用Kruskal算法,不过不是优先使用最小权值,而是优先使用最大权值,然后再加上并查集的知识就ok了,基本算是一道比较水的题。
先附上AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
int a,b,val;
}nec[20005];
int s[1010],N,M;
int cmp(struct node A,struct node B){
return A.val>B.val;
}
int find(int x){
if(x==s[x]) return x;
else return s[x]=find(s[x]);
}
void unite(int x,int y){
s[find(x)]=find(y);
}
int main(){
int sum=0;
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
s[i]=i;
for(int i=1;i<=M;i++)
scanf("%d%d%d",&nec[i].a,&nec[i].b,&nec[i].val);
sort(nec+1,nec+M+1,cmp);
int su=0;
for(int i=1;i<=M&&su<=N-1;i++){ //这里利用su<=N-1,可以极大缩减耗时
if(find(nec[i].a)==find(nec[i].b)) continue;
else sum+=nec[i].val,unite(nec[i].a,nec[i].b),su++;
}
/* int plug=0;
for(int i=2;i<=N;i++)
if(find(s[1])!=find(s[i])){
plug=1;
break;
}
if(plug) printf("-1\n");
else printf("%lld\n",sum);*/
if(su!=N-1) printf("-1\n"); //判断是否可以全部连接到,直接这么做就ojbk了,比我上面注释掉的方法好。
else printf("%d\n",sum);
return 0;
}
poj2377Bad Cowtractors (最小生成树变形之——最大生成树)的更多相关文章
- There is No Alternative~最小生成树变形
Description ICPC (Isles of Coral Park City) consist of several beautiful islands. The citizens reque ...
- bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 -- 最大生成树
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MB Description 奶牛贝 ...
- UVa 1395 - Slim Span(最小生成树变形)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- bzoj 2753 最小生成树变形
我们根据高度建图,将无向边转化为有向边 首先对于第一问,直接一个bfs搞定,得到ans1 然后第二问,我们就相当于要求找到一颗最小生成树, 满足相对来说深度小的高度大,也就是要以高度为优先级 假设现在 ...
- HDU 4786 最小生成树变形 kruscal(13成都区域赛F)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 4081 最小生成树变形
/*关于最小生成树的等效边,就是讲两个相同的集合连接在一起 先建立一个任意最小生成树,这条边分开的两个子树的节点最大的一个和为A,sum为最小生成树的权值和,B为sum-当前边的权值 不断枚举最小生成 ...
- POJ1789&ZOJ2158--Truck History【最小生成树变形】
链接:http://poj.org/problem?id=1789 题意:卡车公司有悠久的历史,它的每一种卡车都有一个唯一的字符串来表示,长度为7,它的全部卡车(除了第一辆)都是由曾经的卡车派生出来的 ...
- poj 2253 Frogger【最小生成树变形】【kruskal】
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30427 Accepted: 9806 Descript ...
- UESTC 918 WHITE ALBUM --生成树变形
最小生成树变形. 题目已经说得很清楚,要求到达每个房间,只需求一个最小生成树,这时边权和一定是最小的,并且那k个房间一定与所有点都有通路,即一定都可以逃脱. 但是有可能当所有点都有了该去的安全房间以后 ...
随机推荐
- [APIO 2010] [LOJ 3144] 奇怪装置 (数学)
[APIO 2010] [LOJ 3144] 奇怪装置 (数学) 题面 略 分析 考虑t1,t2时刻坐标相同的条件 \[\begin{cases} t_1+\lfloor \frac{t_1}{B} ...
- Dom选择器使用与调试记录
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 用Node.js原生代码实现静态服务器
---恢复内容开始--- 后端中服务器类型有两种 1. web服务器[ 静态服务器 ] - 举例: wamp里面www目录 - 目的是为了展示页面内容 - 前端: nginx 2. 应用级服务器[ a ...
- 行人重识别(ReID) ——数据集描述 Market-1501
数据集简介 Market-1501 数据集在清华大学校园中采集,夏天拍摄,在 2015 年构建并公开.它包括由6个摄像头(其中5个高清摄像头和1个低清摄像头)拍摄到的 1501 个行人.32668 个 ...
- ASE Alpha Sprint - backend scrum 9
本次scrum于2019.11.14再sky garden进行,持续15分钟. 参与人: Xin Kang, Zhikai Chen, Jia Ning, Hao Wang 请假: Lihao Ran ...
- 模块(os模块)
一.模块 一个python文件就是一个模块. 模块可分为: 1.标准模块:python自带的模块是标准模块,可以直接import进行使用的. eg:import json.import random. ...
- Promise.all 的原理
// all的原理 Promise.all = function(values){ return new Promise((resolve,reject)=>{ let results = [] ...
- 安装Git,Maven,配置ssh认证
安装git: yum -y install git 安装maven: wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.6.1/binaries ...
- $mona$要成为高端玩家
\(mona\)要成为高端玩家! 好在撑过了联赛,接下来要向高端玩家冲击啦! 新时期当然要有新的学习规划啦! 最近的更新(有什么就在这里说啦) 随便更更. \(FFT\)刷着打算先看看生成函数. 感觉 ...
- securityDemopom
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...