POJ:2377-Bad Cowtractors
传送门:http://poj.org/problem?id=2377
Bad Cowtractors
Time Limit: 1000MS      Memory Limit: 65536K 
Total Submissions: 17373        Accepted: 7040
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
Hint
OUTPUT DETAILS:
The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.
解题心得:
- 求一个最”大“生成树,裸的。
 
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 2e4+100;
int n,m,father[1010];
struct Path {
    int s,e,len;
    bool operator < (const Path & a) const {
        return a.len < len;
    }
}path[maxn];
void init() {
    for(int i=1;i<=n;i++)
        father[i] = i;
    for(int i=0;i<m;i++) {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        path[i].s = a, path[i].e = b, path[i].len = c;
    }
    sort(path,path+m);
}
int find(int x) {
    if(father[x] == x)
        return x;
    return father[x] = find(father[x]);
}
void merge(int x,int y) {
    int fx = find(x);
    int fy = find(y);
    if(fx != fy)
        father[fx] = fy;
}
int main() {
    scanf("%d%d",&n,&m);
    init();
    ll ans = 0;
    for(int i=0;i<m;i++) {
        if(find(path[i].s) != find(path[i].e)) {
            ans += path[i].len;
            merge(path[i].s,path[i].e);
        }
    }
    int cnt = 0;
    for(int i=1;i<=n;i++)
        if(father[i] == i)
            cnt++;
    if(cnt >= 2)
        ans = -1;
    printf("%d",ans);
    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:4091:The Closest M Points
		
poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...
 - 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 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 (最大生成树prim)
		
Bad Cowtractors Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
 - POJ 2377 Bad Cowtractors( 最小生成树转化 )
		
链接:传送门 题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1 思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了 /************* ...
 - poj 2377 Bad Cowtractors(最大生成树!)
		
Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...
 - POJ:1182 食物链(带权并查集)
		
http://poj.org/problem?id=1182 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1 ...
 
随机推荐
- node.js 模块和其下载资源的镜像设置
			
以前安装 electron 时总是失败,然后就在淘宝镜像上下载好相应版本的文件放到用户目录来解决问题. 后来研究发现 npm 不仅可以设置 node.js 模块仓库的代理, 同样可以设置像 elect ...
 - Android Studio 小技巧(2):AS中Button文字默认大写的问题
			
问题类型 设置Layout中添加一个Button <Button android:id="@+id/bt_showerror" android:layout_width=&q ...
 - SVN中建立项目
			
下午建个svn的时候,出错,有个东西配置错了,晚上google看到一篇文章,觉得作者写的不错,而且很用心,转来共享. [转至]5分钟快速建立项目版本控制 – Face Code,Brain bloom ...
 - 笨办法学Python(零)
			
py走起!!! 习题0:准备工作 Windows平台 1. 用浏览器打开 http://learnpythonthehardway.org/exercise0.html 下载并安装 gedit 文本编 ...
 - ffmpeg  安装和参数介绍
			
0.说明: 1).configure,这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制,比如代码:./configure –pr ...
 - Linux MySQL 5.1源码安装
			
安装必备的软件 yum install ncurses-devel -y yum install pcre pcre-devel -y yum install gcc* -y 解压缩 tar -z ...
 - ELF格式文件符号表全解析及readelf命令使用方法
			
http://blog.csdn.net/edonlii/article/details/8779075 1. 读取ELF文件头: $ readelf -h signELF Header: Magi ...
 - 线程属性总结 线程的api属性
			
http://blog.csdn.net/zsf8701/article/details/7842392 //线程属性结构如下:typedef struct{ int etachstate; //线程 ...
 - 命令搜索命令whereis与which
			
whereis 命令名 #搜索命令所在路径及帮助文档所在位置,只能搜索系统命令. 选项: -b: 只查找可执行文件 -m: 只查找帮助文件 whoami whatis ls #ls 是什么命令 whi ...
 - 利用python中的PIL进行矩阵与图像之间的转换
			
1.图像转换为矩阵 matrix = numpy.asarray(image) 2.矩阵转换为图像 image = Image.fromarray(matrix)