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".

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.

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;
}
时间: 2024-07-29 17:50:23

POJ2377 Bad Cowtractors 【最大生成树】的相关文章

[POJ2377]Bad Cowtractors(最大生成树,Kruskal)

题目链接:http://poj.org/problem?id=2377 于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #inc

POJ2377 Bad Cowtractors【Kruskal】【求最大生成树】

Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10933 Accepted: 4614 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

POJ2377Bad Cowtractors(最大生成树)

POJ2377Bad Cowtractors 题目大意:给一个带权无向图,求最大生成树. 解题思路: 因为最小生成树按照kruskal的贪心算法是可以证明正确的,那么反向我们取最大的权值的边,然后不断的加入形成的生成树就是最大生成树. 代码: #include <cstdio> #include <algorithm> using namespace std; const int maxn = 1005; const int maxm = 20005; int N, M; int

poj2377 Bad Cowtractors

思路: 最大生成树. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 8 struct edge 9 { 10 int a, b, cost; 11 }; 12 edge es[20005]; 13 int ran[1005]; 1

poj - 2377 Bad Cowtractors(最大生成树)

http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她想要花费越多越好,并且任意两个农场都需要连通,并且不能存在环.后面两个条件保证最后的连通图是一棵树. 输出最小花费,如果没办法连通所有节点输出-1. 最大生成树问题,按边的权值从大道小排序即可,kruskal算法可以处理重边的情况,但是在处理的时候,不能仅仅因为两个节点在同一个连通子图就判断图不合法

poj图论解题报告索引

最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman-ford,spfa) poj1511 - Invitation Cards(单源来回最短路径,spfa邻接表) poj1797 - Heavy Transportation(最大边,最短路变形,dijkstra,spfa,bellman-ford) poj2240 - Arbitrage(汇率问题,

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116