poj 2377 Bad Cowtractors
题目连接
http://poj.org/problem?id=2377
Bad Cowtractors
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
最大生成树,注意不连通的情况。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::map;
using std::min;
using std::sort;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1100;
const int INF = 0x3f3f3f3f;
struct Kruskal {
struct edge {
int u, v, w;
inline bool operator<(const edge &x) const {
return w > x.w;
}
}G[N * 20];
int E, par[N], rank[N];
inline void init(int n) {
E = 0;
rep(i, n + 1) {
par[i] = i;
rank[i] = 0;
}
}
inline int find(int x) {
while(x != par[x]) {
x = par[x] = par[par[x]];
}
return x;
}
inline bool unite(int x, int y) {
x = find(x), y = find(y);
if(x == y) return false;
if(rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
rank[x] += rank[x] == rank[y];
}
return true;
}
inline void built(int m) {
int u, v, w;
while(m--) {
scanf("%d %d %d", &u, &v, &w);
G[E++] = (edge){ u, v, w };
}
sort(G, G + E);
}
inline void kruskal(int n) {
bool f = false;
int ans = 0, cnt = 0;;
rep(i, E) {
int u = G[i].u, v = G[i].v;
if(unite(u, v)) {
ans += G[i].w;
if(++cnt >= n - 1) { f = true; break; }
}
}
printf("%d\n", f ? ans : -1);
}
inline void solve(int n, int m) {
init(n), built(m), kruskal(n);
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m;
while(~scanf("%d %d", &n, &m)) {
go.solve(n, m);
}
return 0;
}
poj 2377 Bad Cowtractors的更多相关文章
- 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 (最大生成树prim)
Bad Cowtractors Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- 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(最大生成树!)
Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...
- POJ 2377 Bad Cowtractors (Kruskal)
题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-1. 思路:将边权降序再Kruskal,再检查一下是否只有一棵树即可,即根节点只有一个 #include <cstdio> ...
- POJ 2377 Bad Cowtractors( 最小生成树转化 )
链接:传送门 题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1 思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了 /************* ...
- POJ:2377-Bad Cowtractors
传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- MST:Bad Cowtractors(POJ 2377)
坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...
- POJ 2377
#include<stdio.h> #define MAXN 1005 #include<iostream> #include<algorithm> #define ...
随机推荐
- plsql自定义快捷键
说明:如 输入 sf按空格 就变成 SELECT * FROM 输入 w空格 就变成 WHERE 可以帮助你快速的写语句,配置如下图
- extern “C”原理,用法以及使用场景-2016.01.05
1 问题提出 在编程过程中,经常发现如下用法: #ifndef _FILE_NAME_H_ #define _FILE_NAME_H_ #ifdef __cplusplus extern " ...
- C++Builder 2010 Release版本配置
1.Project->Options->C++Compiler 右边Build Configuration 选择 Release,点击Apply选择optionset文件(第四步中保存op ...
- poj1006_Biorhythms
英语真差劲啊,看题目没看明白,无奈重新开始手抄题目,突然发现一句话 “For each cycle,you will be given the number of days form the begi ...
- hdu2085
#include <stdio.h> int main(){ __int64 h[],l[]; int i,j; int n; ){ h[]=; l[]=; ;i<=n;++i){ ...
- 001Linux命令
1.删除非空目录的命令:rm -rf [目录名],r表示迭代,f表示强制: 删除空目录:rmdir [目录名]: 删除文件:rm [文件名]: 2.用户管里类命令: (1)添加用户:useradd [ ...
- jansen字符串转换为xml格式
/// <summary> /// json字符串转换为Xml对象 /// </summary> /// <param name="sJson"> ...
- POJ C++程序设计 编程题#1 编程作业—继承与派生
编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 写一个MyStr ...
- PHP基础笔记汇总
一.PHP简介PHP:超文本预处理器(译者注:PHP: Hypertext Preprocessor,递归命名)PHP 是一种服务器端的脚本语言,类似 ASPPHP 文件的文件后缀是 ".p ...
- javascript变量和对象要注意的点
js对象和变量,对象本身是没有名称的,之所以使用变量是为了通过某个名称来称呼这样一种不具名称的对象.变量分基本变量和引用类型变量.将基本类型的的值付给变量的话,变量将这个值本身保存起来. <sc ...