首页 > 代码库 > POJ2377 Bad Cowtractors 【最大生成树】
POJ2377 Bad Cowtractors 【最大生成树】
Bad Cowtractors
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10885 | Accepted: 4586 |
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".
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.
* 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.
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.
Source
USACO 2004 December Silver
水题。#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <algorithm> #include <queue> using namespace std; typedef long long LL; #define maxn 1002 #define maxm 200010 #define inf 0x3f3f3f3f int N, M, id, pre[maxn]; struct Node { int u, v, w; } E[maxm]; bool cmp(Node a, Node b) { return a.w > b.w; } int ufind(int k){ int a = k, b; while(pre[k]) k = pre[k]; while(a != k){ b = pre[a]; pre[a] = k; a = b; } return k; } bool unite(int a, int b) { a = ufind(a); b = ufind(b); if(a == b) return false; else { pre[a] = b; return true; } } void addEdge(int u, int v, int w) { E[id].u = u; E[id].v = v; E[id].w = w; } void getMap() { int u, v, w, i, j; id = 0; while(M--) { scanf("%d%d%d", &u, &v, &w); addEdge(u, v, w); ++id; addEdge(v, u, w); ++id; } } void Kruskal() { int i, j, u = 1, v, w, sum = 0; sort(E, E + id, cmp); for(i = 0; i < id && N != 1; ++i) { if(unite(E[i].u, E[i].v)) { --N; sum += E[i].w; } } printf("%d\n", N == 1 ? sum : -1); } int main() { // freopen("stdin.txt", "r", stdin); scanf("%d%d", &N, &M); getMap(); Kruskal(); return 0; }
POJ2377 Bad Cowtractors 【最大生成树】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。