hdu 1301

题目:最小生成树裸题。

注意getchar的使用。

 

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int
per[110];
struct
node
{

    char
a,b;
    int
w;
}
s[1000];
bool
cmp(node x,node y)
{

    return
x.w<y.w;
}

void
init()
{

    for
(int i=60;i<100;i++)
    per[i]=i;
}


int
find(int x)
{

    while
(x!=per[x])
    x=per[x];
    return
x;
}

bool
join (int x,int y )
{

    int
fx=find(x);
    int
fy=find(y);
    if
(fx!=fy)
    {

        per[fx]=fy;
        return
true;
    }

    return
false;
}



int
main()
{

    char
a,b,c,d;
    int
e,f,g,n;
    while
(scanf("%d",&n),n)
    {

        init();
        getchar();
        n-=1;
        int
sum=0,k=0;
        while
(n--)
        {

            scanf("%c%d",&a,&e);
            getchar();
            for
(int i=0;i<e;i++)
            {

                scanf("%c%d",&b,&f);
                getchar();
                s[k].a=a;
                s[k].b=b;
                s[k].w=f;
                k++;
            }
        }

        sort(s,s+k,cmp);
        for
(int i=0;i<k;i++)
        {

            if
(join(s[i].a,s[i].b))
            sum+=s[i].w;
        }

        printf("%d\n",sum);
    }

    return
0;   
}

posted @ 2017-09-12 14:55  LMissher  阅读(116)  评论(0)    收藏  举报