zoukankan      html  css  js  c++  java
  • poj1258(Agri-Net)最小生成树

    Description

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. 
    Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. 
    Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. 
    The distance between any two farms will not exceed 100,000. 

    Input

    The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

    Output

    For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

    Sample Input

    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0
    

    Sample Output

    28


    裸的最小生成树,直接kruskal或者prim算法


    #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[10000];
    int V, E;
    int f[110];
    
    void init()
    {
        for (int i = 0; i < 110; ++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;
            }
        }
        return res;
    }
    
    int main()
    {
        while (~scanf("%d", &V))
        {
            E = 0;
            int w;
            for (int i = 0; i < V; ++i)
                for (int j = 0; j < V; ++j)
                {
                    scanf("%d", &w);
                    if (j > i)
                    es[E++] = edge{i, j, w};
                }
            printf("%d
    ", kruskal());
        }
        return 0;
    }
    


  • 相关阅读:
    运算符和表达式详解
    超实用的Java web面试题
    80道最新java基础部分面试题(七)
    80道最新java基础部分面试题(六)
    80道最新java基础部分面试题(五)
    12道算法与编程面试题
    javaee和javase的区别
    2019年最新50道java基础部分面试题(四)
    2019年最新50道java基础部分面试题(三)
    2019年最新50道java基础部分面试题(二)
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203943.html
Copyright © 2011-2022 走看看