40分,求助!
查看原帖
40分,求助!
1383345
hqs100604楼主2024/7/24 16:29
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int a[N],b[N],f[N],s,p,n,m;
double ans;
struct Node{
	int x,y;
	double z;
};
Node st[N];
int find(int x){
	if(x!=f[x]) f[x]=find(f[x]);
	return f[x];
}
bool cmp(Node a,Node b){
	return a.z<b.z;
}
int main(){
	cin>>s>>p;
	for(int i=1;i<=p;i++){
		cin>>a[i]>>b[i];
	}
	for(int i=1;i<=p;i++) f[i]=i;
	for(int i=1;i<=p;i++)
		for(int j=1;j<i;j++){
			n++;
			st[n].z=sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]));
			st[n].y=j;
			st[n].x=i;
		}
	sort(st+1,st+n+1,cmp);
	for(int i=1;i<=n;i++){
		if(find(st[i].x)!=find(st[i].y)){
			int x,y;
			x=find(st[i].x);
			y=find(st[i].y);
			f[st[i].x]=st[i].y;
			ans=st[i].z;
			m++;
		}
		if(m>=p-s){
			cout<<fixed<<setprecision(2)<<ans;
			return 0;
		}
	}
	return 0;
}
2024/7/24 16:29
加载中...