kuangbin 最小生成树专题 - POJ - 1287 Networking (朴素 Prim算法 模板题)

总题单 week 3 [kuangbin带你飞] 题单 最小生成树 + 线段树 Click here ~~
https://blog.csdn.net/m0_46272108/article/details/108980362

英文版 Click here ~~

Description
You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input
The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.

Output
For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

意译版 Click here ~~

题目概述:
前面说到,李云龙集结了所有驻扎在外的部队,想要进攻平安县城,但是平安县城太大了,李云龙的部队太多了,把平安县城围起来之后自己传达命令很不方便,而且由于山本的手下有个狙击手专打通讯员,派遣通讯员很有可能发生意外使得命令传达不到,所以李云龙决定派工程兵修建电话线。由于这是一个危险的任务,而且为了尽快建好通讯电话网络,工程兵需要在最短的时间内建立一个能够传达命令的电话网络。

输入
多样例输入,且至少有一个可行方案。
每个样例的第一行有两个整数,P(需要建立通讯的部队数),R(部队与部队之间能够建立通讯电话的总数)。
接下来的R行输入路径,每行含3个整数,前两个数表示部队编号,最后一个数表示修建这条电话线所需要的时间
当P为0时输入结束。
P最大为50,每条电话线修建时间最多为100。
i和j之间的路径可以表示为 i j 或 j i

输出
对于每个样例,输出一个数表示建立完善的通讯电话网络所需要的最短时间

样例输入

1 0

2 3
1 2 37
2 1 17
1 2 68

3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32

5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12

0

样例输出

0
17
16
26

理解 朴素Prim算法 即可 (代码有详细解释)
关于朴素Prim算法,可以查看上一篇文章(内有模板题及图文详解):
图论 —— 最小生成树(朴素Prim原理及模板题) https://blog.csdn.net/m0_46272108/article/details/108929358

C++ AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
//#define int ll
#define inf 0x3f3f3f3f
using namespace std;
int read()
{
	int w = 1, s = 0;
	char ch = getchar();
	while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';    ch = getchar(); }
	return s * w;
//最大公约数
}int gcd(int x,int y) {
    if(x<y) swap(x,y);//很多人会遗忘,大数在前小数在后
    //递归终止条件千万不要漏了,辗转相除法
    return x % y ? gcd(y, x % y) : y;
}
int lcm(int x,int y)//计算x和y的最小公倍数
{
    return x * y / gcd(x, y);//使用公式
}
//------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
const int N = 1010;
int g[N][N];
int dist[N];
bool st[N];
int n, m;
int Prim()
{
    memset(st, false, sizeof st);//所有点还未到过
    int res = 0;//存最小生成树里面所有长度之和
    for (int i = 1; i <= n; ++i) {
        dist[i] = g[1][i];//所有距离初始化
    }
    for(int i = 1; i <= n; ++i) {
        //初始化t == -1 表示我们当前还没有找到任何一个点。
        int t = -1;
        for (int j = 1; j <= n; ++j) {
            //在集合外,t == -1还没有找到任何一个点 || t的距离大于j的距离
            if(!st[j] && (t == -1 || dist[t] > dist[j])) {
                t = j;//就把t 更新成 j
            }
        }
            
        st[t] = true;//将点加到树里面去。
        
        for (int j = 1; j <= n; ++j) {
            if (!st[j]) {
                dist[j] = min(dist[j], g[t][j]);
            }
        }
    }
    //把dist[t]加到最小生成树的长度和里面去。
    for (int i = 1; i <= n; ++i) {
        res += dist[i];
    }
    return res;
}

int main()
{
    while (~scanf("%d", &n) && n) {
        m = read();
        int u, v, w;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if(i==j)
                    g[i][j] = 0;
                else
                    g[i][j] = inf;
            }
        }
        
        //读入边
        for (int i = 0; i < m; ++i) {
            u = read(), v = read(), w = read();
            //无向图,就是建一条从u到v,再建一条从v到u的就可以了
            g[v][u] = g[u][v] = min(g[u][v], w);//可能有重边,求一个min即可
        }
        printf("%d\n",Prim());
    }
    return 0;
}

Java 代码:


import java.lang.*;
import java.util.*;

public class Main {
    static int N = 1010;
    static int n, m;
    static int [][] g = new int[N][N];
    static int [] dist = new int[N];
    static int [] st = new int[N];

    public static int prim() {
        Arrays.fill(st, 0);
        int res = 0;
        for (int i = 1; i <= n; ++i) {
            dist[i] = g[1][i];
        }

        for (int i = 1; i <= n; ++i) {
            int t = -1;
            for (int j = 1; j <= n; ++j) {
                if (st[j] == 0 && (t == -1 || dist[t] > dist[j])) {
                    t = j;
                }
            }

            st[t] = 1;

            for (int j = 1; j <= n; ++j) {
                if (st[j] == 0) {
                    dist[j] = Math.min(dist[j], g[t][j]);
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            res += dist[i];
        }
        return res;
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while(input.hasNext()) {
            n = input.nextInt();
            if (n == 0) break;
            m = input.nextInt();
            int u, v, w;
            for (int i = 1; i <= n; ++i) {
                for (int j = 1; j <= n; ++j) {
                    if (i == j) g[i][j] = 0;
                    else g[i][j] = 0x3f3f3f3f;
                }
            }

            for (int i = 0; i < m; ++i) {
                u = input.nextInt();
                v = input.nextInt();
                w = input.nextInt();

                g[v][u] = g[u][v] = Math.min(g[u][v], w);
            }

            System.out.println(prim());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值