https://www.luogu.org/problemnew/show/P1195
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int MAXN=5005,MAXM=400005;
int n,m,k,fa[MAXN],cnt,num,ans;
struct cyq{
int u,v,w;
}a[MAXM];
int get(){
char cc=getchar();
int res=0;
while (cc<'0'||cc>'9') cc=getchar();
while (cc>='0'&&cc<='9'){
res=(res<<3)+(res<<1)+cc-'0';
cc=getchar();
}
return res;
}
bool cmp(cyq aa,cyq bb){
return aa.w<bb.w;
}
int find(int x){
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
int main(){
int x,y,w;n=get();m=get();k=get();
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++){
x=get();y=get();w=get();
a[++cnt].u=x;a[cnt].v=y;a[cnt].w=w;
}
sort(a+1,a+1+cnt,cmp);num=1;k=n-k+1;
while(--k){
if(num>cnt) {printf("No Answer\n");return 0;}
while(find(a[num].u)==find(a[num].v) && num<=m) ++num;
fa[find(a[num].u)]=a[num].v;ans+=a[num].w;num++;
}
printf("%d\n",ans);
return 0;
}