hdu 1879 继续畅通工程

.........................................................................................................................................................................................................

简单题。

.........................................................................................................................................................................................................



#include<stdio.h>

#include<algorithm>


using namespace std;
typedef struct node
{
    int a,b;
    int c;
    int d;
}edge;


edge e[5000];
int father[105];


bool cmp(edge o1,edge o2)
{
    if(o1.c<o2.c)
    {
        return 1;
    }
    return 0;
}


int Find_Set(int x)
{
    return x!=father[x]?father[x]=Find_Set(father[x]):x;
}


int kruskal(int n)
{
    int sum=0;
    int i,x,y;
    for(i=0;i<n;i++)
    {
        x=e[i].a;
        y=e[i].b;
        x=Find_Set(x);
        y=Find_Set(y);
        if(x!=y)
        {
            father[x]=y;
            sum+=e[i].c;
        }
    }
    return sum;
}


int main()
{
    int n,i,j;
    int sum;
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(i=0;i<n*(n-1)/2;i++)
        {
            scanf("%d%d%d%d",&e[i].a,&e[i].b,&e[i].c,&e[i].d);
            if(e[i].d==1)
            e[i].c=0;
        }
        for(i=0;i<=n;i++)
        {
            father[i]=i;
        }
        sort(e,e+n*(n-1)/2,cmp);
        sum=kruskal(n*(n-1)/2);
        printf("%d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值