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;


}

原文地址:https://www.cnblogs.com/csnd/p/12062517.html