70蒟蒻求调
  • 板块P2121 拆地毯
  • 楼主YQWQ
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/4/26 18:47
  • 上次更新2023/10/23 17:29:25
查看原帖
70蒟蒻求调
490974
YQWQ楼主2023/4/26 18:47
#include <bits/stdc++.h>
using namespace std;

const int Kmax=1e5+5;

int n,m,k,f[Kmax],t,ans;

struct E{
	int u,v,w;
}s[Kmax];

bool C(E a,E b){
	return a.w>b.w;
}

int find(int x){
    if(f[x]!=x)
        f[x]=find(f[x]);
    return f[x];
}

int main(){
	cin>>n>>m>>k;
	for(int i=1;i<=m;i++){
		cin>>s[i].u>>s[i].v>>s[i].w;
	}
	sort(s+1,s+1+m,C);
	for(int i=1;i<=n;i++){
		f[i]=i;
	}
	for(int i=1;i<=m&&t<k;i++){
		int l=find(s[i].u),r=find(s[i].v);
		if(l!=r){
			f[i]=r;
			t++;
			ans+=s[i].w;
		}
	}
	cout<<ans;
}
2023/4/26 18:47
加载中...