poj 1251 Jungle Roads MST

简介:

   水

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define INF 1E9
using namespace std;
int fa[30];
int far(int nn)
{
    if(fa[nn]<0)return nn;
    return fa[nn]=far(fa[nn]);
}
struct edge
{
    int v,a,b;
};
edge e[80];
bool cmp(edge a,edge b)
{
    return a.v<b.v;
}
int main()
{
    int n,i,j,m,v,a,b;
    char t;
    while(~scanf("%d",&n)&&n)
    {
        int K=0;
        memset(fa,-1,sizeof(fa));
        for(i=0;i<n-1;i++)
        {
            scanf(" %*c%d",&m);
            for(j=0;j<m;j++)
            {
               scanf(" %c%d",&t,&v);
               e[K++]=edge{v,i,t-'A'};
            }
        }
        sort(e,e+K,cmp);
        int ans=0;
        for(i=1,j=0;i<n;j++)
        {
            a=far(e[j].a);
            b=far(e[j].b);
            if(a==b)continue;
            fa[b]=a;ans+=e[j].v;
            i++;
        }
        printf("%d\n",ans);
    }
}


目录
相关文章
|
1月前
Jungle Roads(最小生成树)
Jungle Roads(最小生成树)
|
机器学习/深度学习 人工智能 BI
codeforces-1242-B 0-1 MST
B. 0-1 MST time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out.
146 0
codeforces-1242-B 0-1 MST
【POJ 1679 The Unique MST】最小生成树
无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和。 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树。(并不一定是最小生成树) 分析贪心策略求最小生成树的过程(贪心地选最短的边来扩充已加入生成树的顶点集合U),发现只有当出现“U中两个不同的点到V-U中同一点的距离同时为当前最短边”时,才会出现“异构”的最小生成树。
1217 0