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带你学习算法,不管是编程算法,还是深度学习、自然语言处理算法都一网打尽,更有各种计算机新鲜知识和你分享。别急,算法码上来。

全部评论

相关推荐

10-09 00:50
已编辑
长江大学 算法工程师
不期而遇的夏天:1.同学你面试评价不错,概率很大,请耐心等待;2.你的排名比较靠前,不要担心,耐心等待;3.问题不大,正在审批,不要着急签其他公司,等等我们!4.预计9月中下旬,安心过节;5.下周会有结果,请耐心等待下;6.可能国庆节前后,一有结果我马上通知你;7.预计10月中旬,再坚持一下;8.正在走流程,就这两天了;9.同学,结果我也不知道,你如果查到了也告诉我一声;10.同学你出线不明朗,建议签其他公司保底!11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
牛牛不牛牛3627:再找个稍微大一点的项目做一下然后搞懂,或者如果没时间了的话能八股背熟手撕都能做。建议就是多投多面,面完整理问的问题都搞懂,面多了你就大搞了解都会问什么问题了。主要还是八股,我当时就一个黑马点评也拿到大厂实习了,加油
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务