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;


    }

  • 相关阅读:
    Smith Numbers POJ
    HDU
    dp HDU
    POJ
    HDU
    LOOPS HDU
    水题,P1789 【Mc生存】插火把 (暴力即可)
    LOOPS
    Coprime (单色三角形+莫比乌斯反演(数论容斥))
    莫比乌斯函数 51nod-1240(合数分解试除法)
  • 原文地址:https://www.cnblogs.com/csnd/p/12062517.html
Copyright © 2011-2022 走看看