最小生成树

最小生成树的图中允许存在负环

请添加图片描述

prim

思路:

请添加图片描述

时间复杂度:O(n2+m), n 表示点数,m 表示边数
模版:
int n;      // n表示点数
int g[N][N];        // 邻接矩阵,存储所有边
int dist[N];        // 存储其他点到当前最小生成树的距离
bool st[N];     // 存储每个点是否已经在生成树中


// 如果图不连通,则返回INF(值是0x3f3f3f3f), 否则返回最小生成树的树边权重之和
int prim()
{
    memset(dist, 0x3f, sizeof dist);

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

        if (i && dist[t] == INF) return INF;

        if (i) res += dist[t];
        st[t] = true;

        for (int j = 1; j <= n; j ++ ) dist[j] = min(dist[j], g[t][j]);
    }

    return res;
}

作者:yxc
链接:https://www.acwing.com/blog/content/405/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

题目1:Prim算法求最小生成树
分析:

m 和 n 2 n^2 n2 是一个级别, 用prim算法

时间复杂度:O(n^ 2)
代码区:
#include<iostream>
#include<cstring>

using namespace std;

const int N = 510, M = 1e5 + 10;

int n, m;
int g[N][N];
int dist[N];
bool st[N];
int res;

bool prim()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    
    for(int i = 0; i < n; i ++)
    {
        int t = -1;
        for(int j = 1; j <= n; j ++)
        {
            if(!st[j] && (t == -1 || dist[t] > dist[j]))
            {
                t = j;
            }
        }
        
        if(i && dist[t] == 0x3f3f3f3f) return false;
        st[t] = true;
        res += dist[t];
        
        for(int j = 1; j <= n; j ++)
        {
            dist[j] = min(dist[j], g[t][j]);
        }
        
    }
    
    return true;
}

int main()
{
    cin >> n >> m;
    memset(g, 0x3f, sizeof g);
    while(m --)
    {
        int a, b, c;
        cin >> a >> b >> c;
        g[a][b] = g[b][a] = min(g[a][b], c);
    }
    
    if(prim()) cout << res; 
    else puts("impossible");
    
    return 0;
}

kruskal

思路:

请添加图片描述

时间复杂度:O(mlogm), n 表示点数,m 表示边数
模版:
int n, m;       // n是点数,m是边数
int p[N];       // 并查集的父节点数组

struct Edge     // 存储边
{
    int a, b, w;

    bool operator< (const Edge &W)const
    {
        return w < W.w;
    }
}edges[M];

int find(int x)     // 并查集核心操作
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int kruskal()
{
    sort(edges, edges + m);

    for (int i = 1; i <= n; i ++ ) p[i] = i;    // 初始化并查集

    int res = 0, cnt = 0;
    for (int i = 0; i < m; i ++ )
    {
        int a = edges[i].a, b = edges[i].b, w = edges[i].w;

        a = find(a), b = find(b);
        if (a != b)     // 如果两个连通块不连通,则将这两个连通块合并
        {
            p[a] = b;
            res += w;
            cnt ++ ;
        }
    }

    if (cnt < n - 1) return INF;
    return res;
}

作者:yxc
链接:https://www.acwing.com/blog/content/405/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

题目1:Kruskal算法求最小生成树
时间复杂度:O(mlogm), n 表示点数,m 表示边数
代码:
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 1e5 + 10, M = 2e5 + 10;

int n, m;
struct Edge
{
    int a, b, w;
    
    bool operator< (const Edge &W)const
    {
        return w < W.w;
    }
    
}edges[M];
int p[N];
int res = 0;

int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}

bool kruskal()
{
    sort(edges, edges + m);
    
    int cnt = 0;;
    for(int i = 0; i < m; i ++)
    {
        int a = edges[i].a, b = edges[i].b, w = edges[i].w;
        if(find(a) != find(b))
        {
            p[find(a)] = find(b);
            res += w;
            cnt ++;
        }
    }
    if(cnt == n - 1) return true;
    return false;
}

int main()
{
    cin >> n >> m;
    
    for(int i = 0; i < m; i ++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        edges[i] = {a, b, c};
    }
    
    for(int i = 1; i <= n; i ++) p[i] = i;
    
    if(kruskal()) cout << res;
    else puts("impossible");
    
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值