OpenJudge: Agri-Net

该博客讨论了一种解决农民约翰如何通过铺设最少的光纤将所有农场连接起来的问题,以实现他的竞选承诺——为所有农场提供互联网连接。这个问题可以转化为经典的图论问题——最小生成树。通过构建一个邻接矩阵并应用Prim算法或Kruskal算法来找到最小的总距离,从而确定所需铺设的光纤总长度。给出的示例中,给出了一个4个农场的案例,并计算了需要的最小光纤长度为28单位。博客深入介绍了算法细节和实现方法。
摘要由CSDN通过智能技术生成

总时间限制: 1000ms  内存限制: 65536kB

描述

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.

输入

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.

输出

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.

样例输入

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

样例输出

28

分析

最小生成树。没啥好说的

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int N = 105;

struct Edge {
    int x, y, dis;
    Edge(int _x, int _y, int _d) : x(_x), y(_y), dis(_d) {}
    bool operator<(const Edge &e) const { return this->dis > e.dis; }
};

struct UnionFind {
    vector<int> f;
    UnionFind(int n) {
        for (int i = 0; i < n; ++i) {
            f.push_back(i);
        }
    }
    bool insert(int x, int y) {
        int fx = find(x);
        int fy = find(y);
        if (fx != fy) {
            f[fx] = fy;
            return true;
        } else {
            return false;
        }
    }
    int find(int d) {
        if (d != f[d])
            f[d] = find(f[d]);
        return f[d];
    }
};

int handle_case(int n) {
    priority_queue<Edge> q;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int d = 0;
            scanf("%d", &d);
            q.push(Edge(i, j, d));
        }
    }

    int sum_edges = 0, no_edges = 0;
    UnionFind uf = UnionFind(n);
    while (!q.empty() && no_edges < n - 1) {
        Edge curr = q.top();
        q.pop();

        bool succ = uf.insert(curr.x, curr.y);
        if (succ) {
            ++no_edges;
            sum_edges += curr.dis;
        }
    }
    return sum_edges;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    int n = 0;
    while (scanf("%d", &n) != EOF) {
        int a = handle_case(n);
        printf("%d\n", a);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值