51nod-1212 无向图最小生成树

本文详细介绍了两种求解最小生成树的经典算法:Prim算法和Kruskal算法,并提供了完整的C++实现代码。通过实例讲解了如何利用这两种算法解决具体问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目传送门: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1212


题目大意:第一行给N个点M个边,接下来M行每行给三个数a,b,c分别是线的两个端点以及线的长度,输出最小生成树的权值之和


算法一:prime

先添加任意一个点到集合V中,然后根据贪心的思想找到与集合V中点相连的非V集合元素加到V集合中,直到将N个点全加到集合V中。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <string>
#include <cmath>
#define INF 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long
#define mem(name,value) memset(name,value,sizeof(name))
#define mod 1000000000
#define PI 3.1415926
#define E  2.718281828459
using namespace std;
const int maxn = 1005;

int Map[maxn][maxn];

int prime(int n,int m)
{
    int tree[maxn];
    mem(tree,0);
    int Begin = 1;
    int lowcost[maxn];// lowcost[i]表示非V集合中的点i到V集合中的点的小权值
    tree[Begin] = 1;
    int cost = 0;
    for(int i=1;i<=n;i++) lowcost[i] = Map[Begin][i];
    for(int i=1;i<n;i++){
        int Min = INF;
        int index = 0;
        for(int j=1;j<=n;j++){
            if(!tree[j] && Min > lowcost[j]){
                index = j;
                Min = lowcost[j];
            }
        }
        cost += Min;
        tree[index] = 1;//加入V集合中
        Begin = index;
        for(int j=1;j<=n;j++)//更新非V集合中的点到V集合中的点的小权值
            if(!tree[j] && Map[Begin][j] < lowcost[j]) lowcost[j] = Map[Begin][j];
    }

    return cost;//最小花费
}
int main()
{
    int n,m;
    cin >> n >> m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            Map[i][j] = INF;
    int a,b,c;
    for(int i=0;i<m;i++){
        scanf("%d %d %d",&a,&b,&c);
        Map[a][b] = c;
        Map[b][a] = c;
    }
    cout << prime(n,m) << endl;

    return 0;
}

算法二:kruskal

同样运用了贪心的思想,用一个优先队列,每次取出边长最短的边。

刚开始每个点都是独立的集合,然后从优先队列中取出边,如果该边的两个端点不在同一个集合中,合并两个点的集合,将元素少的集合合并到元素多的集合中,重复操作直到集合中有n-1条边。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <string>
#include <cmath>
#include <queue>
#define INF 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long
#define mem(name,value) memset(name,value,sizeof(name))
#define mod 1000000000
#define PI 3.1415926
#define E  2.718281828459
using namespace std;
const int maxn = 1005;

struct edge{
    int point1;
    int point2;
    int lenth;
    friend bool operator < (const struct edge a,const struct edge b){
        return a.lenth > b.lenth;
    }
};

priority_queue<struct edge>Map;

int root[maxn];
int next[maxn];
int lenth[maxn];

void setUnion(int n)
{
    for(int i=1;i<=n;i++){
        next[i] = root[i] = i;
        lenth[i] = 1;
    }
}

void Union(int v,int u)
{
    if(lenth[v] > lenth[u]) swap(v,u);
    int rt = root[v];
    lenth[u] += lenth[v];
    root[rt] = root[u];
    for(int i=next[rt];i!=rt;i=next[i]){
        root[i] = root[u];
    }
    swap(next[rt],next[u]);
}

int kruskal(int n,int m)
{
    struct edge temp;
    int cost = 0;
    int edge_num = 0;
    while(edge_num < n -1){
        temp = Map.top();
        Map.pop();
        if(root[temp.point1] != root[temp.point2]){
            cost += temp.lenth;
            Union(temp.point1,temp.point2);
            edge_num++;
        }
    }

    return cost;
}

int main()
{

    int n,m;
    cin >> n >> m;
    struct edge temp;
    setUnion(n);
    for(int i=0;i<m;i++){
        scanf("%d %d %d",&temp.point1,&temp.point2,&temp.lenth);
        Map.push(temp);
    }
    cout << kruskal(n,m) << endl;

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值