• 最小生成树(Kruskal算法)


    最小生成树就是一张图能生成的边权最小的树。

     

    方法(Kruskal算法):将所有边权从小到大排序,然后一条一条边检查,如果加入这条边形成了回路,那么不加入树中,否则加入。至于如何判断回路,用并查集维护即可。

    代码:

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<iostream>
     6 #include<algorithm>
     7 #include<string>
     8 #include<vector>
     9 #include<queue>
    10 #include<stack>
    11 using namespace std;
    12 const int MaxN = 1e5;
    13 const int Inf = 1 << 30;
    14 typedef struct
    15 {
    16     int u, v, w;//连接u,v两点权值为w的边
    17 } Power;
    18 
    19 Power edge[MaxN+5];
    20 Power res[MaxN+5];
    21 int father[MaxN+5];
    22 int n, m;//点和边的数量
    23 
    24 bool cmp(Power a, Power b)
    25 {
    26     return a.w < b.w;
    27 }
    28 
    29 int Find(int x)
    30 {
    31     if(father[x] == x) return x;
    32     else return father[x] = Find(father[x]);
    33 }
    34 
    35 int main()
    36 {
    37     scanf("%d %d", &n, &m);
    38     for(int i = 1;i <= n;i++) father[i] = i;
    39     for(int i = 1;i <= m;i++)
    40     {
    41         scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].w);
    42     }
    43     sort(edge + 1, edge + 1 + m, cmp);
    44     int ans = 0, cnt = 0;
    45     for(int i = 1;i <= m;i++)
    46     {
    47         int u = edge[i].u, v = edge[i].v, w = edge[i].w;
    48         if(Find(u) != Find(v))
    49         {
    50             father[Find(u)] = Find(v);
    51             ans += w;
    52             cnt++;
    53             res[cnt].u = u; res[cnt].v = v; res[cnt].w = w;
    54         }
    55     }
    56     printf("%d
    ", ans);
    57     for(int i = 1;i <= cnt;i++)
    58     {
    59         printf("%d %d %d
    ", res[i].u, res[i].v, res[i].w);
    60     }
    61     return 0;
    62 }

     

  • 相关阅读:
    JavaWeb核心编程之(四.1)JSP
    一起来说 Vim 语
    你应该知道的基础 Git 命令
    Git 系列(五):三个 Git 图形化工具
    Git 系列(四):在 Git 中进行版本回退
    Git 系列(三):建立你的第一个 Git 仓库
    Git 系列(二):初步了解 Git
    Git 系列(一):什么是 Git
    JavaWeb核心编程之(三.6)HttpServlet
    多线程:子线程执行完成后通知主线程
  • 原文地址:https://www.cnblogs.com/Lightfall/p/9334807.html
Copyright © 2020-2023  润新知