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 ...
随机推荐
- webpack-webpackConfig-plugin 配置
ProvidePlugin 语法: module.export = { plugins: [ new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jqu ...
- Design Pattern ->Composite
Layering & Contract Philosophy With additional indirection class CComponent { ; ; ; public: virt ...
- 绘图之Canvas学习
一 Canvas的用法 博客:http://blog.taorenjia.com/?p=237 1.drawCircle(float cx, float cy, float radius, ...
- 【从业余项目中学习1】C# 实现XML存储用户名密码(MD5加密)
最近在写一个C#的项目,用户需求是实现Winform的多文档界面与Matlab算法程序之间的交互.做了一段时间发现,这既能利用业余时间,实战中也可学习一些技术,同时刚毕业也增加一份收入.所以后面会不断 ...
- 开发中常用的sql语句二
sql 数字全角半角转换 create FUNCTION dbo.ConvertWordAngle ( ), --要转换的字符串 @flag bit --转换标志,0转换成半角,1转换成全角 )) A ...
- SonarQube代码质量管理平台介绍与搭建
前 言 1.SonarQube的介绍 SonarQube是一个管理代码质量的开放平台. 可以从七个维度检测代码质量(为什么要用SonarQube): (1) 复杂度分布(complexity):代码复 ...
- 整理齐全 - Vultr VPS自定义安装Windows ISO(2003/2012/2008/WIN7)
最近公司有几个项目是需要在Windows VPS服务器中运行调试的,但是公司给予的成本有限,所以只能在Linux VPS中考虑,毕竟Linux服务器相比Windows系统便宜很多.开始我们运维部门考虑 ...
- [转载]Memcached缓存服务的简单安装
1.Linux下的安装方法 下载:wget http://memcached.org/latest tar -zxvf memcached-1.x.x.tar.gz cd memcached-1.x. ...
- 45. 腾讯面试题: 使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据
题目:使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据 分析: 使用hashmap插入数据,数据的顺序会改变.能够写个小程序试试. 那怎么样依照插入的顺序输出呢? 方法一: 这是我第一时 ...
- 【BZOJ3506】[CQOI2014] 排序机械臂(Splay)
点此看题面 大致题意: 给你\(n\)个数.第一次找到最小值所在位置\(P_1\),翻转\([1,P_1]\),第二次找到剩余数中最小值所在位置\(P_2\),翻转\([2,P_2]\),以此类推.求 ...