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


  • 相关阅读:
    WindowsXP/2000来帮您自动关机
    ASP程序加密/解密方法大揭密
    教你如何在Windows XP使用定时关机命令
    向左滚动 替代 Marquee
    [转]Subversion的权限控制
    Xplanner的安装
    推荐一个不错的浮动广告代码
    微软称20日验证Windows与Office 盗版将黑屏?!
    Internet Explorer 8 Beta 2十大看点
    [转]Subversion的权限控制
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203942.html
Copyright © 2011-2022 走看看