要加判orz在哪里加怎么判啊
查看原帖
要加判orz在哪里加怎么判啊
1306142
Eddy1341楼主2024/8/19 21:14
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int cnt,fa[5005];
long long ans;
struct edge{
	int st,to;
	long long p;
}E[200005];
int findfa(int x){
	if(x==fa[x])return x;
	return fa[x]=findfa(fa[x]);
}
bool cmp(edge a,edge b){
	return a.p<b.p;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)fa[i]=i;
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&E[i].st,&E[i].to,&E[i].p);
	}
	sort(E+1,E+1+m,cmp);
	for(int i=1;i<=m;i++){
		int x=findfa(E[i].st),y=findfa(E[i].to);
		if(x==y)continue;
		ans+=E[i].p;
		fa[x]=y;
                if(++cnt==n-1)break;
	}
	printf("%lld",ans);
	return 0;
}

不想另外跑深搜qwq

2024/8/19 21:14
加载中...