79pts求助
查看原帖
79pts求助
757946
gaolangwen_is_sb楼主2022/12/21 17:46

rt

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define inf_7 0x7f7f7f7f
#define inf_c 0xcfcfcfcf
#define inf_8 0x8f8f8f8f
#define LL long long
#define uLL unsigned long long
//#define int long long

struct Node
{
	int x,y,z;
}e[10005];
int n,m,num[5005],tot;
uLL ans;

bool cmp(Node x,Node y)
{
	return x.z<y.z;
}

int Find(int x)
{
	if(num[x]==x)
		return x;
	return num[x]=Find(num[x]);
}

void bingchaji()
{
	for(int i=1;i<=n;i++)
		num[i]=i;
	for(int i=1;i<=m;i++)
	{
		int tmpx=Find(e[i].x),tmpy=Find(e[i].y);
		if(tmpx==tmpy)
			continue;
		num[tmpy]=tmpx;
		ans+=e[i].z;
		tot++;
		if(tot==n-1)
			break;
	}
	return ;
}

signed main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
		cin>>e[i].x>>e[i].y>>e[i].z;
	sort(e+1,e+1+m,cmp);
	bingchaji();
	if(tot==n-1)
		cout<<ans;
	else
		cout<<"orz";
	return 0;
}

求助

2022/12/21 17:46
加载中...