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


  • 相关阅读:
    一百一十:CMS系统之剩余菜单栏的页面和视图
    一百零九:CMS系统之前端根据不同权限渲染不同菜单
    一百零八:CMS系统之封装权限判断功能
    一百零七:CMS系统之权限和角色模型定义
    一百零六:CMS系统之修改邮箱功能完成
    一百零五:CMS系统之flask-mail使用和邮箱配置、发送邮件功能
    一百零四:CMS系统之修改邮箱界面
    一百零三:CMS系统之使用sweetalert提示框优化返回结果
    一百零二:CMS系统之sweetalert提示框和使用
    一百零一:CMS系统之自定义restful风格json返回格式和内容
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203942.html
Copyright © 2011-2022 走看看