题目链接:http://poj.org/problem?id=2349
题意:有s颗星球,有p个雷达站,如果雷达站在同一个星球上,不需要距离就可以直接通信,如果不在一个星球上,他们之间的距离不能超过D,现在给你p个雷达站的坐标,如果要把这整个雷达站连通,问你最小的D是多少
解析:首先要联通全部雷达站,肯定是最小生成树,然后让你求一个D值,雷达站之间连边,相当于如果距离大于D就不能相连,那么可以先对他做最小生成树,共p-1条边,因为在一个星球上的雷达站默认是相连的,那么也就可以在去掉s-1(因为一定有一个雷达站在一个星球上)条边,然后在取一下剩下的边权的最大值
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 1e6+100;
struct node
{
int u,v;
double c;
node() {}
node(int _u,int _v,double _c)
{
u = _u;
v = _v;
c = _c;
}
bool operator < (const node &b)const
{
return c<b.c;
}
}a[maxn];
int fa[maxn];
double d[maxn];
double x[maxn],y[maxn];
double dis(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int getfa(int x)
{
if(fa[x]==x)
return fa[x];
return fa[x] = getfa(fa[x]);
}
int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
int s,n;
scanf("%d %d",&s,&n);
for(int i=1;i<=n;i++)
{
scanf("%lf %lf",&x[i],&y[i]);
fa[i] = i;
}
int cnt1 = 0,cnt2 = 0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
a[cnt1++] = node(i,j,dis(x[i],y[i],x[j],y[j]));
}
sort(a,a+cnt1);
for(int i=0;i<cnt1;i++)
{
int t1 = getfa(a[i].u);
int t2 = getfa(a[i].v);
if(t1!=t2)
{
fa[t1] = t2;
d[cnt2++] = a[i].c;
}
}
double ans = 0;
for(int i=0;i<cnt2-s+1;i++)
ans = max(ans,d[i]);
printf("%.2f\n",ans);
}
return 0;
}