题目链接:http://poj.org/problem?id=2377

Description

Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.

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

* Line 1: Two space-separated integers: N and M

* 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

* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.

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 (最小生成树变形之——最大生成树)的更多相关文章

  1. There is No Alternative~最小生成树变形

    Description ICPC (Isles of Coral Park City) consist of several beautiful islands. The citizens reque ...

  2. bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 -- 最大生成树

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MB Description     奶牛贝 ...

  3. UVa 1395 - Slim Span(最小生成树变形)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. bzoj 2753 最小生成树变形

    我们根据高度建图,将无向边转化为有向边 首先对于第一问,直接一个bfs搞定,得到ans1 然后第二问,我们就相当于要求找到一颗最小生成树, 满足相对来说深度小的高度大,也就是要以高度为优先级 假设现在 ...

  5. HDU 4786 最小生成树变形 kruscal(13成都区域赛F)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. hdu 4081 最小生成树变形

    /*关于最小生成树的等效边,就是讲两个相同的集合连接在一起 先建立一个任意最小生成树,这条边分开的两个子树的节点最大的一个和为A,sum为最小生成树的权值和,B为sum-当前边的权值 不断枚举最小生成 ...

  7. POJ1789&amp;ZOJ2158--Truck History【最小生成树变形】

    链接:http://poj.org/problem?id=1789 题意:卡车公司有悠久的历史,它的每一种卡车都有一个唯一的字符串来表示,长度为7,它的全部卡车(除了第一辆)都是由曾经的卡车派生出来的 ...

  8. poj 2253 Frogger【最小生成树变形】【kruskal】

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30427   Accepted: 9806 Descript ...

  9. UESTC 918 WHITE ALBUM --生成树变形

    最小生成树变形. 题目已经说得很清楚,要求到达每个房间,只需求一个最小生成树,这时边权和一定是最小的,并且那k个房间一定与所有点都有通路,即一定都可以逃脱. 但是有可能当所有点都有了该去的安全房间以后 ...

随机推荐

  1. IDEA错误: 找不到或无法加载主类 com.xxx.freight.dofreight.doFreight解决办法

    1.右键点击工程,选择open Module Settings或点击File选择Project Structure,进入页面 2.选择Artifacts->JAR->From module ...

  2. java反射, 不看你可别后悔

    开发中, 难免遇到些私有的属性和方法, 就好比下面的实体一样, 我们该怎么获得她, 并玩弄于手掌呢? 我们先来个实体瞧瞧, 给你个对象你也new不了, hahaha- 单身wang public cl ...

  3. ASE Alpha Sprint - backend scrum 3

    本次scrum于2019.11.7再sky garden进行,持续10分钟. 参与人: Zhikai Chen, Jia Ning, Hao Wang 请假: Xin Kang, Lihao Ran, ...

  4. C++ CMake 入门实战[转载]

    C++ CMake 入门实战 2016-11-05 CMake用于跨平台的编译系统,对于通常的c/c++工程,都是通过make来进行编译的,CMake可以通过指令生成Makefile文件来指导整个项目 ...

  5. 2018-8-10-cant-found-Microsoft.VSSDK.BuildTools.15.0.26201

    title author date CreateTime categories cant found Microsoft.VSSDK.BuildTools.15.0.26201 lindexi 201 ...

  6. C#.Net 调用Java的Web Service

    首先,得有一个web service地址:http://www.baiduc.om/XXServices?wsdl 然后在.net 项目中添加Web引用,并把地址给它输进去 第三.编码: using ...

  7. Taro -- 使用 Redux 来进行全局变量的管理

    前言 Redux是JavaScript 状态容器,提供可预测化的状态管理.一般来说,规模比较大的小程序,页面状态,数据缓存,需要管理的东西太多,这时候引入Redux可以方便的管理这些状态,同一数据,一 ...

  8. W3C 事件切换 颜色变化

    颜色变化代码: HTML: <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  9. 使用rabbitctl添加用户

    使用rabbitctl添加用户 第一.添加mq用户并设置密码 root@live-mq-01:~ # rabbitmqctl add_user mq 123456 1 root@live-mq-01: ...

  10. powerdesign 显示comment,生成excel

    一.显示 Comment 1. 在弹窗的Table描述里面显Comment 效果图 实现方式: 2. 在最外层显示Comment 效果图 现实方式: 2.1. Tools > Display P ...