zoukankan      html  css  js  c++  java
  • HDU 1301 Jungle Roads

    题解:最小生成树…………

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    struct node{int a,b,l;}seg[900];
    int f[27],m,n,a,b; char c;
    int sf(int x){return f[x]==x?x:f[x]=sf(f[x]);}
    bool cmp(node a,node b){return a.l<b.l;}
    void scanc(int &x){
        while(c=getchar(),c<'A'||c>'Z');x=c-'A';
    }
    void scan(int &x){
        while(c=getchar(),c<'0'||c>'9');x=c-'0';
        while(c=getchar(),c>='0'&&c<='9')x=x*10+c-'0';
    }
    int main(){
        while(~scanf("%d",&n)){
            if(n==0)break;int ans=0,t=0,cnt=n-1;
            for(int i=0;i<=n;i++)f[i]=i;
            for(int i=0;i<n-1;i++){
                scanc(a);scan(m);
                for(int j=0;j<m;j++)seg[t].a=a,scanc(seg[t].b),scan(seg[t++].l);
            }
            sort(seg,seg+t,cmp); int i=-1;
            while(cnt){
                int q=sf(seg[++i].a),w=sf(seg[i].b);
                if(q!=w)cnt--,f[q]=w,ans+=seg[i].l;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    (Lineup the Dominoes筛子)三维状压
    Halloween Costumes 玄学题
    jQuery之动画
    javascript之位置
    javascript之事件
    jQuery之DOM
    jQuery之选择器
    jQuery简介
    javascript之Bom简介
    javascript之DOM操作
  • 原文地址:https://www.cnblogs.com/forever97/p/3603327.html
Copyright © 2011-2022 走看看