84分求助
查看原帖
84分求助
1333393
zp20120123楼主2024/7/27 13:30

orz无敌了

附上错误代码

//kruskal
//最小生成树
//small
//sort


#include<bits/stdc++.h>
using namespace std;

int fa[5005];

struct node{
	int u, v, w;
}a[200005];

bool cmp(node x, node y){
	return x.w < y.w;
}

int find(int x){
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}

void merge(int x, int y){
	fa[find(x)] = find(y);
}

inline int read(){
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
signed main(){
	int n = read(), m = read();
	for(int i = 0; i < m; i++){
		cin >> a[i].u >> a[i].v >> a[i].w;
	} 
	sort(a, a + m, cmp);
	for(int i = 0; i <n; i++){
		fa[i] = i;
	}
	int tot = 0;
	for(int i = 0; i < m; i++){
		if(find(a[i].u) != find(a[i].v)){
			merge(a[i].u, a[i].v);
			tot += a[i].w;
		}
	}
	cout << tot;
	return 0;
 } 

我知道要输出orzorz,but,我不知道怎么输出

@Laugh_at_the_sky

2024/7/27 13:30
加载中...