传送门: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.


解题心得:

  1. 求一个最”大“生成树,裸的。

#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的更多相关文章

  1. poj 2377 Bad Cowtractors

    题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...

  2. poj:4091:The Closest M Points

    poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...

  3. poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)

    http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...

  4. POJ - 2377 Bad Cowtractors Kru最大生成树

    Bad Cowtractors Bessie has been hired to build a cheap internet network among Farmer John's N (2 < ...

  5. POJ 2377 Bad Cowtractors (Kruskal)

    题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-1. 思路:将边权降序再Kruskal,再检查一下是否只有一棵树即可,即根节点只有一个 #include <cstdio> ...

  6. poj 2377 Bad Cowtractors (最大生成树prim)

    Bad Cowtractors Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  7. POJ 2377 Bad Cowtractors( 最小生成树转化 )

    链接:传送门 题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1 思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了 /************* ...

  8. poj 2377 Bad Cowtractors(最大生成树!)

    Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...

  9. POJ:1182 食物链(带权并查集)

    http://poj.org/problem?id=1182 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1 ...

随机推荐

  1. MySQL(五)

    一.视图 视图是一个虚拟表(非真实存在),其本质是根据SQL语句获取动态的数据集,并为其命名,用户使用时只需使用名称即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时表摘 ...

  2. (生产)better-scroll - 下拉刷新

    Options 参数 startX: 0 开始的X轴位置 startY: 0 开始的Y轴位置 scrollY: true 滚动方向为 Y 轴 scrollX: 'true' 滚动方向为 X 轴 cli ...

  3. <Android 基础(九)> Ndk配置与Demo

    介绍 The NDK is a toolset that allows you to implement parts of your app using native-code languages s ...

  4. lucene查询解析器语法

    注意:使用QueryParser查询,关键词是会被分词的,如果不需要分词,可以选择使用Lucene提供的API查询类. Lucene提供了丰富的API来组合定制你所需要的查询器,同时也可以利用Quer ...

  5. day004-Map类

    1.Map集合概述 Map是一个接口,只要是实现了该接口的类就是一个双列集合. 双列集合就是每次存储元素时需要存储两个元素的集合. 这两个元素称为键值对, Key Value ==>映射关系 特 ...

  6. mongoDB基础知识(一)

    mongoDB是一个基于分布式文件存储的数据库,介于关系型数据库和非关系型数据库之间,在非关系型数据库中功能最丰富, 最像关系型数据库.数据结构松散,类似于json的bson格式,可以存储比较复杂的数 ...

  7. php网站修改默认访问文件的nginx配置

    搭建好lnmp后,有时候并不需要直接访问index.php,配置其他的默认访问文件比如index.html这时候需要配置一下nginx才能访问到你想要设置的文件 直接上代码,如下是我的配置的一份简单的 ...

  8. May 8th 2017 Week 19th Monday

    Art lies in concealing art. 隐而不露即艺术. Sometimes, concealing is much more seductive than totally naked ...

  9. STM32开发-MDK新建工程及配置

    本人也是接触stm32没多久,之前用的MDK是5.1,现在用的是5.13,MDK5.0之前的版本(本人简称旧版)和之后的版本(本人简称新版)新建工程有很大区别.对于刚开始用学stm32的新手来说,基本 ...

  10. OJ网站

    没事做做题是打发时间的好办法,还能练习下思维,效益很不错,但就是耗时 就选了3个oj,多了眼花缭乱 https://www.vijos.org/ http://uoj.ac/ https://leet ...