zoukankan      html  css  js  c++  java
  • 最小生成树

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn = 1e5+10;
    int fa[maxn];

    struct Edge{
        int from;
        int to;
        int w;
    }edge[maxn];

    bool cmp(Edge a,Edge b){
        return a.w < b.w;
    }

    int find(int x){
        return (x == fa[x]) ? x : (fa[x] = find(fa[x]));
    }//找到节点祖先并且压缩路径

    void Merge(int x,int y){
        fa[find(x)] = find(y);
    }//判断两个节点是否为同一个祖先

    int tot;

    void add(int x,int y,int z){
        edge[++tot].from = x;
        edge[tot].to = y;
        edge[tot].w = z;
    }

    int Solve(int n,int m){
        sort(edge+1,edge+1+m,cmp);
        int cnt = 0;
        int ans = 0;
        for(int i = 1;i <= m;++i){
            int x = edge[i].from,y = edge[i].to;
            if(find(x) != find(y)){
                Merge(x,y);
                ans += edge[i].w;
                if(++cnt == n - 1)return ans;//cnt 用来记录已经添加了的边的条数,最后跳出循环表示已经添加了n - 1条边,即树已经完成
            }
        }
        return -1;//找不到最小生成树,即图不联通
    }

    int main(){
        int n;
        scanf("%d",&n);
        for(int i = 1;i <= n;++i)fa[i] = i;
        for(int i = 1;i <= n;++i){
            for(int j = 1;j <= n;++j){
                int dis;
                scanf("%d",&dis);
                add(i,j,dis);
            }
        }
        int ans = Solve(n,n*n);
        printf("%d",ans);
        return 0;
    }
    //感到了最小生成树深深的恶意

    如初见 与初见
  • 相关阅读:
    CSS 内外边距
    CSS 边框
    android chrome this account already exists on your device
    linux s s r client ubuntu kali
    rEFInd 美化 windows
    sogou opensuse
    Ping 虚拟机 超时
    python3 批量自动下载对应用户 github上的项目 或者 starts 的项目
    materialize 样例
    opensuse input methods
  • 原文地址:https://www.cnblogs.com/HISKrrr/p/12318293.html
Copyright © 2011-2022 走看看