why3AC7RE
  • 板块P2121 拆地毯
  • 楼主xiarui1
  • 当前回复7
  • 已保存回复7
  • 发布时间2024/8/30 19:52
  • 上次更新2024/8/30 22:28:46
查看原帖
why3AC7RE
743593
xiarui1楼主2024/8/30 19:52
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct node{
	int u,v,w;
}a[100010];
bool cmp(node a,node b){
	return a.w>=b.w;
}
int n,m,k,fa[1000010],x,y,s,tot;
int find(int x){
	if(fa[x]==x) return x;
	else fa[x]=find(fa[x]);
}
signed main(){
	cin>>n>>m>>k;
	for(int i=1;i<=m;i++){
		cin>>a[i].u>>a[i].v>>a[i].w;
	}
	for(int i=1;i<=n;i++){
		fa[i]=i;
	}
	sort(a+1,a+1+m,cmp);
	for(int i=1;i<=m;i++){
		x=a[i].u;
		y=a[i].v;
		if(x>y) swap(x,y);
		x=find(x);
		y=find(y);
		if(x!=y){
			fa[x]=y;
			tot++;
			s+=a[i].w;
		}
		if(tot>=k or tot>=n-1) break;
	}
	cout<<s;
	return 0;
}
2024/8/30 19:52
加载中...