problem
solution
codes
#include<iostream>
#include<algorithm>
using namespace std;
struct side{ int u, v, w; }e[10010];
bool cmp(const side &a, const side &b){ return a.w<b.w; }
int fa[10010];
int find(int x){ return x==fa[x] ? x: fa[x]=find(fa[x]);}
int main(){
int n, m, k; cin>>n>>m>>k;
for(int i = 0; i < m; i++)cin>>e[i].u>>e[i].v>>e[i].w;
sort(e, e+m, cmp);
int cc = n-k, ans = 0;
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = 0; i < m; i++){
if(find(e[i].u) != find(e[i].v)){
ans += e[i].w;
fa[find(e[i].u)] = find(e[i].v);
cc--;
}
if(!cc)break;
}
cout<<ans;
return 0;
}