• 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;
    }
    


  • 相关阅读:
    真不容易...终于我也有了个js的语法高亮
    持久层相关概念
    测试语法高亮显示
    BSTR简介和内部结构
    Debugging JavaScript in Your Applications
    Google Analytics 跟踪代码迁移手册
    我对事件驱动的理解
    imagettftext 可调整字间距输出
    投票机的实现及相关技术
    js光标定位到文本末尾
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203942.html
Copyright © 2020-2023  润新知