POJ 1258 Agri-Net (kruskal算法)

题目链接http://poj.org/problem?id=1258

使用快排将保存有一条线段两个端点和长度的结构体按长度进行排序,然后使用并查集查找两个端点是否已经被查找过,相比prim算法,kruskal算法生成最小树要好理解一点,简单来说,就是排序+并查集的应用来保证所有的点连接,并使权值之和最小


<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct map
{
    int s,e,lenth;
}node[10010];
int n;
int nodeSet[110];
int top;

int cmp(const void *a ,const void *b)
{
    struct map *p1 = (struct map *)a;
    struct map *p2 = (struct map *)b;

    return p1->lenth - p2->lenth;
}

int find(int x)
{
    if(x==nodeSet[x])
        return x;
    else
        return find(nodeSet[x]);
}

int kruskal()
{
    int i;
    int sum = 0;
    for(i=1;i<=top;i++)
    {
        int a = find(node[i].s);
        int b = find(node[i].e);

        if(a != b)
        {
            nodeSet[a]=b;

            sum+=node[i].lenth;
        }
    }
    return sum;
}

int main()
{
    while(~scanf("%d",&n))
    {
        int i,j;
        top=0;
        for(i=1; i<=n; i++)
        {
            nodeSet[i]=i;
            for(j=1; j<=n; j++)
            {
                scanf("%d",&node[top].lenth);
                node[top].s=i;
                node[top].e=j;
                top++;
            }
        }

      //  printf("%d\n",top);

        qsort(node,top,sizeof(node[0]),cmp);

        /*for(i=0;i<top;i++)
        {
            printf("%d ",node[i].lenth);
        }*/
        int result = kruskal();

       printf("%d\n",result);

    }
    return 0;
}</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值