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\n", kruskal());
    }
    return 0;
}


算法码上来 文章被收录于专栏

公众号「算法码上来」。godweiyang带你学习算法,不管是编程算法,还是深度学习、自然语言处理算法都一网打尽,更有各种计算机新鲜知识和你分享。别急,算法码上来。

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-26 18:54
说等下个版本吧的发呆爱好者很贪睡:佬最后去了哪家呀
点赞 评论 收藏
分享
投递华为等公司10个岗位
点赞 评论 收藏
分享
投递大华股份等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务