题目连接

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

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

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

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

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

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

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

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

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

  5. POJ 2377 Bad Cowtractors (Kruskal)

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

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

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

  7. POJ:2377-Bad Cowtractors

    传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...

  8. MST:Bad Cowtractors(POJ 2377)

    坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...

  9. POJ 2377

    #include<stdio.h> #define MAXN 1005 #include<iostream> #include<algorithm> #define ...

随机推荐

  1. My Rules of Information

    http://www.infotoday.com/searcher/jan02/block.htm I often suggested to students that information is ...

  2. TCP/IP详解学习笔记(13)-- TCP连接的建立与终止

    1.TCP连接的建立            设主机B运行一个服务器进程,它先发出一个被动打开命令,告诉它的TCP要准备接收客户进程的连续请求,然后服务进程就处于听的状态.不断检测是否有客户进程发起连续 ...

  3. Windows7:Visual Studio 2008试用版的评估期已经结束解决方法

    Windows7:Visual Studio 2008试用版的评估期已经结束解决方法       以前在Windows2003碰到这个问题时,都是到"控制面板→添加或删除程序"选择 ...

  4. Android书籍资源汇总

    之前一直在Linux下使用C++做服务器端的开发工作,对Android关于移动方面的开发了解较少,现将android开发方面的书籍与资源整理如下,方便后续的查阅. 19本Android开发书籍 7本A ...

  5. LoadRunner性能测试指挥中心Controller 《第四篇》

    一.设计场景 1.设计性能测试场景 Controller有两个视图:设计(Design)视图和运行(Run)视图.启动Controller,在Controller主界面里,我们可以看到这两个视图. 1 ...

  6. Windows phone 8 学习笔记(8) 定位地图导航(转)

    Windows phone 8 已经不使用自家的bing地图,新地图控件可以指定制图模式.视图等.bing地图的定位误差比较大,在模拟器中测试新地图貌似比较理想.本节主要讲解下位置服务以及新地图控件的 ...

  7. sorcketlog

    正在运行的API有bug,不能var_dump进行调试,因为会影响client的调用. 将日志写到文件,查看也不方便,特别是带调用栈或大数据结构的文件日志,查看日志十分困难. 这时候用SocketLo ...

  8. Android IOS WebRTC 音视频开发总结(十八)-- 手机适配

    本文主要介绍上次碰到的某些机器上看不到视频的问题,文章来自博客园RTC.Blacker,转载请说明出处. 之前做的视频聊天App一直运行良好,前几天客户反馈说在三星9100. Android4.0.3 ...

  9. 学生信息管理系统应用ios源码iPad版

    学生信息管理系统应用iPad版,该应用源码比较完整的,而且也很详细,这也是一款学校用的学生和老师管理系统,里面涉及到了很多ipad常用的控件,操作和数据存储. <ignore_js_op> ...

  10. MySQL中的数据类型

    文本 CHAR(*):最多255个字节的定长字符串,它的长度必须在创建时指定 VARCHAR(*):最多255个字节的可变长度字符串,它的长度必须在创建时指定 TEXT:最大长度为64K字符的变长文本 ...