事实上就是求最小生成树中第m-(s-1)条最长的边。
在有前哨已经拥有卫星频道的情况下,如果某一个前哨使用卫星频道那么意味着删掉与该点关联的最长的一条边。用s个频道可以删掉(s-1)条最长边,所以答案就是第m-(s-1)条最长的边。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define MAXN 505
using namespace std;
struct Edge
{
int a,b;
double weight;
};
struct Point
{
int x,y;
};
int father[MAXN];
int find(int p)
{
return p==father[p]?p:(father[p]=find(father[p]));
}
double Dist(Point a,Point b)
{
double ll=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
return sqrt(ll);
}
bool cmp(Edge a,Edge b)
{
return a.weight<b.weight;
}
Edge e[MAXN*MAXN];
Point po[MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int s,p;
scanf("%d%d",&s,&p);
for(int i=0; i<p; ++i)
scanf("%d%d",&po[i].x,&po[i].y);
int N=0,cou=0;
for(int i=0; i<p; ++i)
for(int j=i+1; j<p; ++j)
{
e[N].a=i;
e[N].b=j;
e[N++].weight=Dist(po[i],po[j]);
}
sort(e,e+N,cmp);
for(int i=0; i<=p; ++i)
father[i]=i;
int _id[MAXN]= {0};
for(int i=0; i<N; ++i)
{
int ta=find(e[i].a),tb=find(e[i].b);
if(ta!=tb)
{
father[ta]=tb;
_id[cou++]=i;
}
}
printf("%.2lf\n",e[_id[cou-s]].weight);
}
return 0;
}