• POJ Problem 2377 Cowtractors 【最小生成树Prim】


    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13483   Accepted: 5576

    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

    题意:有从1—N个点,求这些点的最大生成树。要求权值最大,不能出现环,而且所有节点都在树上。

    思路:用Prim算法求出最大生成树,在检查是否只有一棵树。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <iostream>
    #define MAX_N 1005
    using namespace std;
    const int INF = 1e9;
    const double ESP = 1e-5;
    
    int cost[MAX_N][MAX_N];
    int maxn[MAX_N], par[MAX_N];
    int n, m;
    bool vis[MAX_N];
    
    int prim() {
        for (int i = 0; i <= n; i++) {
            maxn[i] = 0;
        }
        memset(vis, false, sizeof(vis));
        maxn[1] = 0;
        int res = 0;
        while (true) {
            int v = -1;
            for (int u = 1; u <= n; u++) {
                if (!vis[u] && (v == -1 || maxn[u] > maxn[v])) v = u;
            }
            if (v == -1) break;
            vis[v] = true;
            res += maxn[v];
            for (int i = 1; i <= n; i++) {
                maxn[i] = max(maxn[i], cost[i][v]);
            }
        }
        return res;
    }
    void init(){
        for (int i = 1; i < 1003; i++) {
            par[i] = i;
        }
    }
    int findt(int x) {
        if (par[x] == x) return x;
        else return par[x] = findt(par[x]);
    }
    void unite(int x, int y) {
        int fx = findt(x);
        int fy = findt(y);
        if (fx != fy) {
            par[fx] = fy;
        }
    }
    int main() {
        int a, b, c;
        bool tree[MAX_N];
        while (scanf("%d%d", &n, &m) != EOF) {
            bool flag = false;
            int cnt = 0;
            init();
            memset(cost, 0, sizeof(cost));
            memset(tree, false, sizeof(tree));
            for (int i = 1; i <= m; i++) {
                scanf("%d%d%d", &a, &b, &c);
                unite(a, b);
                c = max(c, cost[a][b]);
                cost[a][b] = cost[b][a] = c;
            }
            for (int i = 1; i <= n; i++) {
                if (par[i] == i) cnt++;
                if (cnt >= 2) {
                    flag = true;
                    break;
                }
            }
            if (flag) printf("-1
    ");
            else printf("%d
    ", prim());
        }
        return 0;
    }
    
    

     

  • 相关阅读:
    C#网络编程简单实现通信小例子-1
    对象序列化
    C#网络编程(1)
    Web前端基础——jQuery(三)
    Web前端基础——jQuery(二)
    Web前端基础——jQuery(一)
    Java基础——正则表达式
    Java基础——Servlet(八)文件上传下载
    Java基础——Servlet(七)过滤器&监听器 相关
    Java基础——Servlet(六)分页相关
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770885.html
Copyright © 2020-2023  润新知