最小生成树,已经开通s条。用并查集实现
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
const int N=502;
int father[N],x[N],y[N],cnt;
struct node{
int x,y;
double d;
}e[N*N];
int find(int v){
if(father[v]!=v)
father[v]=find(father[v]);
return father[v];
}
double calc(int i,int j){
double rst=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
return sqrt(rst);
}
int cmp(node a,node b){
return a.d<b.d;
}
int main(){
int tt,s,p;
for(cin>>tt;tt>0;tt--){//s个点有
cin>>s>>p;
for(int i=0;i<=p;i++)
father[i]=i;
int u,v;
for(int i=1;i<=p;i++)
scanf("%d %d",&x[i],&y[i]);
cnt=0;
for(int i=1;i<=p;i++)
for(int j=i+1;j<=p;j++)
e[cnt].x=i,e[cnt].y=j,e[cnt++].d=calc(i,j);
sort(e,e+cnt,cmp);
int tot=p-1-s;
double ans=0;
for(int i=0;i<cnt;i++){
u=find(e[i].x),v=find(e[i].y);
//printf("%.2lf--%d %d\n",e[i].d,u,v);
if(u!=v){
father[u]=v;
ans=max(ans,e[i].d);
if(tot--==0)
break;
}
}
printf("%.2lf\n",ans);
}
return 0;
}