代码求调
查看原帖
代码求调
341353
封禁用户楼主2022/8/31 10:15

Rt,63分。

#8#9#10 RE,#13WA。

#13可能是特判的问题,8、9、10显示数组越界,可是开大多少都没用,并且是在题目合理范围之内的。

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+10;
long long ans = 0, p, z;
struct IAKIOI{ int x, y; } a[N];
int n, m, x, y;
int t, cnt = 0;
int f[N];

struct EDGE{
	int from, to;
	double w;
} e[N];

inline int getfa(int x){
	if (f[x] == x) return x;
	return f[x] = getfa(f[x]);
}

double dist(int x, int y){
	return (double)sqrt((double)(a[x].x - a[y].x) *
	                            (a[x].x - a[y].x) +
						(double)(a[x].y - a[y].y) *
						        (a[x].y - a[y].y));
} 

void add(int i, int j, double k){ e[++ cnt].from = i, e[cnt].to = j, e[cnt].w = k; }

bool cmp(EDGE a, EDGE b){
	if (a.w == b.w) return a.from < b.from;
	return a.w < b.w;
}

int main(){
	scanf("%d %d", &n, &m);
	if (n == 1){
	    printf("orz\n");
	    return 0;
	}
	for (int i = 1; i <= n; i ++) f[i] = i;
	for (int i = 1; i <= m; i ++) scanf("%d %d %d", &x, &y, &z), add(x, y, z);
	
	
	sort(e + 1, e + cnt + 1, cmp);
	
	for (int i = 1; i <= n; i ++)
		for (int j = i + 1; j <= n; j ++) p = dist(i, j), add(i, j, p);
	
	for (int i = 1; i <= cnt; i ++){
		x = getfa(e[i].from), y = getfa(e[i].to);
		if (x != y) f[x] = y, t ++, ans += e[i].w;
		if (t + 1 == n) break;
	}
	if (!ans){
		printf("orz\n");
		return 0;
	}
	printf("%d\n", ans);
	return 0;
}

qwq

2022/8/31 10:15
加载中...