其他分享
首页 > 其他分享> > 最小生成树

最小生成树

作者:互联网

最小生成树

最小生成树的算法有两种

image

我们一般遇到稠密图都会用到朴素版Prim,稀疏图都会用Kruskal

因为这样代码最短

朴素版Prim算法

Prim根dijkstra长得特别像

image

算法实现:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 510, INF = 0x3f3f3f3f;

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

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;

        // 如果没有找到最小点,那么直接return INF
        if (i && dist[t] == INF) return INF;

        // 先加上边权,防止有自环是负的情况
        if (i) res += dist[t];

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

        st[t] = true;
    }

    return res;
}

int main()
{
    scanf("%d%d", &n, &m);

    memset(g, 0x3f, sizeof g);

    while (m -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        g[a][b] = g[b][a] = min(g[a][b], c);
    }

    int t = prim();

    if (t == INF) printf("impossible\n");
    else printf("%d\n", t);

    return 0;
}

堆优化版Prim就是将dist数组简称堆,查询最小值就是O(1)的,插入是logn的

Kruskal算法

image

这个第二步可以用并查集来优化

算法实现:

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 200010;

int n, m;
int p[N];

struct Edge
{
    int a, b, w;

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

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

int main()
{
    scanf("%d%d", &n, &m);

    for (int i = 0; i < m; i ++ )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        edges[i].a = a, edges[i].b = b, edges[i].w = c;
    }

    for (int i = 1; i <= n; i ++ ) p[i] = i;

    sort(edges, edges + m);

    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) printf("impossible\n");
    else printf("%d\n", res);

    return 0;
}

标签:return,int,最小,生成,edges,dist,include,const
来源: https://www.cnblogs.com/zyrddd/p/16637539.html