有奖求助
查看原帖
有奖求助
553904
yingbowen楼主2022/10/8 00:09
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
long long n,m,cnt,fa[10000005];
double ans;
struct point{
	long long x,y;
}pt[10000005];
struct Edge{
	long long a,b;
	double w;
}ed[10000005];
long long fd(long long x){
	return fa[x]=(fa[x]==x)?x:fd(fa[x]);
}
bool cmp(Edge h,Edge hh){
	if(h.w == hh.w)return h.a < hh.a;
	return h.w < hh.w;
}
double jl(int x,int y){
	return (double)(sqrt((double)(pt[x].x-pt[y].x)*(pt[x].x-pt[y].x)+(double)(pt[x].y-pt[y].y)*(pt[x].y-pt[y].y)));
}
long long read(){
	long long s = 0, w = 1;
	char ch = getchar();
	while(!isdigit(ch)){if(ch == '-') w = -1;ch = getchar();}
	while(isdigit(ch)){s = s * 10 + ch - '0';ch = getchar();}
	return s * w;
}
int main(){
	n = read(),m = read();
	for(long long i = 1;i<=n;i++){
		pt[i].x = read(),pt[i].y = read();
		fa[i] = i;
	}
	for(long long i = 1;i<=m;i++){
		long long h=0,hh=0;
		h = read();
		hh = read();
		cnt++;
		ed[cnt].a = h;
		ed[cnt].b = hh;
		ed[cnt].w = 0.0;
	}
	for(long long i = 1;i<=n;i++){
		for(long long j = i+1;j<=n;j++){
			cnt++;
			ed[cnt].a = i;
			ed[cnt].b = j;
			ed[cnt].w = jl(i,j);
		}
	}
	sort(ed+1,ed+1+cnt,cmp);
	int sum = 0;
	for(long long i = 1;i<=cnt;i++){
		long long sa = ed[i].a,sb = ed[i].b;
		double wl = ed[i].w;
		if(fd(sa) != fd(sb)){
			fa[sa] = sb;
			ans += wl;
			sum++;
		}
		if(sum == n-1)break;
	}
	printf("%.2lf",ans);
	return 0;
}

第一个帮我找出错误者v1元

2022/10/8 00:09
加载中...