kuangbin 最小生成树专题 - POJ - 2421 Constructing Roads (朴素 Prim算法 模板题)

kuangbin 最小生成树专题 - POJ - 2421 Constructing Roads (朴素 Prim算法 模板题)

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

英文版 Click here ~~

Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

意译版 Click here ~~

题目描述
有N个村庄,从1到N,你应该修建一些道路,这样每两个村庄就可以连接起来。我们说两个村庄A和B相连,当且仅当A和B之间有一条路,或者存在一个村庄C使得A和C之间有一条路,并且C和B相连。我们知道一些村庄之间已经有一些道路了,你的工作是修建一些道路,这样所有的村庄都连接起来,所有道路的长度都是最小的。

输入
第一行是整数N (3 <= N <= 100),即村庄数。然后是N行,第i行包含N个整数,第j列是村i和村j之间的距离(距离应该是[1,1000]内的整数)。然后是整数Q (0 <= Q <= N * (N + 1) / 2),然后是Q行,每一行包含两个整数a和b (1 <= a < b <= N),这意味着a村和b村之间的道路已经建成。

输出
您应该输出一条包含整数的线,该整数是所有要修建的道路的长度,以便所有村庄都连接起来,并且该值是最小的。

Sample Input

3
0 990 692
990 0 179
692 179 0
1
1 2

Sample Output

179

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

注意输入输出!!!!

#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;
}
//计算x和y的最小公倍数
int lcm (int x,int y) {
    return x * y / gcd(x, y);//使用公式
}
//------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
const int N = 110;
int n;//点的数量
int g[N][N];//邻接矩阵存稠密图
int dist[N];//各点到生成树的最小距离
bool st[N];//生成树外的点标记为false

int prim()
{
    int res = 0;;
    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;//将点加到树里面去。
        
        res += dist[t];//把dist[t]加到最小生成树的长度和里面去。
        
        for (int j = 1; j <= n; ++j) {
            if (!st[j]) {
                dist[j] = min(dist[j], g[t][j]);
            }
        }
    }
    return res;
}

int main()
{
    n = read();
    //初始化图
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n; ++j) {
            g[i][j] = inf;
        }
        g[i][i] = 0; 
    }
    //建图
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            g[i][j] = read();
        }
    }
        
    //更新图
    int cnt = read();//cnt表示已连接的边的数量
    while (cnt--) {
        //联通
        int a = read(), b = read();
        g[a][b] = g[b][a] = 0;
    }
    
    for (int i = 1; i <= n; ++i) {
        dist[i] = g[i][1];
    }
    
    memset(st, false, sizeof st);
    
    //点1放入生成树
    st[1] = true;
    printf("%d\n", prim());
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值