CH模拟赛 拆地毯

/*
MST,注意只能加K条边,但是备选是M条边
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define fo(i,l,r) for(int i = l;i <= r;i++)
#define fd(i,l,r) for(int i = r;i >= l;i--)
using namespace std;
const int N = 100050;
ll read(){
    ll x=0,f=1;
    char ch=getchar();
    while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();};
    while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();};
    return x*f;
}
struct edge{
    int u,v;ll w;
}e[N*3];
int n,m,k,cnt,head[N],f[N];ll ans;
bool cmp(edge a,edge b){return a.w > b.w;}
int findf(int x) {return f[x] == x ? f[x] : f[x] = findf(f[x]);}
int main(){
    freopen("carpet.in","r",stdin);
    freopen("carpet.out","w",stdout);
    n =read();m=read();k=read();
    int u,v;ll w;
    fo(i,1,m){
        e[i].u=read();e[i].v=read();e[i].w=read();
    }
    sort(e+1,e+1+m,cmp);
    fo(i,1,n) f[i] = i;
    int fa,fb;
    fo(i,1,m){
        fa = findf(e[i].u);fb=findf(e[i].v);
        if(fa != fb){
            f[fa] = fb,ans += e[i].w;
            k--;
        }
        if(!k) break;
    }
    cout<<ans;
    return 0;
} 

 

posted @ 2016-11-06 16:06  ACforever  阅读(158)  评论(0编辑  收藏  举报