最小生成树模板

具体学习参考https://blog.youkuaiyun.com/gettogetto/article/details/53216951

prim算法(稠密图):

/*
*O(n^2)
* Prim 求 MST
* 耗费矩阵 cost[][],标号从 0 开始,0 ∼ n-1
* 返回最小生成树的权值,返回 -1 表示原图不连通
*/
bool vis[maxn];
int lowc[maxn];
int prim(int cost[][maxn],int n)
{
    int ans=0;
    memset(vis,false,sizeof(vis));
    vis[0]=true;
    for(int i=1; i<n; i++)
    {
        lowc[i]=cost[0][i];
    }
    for(int i=1; i<n; i++)
    {
        int minc=inf;
        int p=-1;
        for(int j=0; j<n; j++)
        {
            if(!vis[j]&&minc>lowc[j])
            {
                minc=lowc[j];
                p=j;
            }
        }
        if(minc==inf)   return -1;
        ans+=minc;
        vis[p]=true;
        for(int j=0; j<n; j++)
        {
            if(!vis[j]&&lowc[j]>cost[p][j])
                lowc[j]=cost[p][j];
        }
    }
    return ans;
}

prim算法堆优化:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
#define LL long long int
#define N 100000+10 //最大节点数
#define M 1000000+10 //最大的边数
#define MOD 142857
//N<=10^5, M<=10^6
 
using namespace std;
int n, m;
struct node
{
    int v, w;
    bool operator<(const node &dd)const{
        return w>dd.w;
    } //权值小的优先
};
vector<node>q[N];
bool vis[N];
//堆优化的prim算法
LL ans;
 
void queue_prim()
{
    //以节点1为起点进行扩展安全边 生成最小树
    priority_queue<node>que;
    while(!que.empty())
        que.pop(); //初始化清空优先队列 维护一个小根堆
                  //这样每次找安全边的速度就提高了
    ans = 0;
    memset(vis, false, sizeof(vis));
    for(int i=0; i<q[1].size(); i++){
        que.push(q[1][i]); //将起点的所有连接边全部加入队列中来
    }
    vis[1]=true;
    int edge=n-1;//边数
    node cur;
    while(edge--)
    {
        cur = que.top();
        que.pop();//这个地方需要注意一下
                  //并不是每个从优先队列取出来的边都是可以加到生成树上去的
 
        if(vis[cur.v]==true){
            while(vis[cur.v]){
                cur=que.top(); que.pop();
            }
        }
        ans = ans+cur.w; //printf("%d--  ", cur.w );
        vis[cur.v]=true; //加入生成树的该点将被标记访问
        for(int i=0; i<q[cur.v].size(); i++){
            if(vis[ q[cur.v][i].v ]==false) //当前加入生成树的点可以扩充出的边指向的节点
                que.push(q[cur.v][i]);//如果没有被访问才会加入到队列当中来
        }
    }
}
 
int main()
{
    scanf("%d %d", &n, &m);
    int i, j;
    int u, v, w;
    node cur;
    for(i=0; i<=n; i++)
        q[i].clear();
 
    for(i=0; i<m; i++)
    {
       scanf("%d %d %d", &u, &v, &w);
       cur.v=v; cur.w=w;
       q[u].push_back(cur);
       cur.v=u;
       q[v].push_back(cur); //建立双向边
    }
    queue_prim();
    printf("%lld\n", ans );
    return 0;
}

Kruskal算法(稀疏图):

/*
*(ElogE)
* Kruskal 算法求 MST
*/
const int MAXN=110;//最大点数
const int MAXM=10000;//最大边数
int F[MAXN];//并查集使用
struct Edge
{
    int u,v,w;
} edge[MAXM]; //存储边的信息,包括起点/终点/权值
int tol;//边数,加边前赋值为 0
void addedge(int u,int v,int w)
{
    edge[tol].u=u;
    edge[tol].v=v;
    edge[tol++].w=w;
}
//排序函数,讲边按照权值从小到大排序
bool cmp(Edge a,Edge b)
{
    return a.w<b.w;
}
int find(int x)
{
    if(F[x]== -1)return x;
    else return F[x]=find(F[x]);
}
//传入点数,返回最小生成树的权值,如果不连通返回 -1
int Kruskal(int n)
{
    memset(F, - 1,sizeof(F));
    sort(edge,edge+tol,cmp);
    int cnt=0;//计算加入的边数
    int ans=0;
    for(int i=0; i<tol; i++)
    {
        int u=edge[i].u;
        int v=edge[i].v;
        int w=edge[i].w;
        int t1=find(u);
        int t2=find(v);
        if(t1!=t2)
        {
            ans+=w;
            F[t1]=t2;
            cnt++;
        }
        if(cnt==n - 1)break;
    }
    if(cnt<n - 1)return - 1;//不连通
    else return ans;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值