40分求助
查看原帖
40分求助
362167
b1468821672楼主2023/7/15 10:27
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
const int N=1e7;
int fa[N],head[N];
int S,P,cnt,k;
double ans;
struct edge
{
	int x,y;
	double w;
}e[N];
int find(int x)
{
	if(fa[x]!=x)fa[x]=find(fa[x]);
	return x;
}
struct point
{
	double x,y;
}p[N];
bool cmp(edge x,edge y)
{
	return x.w<y.w;
}
int main()
{
	cin>>S>>P;
	for(int i=1;i<=P;i++)
	{
		cin>>p[i].x>>p[i].y;
		for(int j=1;j<i;j++)
		{
			e[++cnt].w=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));
			e[cnt].x=i;
		    e[cnt].y=j;
		}
	}
	for(int i=1;i<=P;i++)fa[i]=i;
	sort(e+1,e+1+cnt,cmp);
	for(int i=1;i<=cnt;i++)
	{
		if(find(e[i].x)!=find(e[i].y))
		{
			int r1=find(e[i].x);
			int r2=find(e[i].y);
			fa[r1]=r2;
    		k++;ans=e[i].w; 
    		if(k>=P-S)
     		{
     			cout<<fixed<<setprecision(2)<<ans;
    			return 0;
	    	}
		}
	}
}
2023/7/15 10:27
加载中...