zoukankan      html  css  js  c++  java
  • P1195 口袋的天空

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    #define tcl(a,b,c) for(a=b;a<=c;a++)
    const int maxx=20001;
    struct data
    {
        int x,y,dis;
    }a[100001];
    int f[maxx];
    int getf(int x)
    {
        if(f[x]==x) return x;
        else f[x]=getf(f[x]);
        return f[x];
    }
    bool cmp(data a,data b)
    {
        return a.dis<b.dis;
    }
    int n,m,k,sum,ans;
    int main()
    {
        int i;
        cin>>n>>m>>k;
        tcl(i,1,n)
        {
            f[i]=i;
        }
        tcl(i,1,m)
        {
            cin>>a[i].x>>a[i].y>>a[i].dis;
        }
        sort(a+1,a+1+m,cmp);
        tcl(i,1,m)
        {
            if(getf(a[i].x)!=getf(a[i].y))
            {
                f[getf(a[i].x)]=getf(a[i].y);
                ans+=a[i].dis;
                sum++;
            }
            if(sum==n-k)//K个树已经建成
            {
                cout<<ans<<endl;
                return 0;
            }
        }
        cout<<"No Answer"<<endl;
        return 0;
    }
    
  • 相关阅读:
    js-20170816-Date对象
    js-20170804-Math对象
    js-20170619-string对象
    微信web开发者工具
    前端开发指南
    接口调试工具DHC
    CSS预处理器
    Weex
    Vue.js
    hbuilder
  • 原文地址:https://www.cnblogs.com/LSWorld/p/9733523.html
Copyright © 2011-2022 走看看