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个房间一定与所有点都有通路,即一定都可以逃脱. 但是有可能当所有点都有了该去的安全房间以后 ...
随机推荐
- 扩展欧几里得算法详解(exgcd)
一.前言 本博客适合已经学会欧几里得算法的人食用~~~ 二.扩展欧几里得算法 为了更好的理解扩展欧几里得算法,首先你要知道一个叫做贝祖定理的玄学定理: 即如果a.b是整数,那么一定存在整数x.y使得$ ...
- (转载)图解Java多态内存分配以及多态中成员方法的特点
图解Java多态内存分配以及多态中成员方法的特点 图解Java多态内存分配以及多态中成员方法的特点 Person worker = new Worker(); 子类实例对象地址赋值给父类类型引 ...
- SCAU 2015 GDCPC team_training1
A: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1525 题意:前两段都是废话 , 然后给出一个 p( p(a) ) = p(a) 的公式给你 ...
- Linux mint启用内核转储
1.查看当前是否启动了内核转储: star@sky:~$ ulimit -c unlimited 2.如果上面显示为0,即没有开启,那么,直接执行 ulimit -c unlimited 就好了. 3 ...
- mybatis-plus&springboot
** 问题1:mybatis 读取不到 mapper映射文件. 如下: ** 如果引用 mybatis-plus 包 <dependency> <groupId>com.bao ...
- 搜索---DFS
DFS 广度优先一层一层遍历,每一层得到的所有新节点,要用队列存储起来以备下一层遍历的时候再遍历. 而深度优先遍历搜索在得到一个新节点时立即对新节点进行遍历:从节点0出发开始遍历,得到新节点6 ...
- Failed to determine the https port for redirect
原文:Failed to determine the https port for redirect warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedir ...
- C# 共享文件读取(转)
using System;using System.Runtime.InteropServices;using BOOL = System.Boolean;using DWORD = System.U ...
- 模拟.net post请求属性
这两天在做一个nodejs的爬虫项目,需要模拟post请求获得网站数据.遇到2个asp.net的网站,掉到坑里面,调试了好几天.总结一下过程. 一般我们模拟post请求的时候最重要的就是post请求里 ...
- go中指针类型的用法小结
代码 // 指针的用法 package main import ( "fmt" ) func main() { var i int = 100 // 输出i的地址 fmt.Prin ...