通不过
查看原帖
通不过
334100
eternal79楼主2020/5/5 12:41
#include<bits/stdc++.h>
using namespace std;
int n,m,total=0;
struct edge {
	int start, to;
	long long val;
}bian[20200505];
int find_union[100000];
long long ans;

int find(int x) {//并查集部分
	if (find_union[x] == x)
		return x;
	else {
		find_union[x] = find(find_union[x]);
		return find_union[x];
	}
}
bool cmp(edge x, edge y) {
	return x.val < y.val;
}

void kruscal() {
	sort(bian, bian + m, cmp);
	for (int i=1; i<=m; i++) {
		int u = find(bian[i].start);
		int v = find(bian[i].to);
		if (u == v)continue;
		else {
			ans += bian[i].val;
			find_union[u] = v;
			total++;
			if (total == n - 1)
				break;
		}
	}
}


int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		find_union[i] = i;
	}
	for (int i = 1; i <= m; i++) {
		scanf("%d%d%d", &bian[i].start, &bian[i].to, &bian[i].val);
	}
	sort(bian + 1, bian + 1 + m, cmp);
	kruscal();
	return 0;
}

可以帮我看看哪里错了吗

2020/5/5 12:41
加载中...