最小生成树

最小生成树 - 图1

朴素版Prim算法

算法描述

  1. 所有距离初始化为正无穷 dist[i] <—— +oo
  2. n次迭代 for (int i = 0; i < n; i ++)
    1. 找到集合外距离最近的点赋值给t
    2. 用t更新其他点到集合的距离
    3. 把t加到集合中去 st[ti] = true

      Prim算法求最小生成树

      给定一个最小生成树 - 图2个顶点最小生成树 - 图3条边的无向图,图中可能存在重边和自环,边权可能为负数。
      求最小生成树的边权重之和,如果最小生成树不存在则输出impossible。
      给定一张带权的无向图最小生成树 - 图4,其中最小生成树 - 图5表示图中点的集合,最小生成树 - 图6表示图中边的集合,最小生成树 - 图7, 最小生成树 - 图8
      最小生成树 - 图9中全部最小生成树 - 图10个顶点和最小生成树 - 图11最小生成树 - 图12条边构成的无向连通子图被称为最小生成树 - 图13的一棵生成树,其中边权值之和最小的生成树被称为无向图最小生成树 - 图14的最小生成树。
      输入格式
      第一行包含两个整数最小生成树 - 图15最小生成树 - 图16
      接下来最小生成树 - 图17行,每行包含三个整数最小生成树 - 图18,表示点最小生成树 - 图19和点最小生成树 - 图20之间存在一条权值为最小生成树 - 图21的边。
      输出格式
      一行,若存在最小生成树,则输出一个整数,表示最小生成树的数边权重之和,如果最小生成树不存在则输出impossible。
      数据范围
      最小生成树 - 图22,
      最小生成树 - 图23,
      图中涉及边的边权的绝对值不超过10000。
      输入样例
      1. 4 5
      2. 1 2 1
      3. 1 3 2
      4. 1 4 3
      5. 2 3 2
      6. 3 4 4
      输出样例
      1. 6
      代码 ```cpp

      include

      include

      include

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);

  1. int res = 0; //存放权值之和
  2. for (int i = 0; i < n; i ++) {
  3. int t = -1;
  4. for (int j = 1; j <= n; j ++)
  5. if (!st[j] && (t == -1 || dist[t] > dist[j]))
  6. t = j;
  7. //尽早跳出循环防止TLE
  8. if (i && dist[t] == INF) return INF;
  9. //需要先累加再更新 防止自环加入的情况
  10. if (i) res += dist[t];
  11. st[t] = true;
  12. for (int j = 1; j <= n; j ++) dist[j] = min(dist[j], g[t][j]);
  13. }
  14. 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) puts("impossible");
else printf("%d\n", t);

return 0;

}

<a name="Dut3t"></a>
### Kruskal算法
<a name="Fmj3t"></a>
#### 算法描述

1. 将所有边按权重从小到大顺序排序。时间复杂度O(mlogn)。
1. 枚举每条边a—>b,权重是c。
   1. 如果a b不连通,将这条边加入集合中。
   1. 反之舍去。
<a name="UU4uF"></a>
#### [Kruskal算法求最小生成树](https://www.acwing.com/problem/content/861/)
题目描述同上一题,仅数据范围不同。<br />**数据范围**<br />![](https://cdn.nlark.com/yuque/__latex/90ab8358cbba19bcafa95319658d494d.svg#card=math&code=1%5Cleq%20n%20%5Cleq%2010%5E5&id=JqGRR),<br />![](https://cdn.nlark.com/yuque/__latex/0543481da6662c83bb898dd50b497639.svg#card=math&code=1%20%5Cleq%20m%20%5Cleq%202%20%2A%2010%5E5&id=ToUlQ),<br />图中涉及边的边权的绝对值不超过1000。<br />**代码**
```cpp
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010, M = 200010, INF = 0x3f3f3f3f;

int 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;
}

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

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

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

    return 0;
}

参考

搜索与图论(三)