TLE+MLE 求调
查看原帖
TLE+MLE 求调
1381752
shizhengyang楼主2025/7/24 17:21
#include<bits/stdc++.h>
using namespace std;

const int N = 500;
struct node{
	int x, y, v;
}; 
vector <node> e;
int a, b, m, ans, fa[N], w[N][N], mk[N];

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

int findfa(int x) {
	if(fa[x] != x) fa[x] = findfa(fa[x]);
	else return fa[x];
}

void f() {
	for(int i = 0; i <= b; i ++) {
		fa[i] = i;
	}
	sort(e.begin(), e.end(), cmp);
	for(int i = 0; i < m; i ++) {
		int x = e[i].x, y = e[i].y, v = e[i].v;
		int fx = findfa(x);
		int fy = findfa(y);
		if(fx != fy) {
			fa[fx] = fy;
			ans += v;
		}
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> a >> b;
	int c;
	for(int i = 1; i <= b; i ++) {
		m ++;
		e.push_back((node){0, i, a});
	}
	for(int i = 1; i <= b; i ++) {
		for(int j = 1; j <= b; j ++) {
			cin >> w[i][j];
			if(j > i && w[i][j]) {
				m ++;
				e.push_back((node){i, j, w[i][j]});
			}	
		}
	}
	f();
	int cnt = 0;
	cout << ans + cnt;
	return 0;
}

2025/7/24 17:21
加载中...