zoukankan      html  css  js  c++  java
  • POJ1258最小生成树简单题

    题意:
          给你个图,让你求一颗最小生成树。
    思路:
         裸题,克鲁斯卡尔或者普利姆都行。


    #include<stdio.h>
    #include<algorithm>


    using namespace std;


    typedef struct
    {
        int a ,b ,c;
    }NODE;


    NODE node[100*100+10];
    int mer[105];


    bool camp(NODE a ,NODE b)
    {
        return a.c < b.c;
    }


    int finds(int x)
    {
        return x == mer[x] ? x : mer[x] = finds(mer[x]);
    }


    int main ()
    {
        int n ,i ,j ,ans;
        while(~scanf("%d" ,&n))
        {
            int nowid = 0;
            for(i = 1 ;i <= n ;i ++)
            {
                for(j = 1 ;j <= n ;j ++)
                {
                    nowid++;
                    scanf("%d" ,&node[nowid].c);
                    node[nowid].a = i ,node[nowid].b = j;
                }
                mer[i] = i;
            }
            sort(node + 1 ,node + nowid + 1 ,camp);
            ans = 0;
            for(i = 1 ;i <= nowid ;i ++)
            {
                int x = finds(node[i].a);
                int y = finds(node[i].b);
                if(x == y) continue;
                ans += node[i].c;
                mer[x] = y;
            }
            printf("%d " ,ans);
        }
        return 0;


    }

  • 相关阅读:
    python主成分分析
    matplotlib绘图pie
    cpu监控:mpstat命令
    cpu监控:dstat
    MongoDB--安装部署
    Linux-网络管理
    Apache 虚拟主机配置
    Apache 访问控制
    Apache 域名跳转配置
    Apache 日志管理
  • 原文地址:https://www.cnblogs.com/csnd/p/12062516.html
Copyright © 2011-2022 走看看