zoukankan      html  css  js  c++  java
  • poj2377(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

    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.


    最大生成树,不能更水的题,kruskal算法排序倒过来就行


    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <set>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <algorithm>
    #include <functional>
    #include <numeric>
    #include <iomanip>
    #include <limits>
    #include <new>
    #include <utility>
    #include <iterator>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    using namespace std;
    
    struct edge
    {
        int u, v, w;
        bool operator < (const edge& b) const
        {
            return w > b.w;
        }
    };
    
    edge es[100010];
    int V, E;
    int f[3100];
    
    void init()
    {
        for (int i = 0; i < 3100; ++i)
            f[i] = i;
    }
    
    int Find(int x)
    {
        return x == f[x] ? x : (f[x] = Find(f[x]));
    }
    
    void join(int x, int y)
    {
        int fx = Find(x), fy = Find(y);
        f[fx] = fy;
    }
    
    int kruskal()
    {
        sort(es, es+E);
        init();
        int res = 0;
        for (int i = 0; i < E; ++i)
        {
            edge e = es[i];
            if (Find(e.u) != Find(e.v))
            {
                join(e.u, e.v);
                res += e.w;
            }
        }
        int f = Find(0);
        for (int i = 1; i < V; ++i)
            if (Find(i) != f)
                return -1;
        return res;
    }
    
    int main()
    {
        scanf("%d%d", &V, &E);
        for (int i = 0; i < E; ++i)
        {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            es[i] = edge{u-1, v-1, w};
        }
        printf("%d
    ", kruskal());
        return 0;
    }
    


  • 相关阅读:
    排序算法
    【转】《分享一下我研究SQLSERVER以来收集的笔记》未整理
    D3.js学习记录
    D3.js学习记录【转】【新】
    JAVA FILE or I/O学习
    JAVA GUI学习
    android一键锁屏
    源文件如何转换到可执行文件
    手动搭建maven项目
    ThinkingInJava----第11章 持有对象
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203942.html
Copyright © 2011-2022 走看看