题意:S个点,P个卫星,每个点要么放卫星,要么放电台,使所有点连通,两个卫星之间可以连无限远,但两个电台之间只能在D距离内有效,求最小的D(1 <= S <= 100,S < P <= 500)。
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22166
——>>生成最小生成树后,一个问题,理解题意:
卫星不放在两点之间,而是放在点上,所以S个卫星能连通S个点,卫星之间有S(S-1)/2条信道,但对点来说只是连通了S-1条边,而不是S条边。
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
const int maxn = 500 + 10;
int N, S, P, fa[maxn];
double ret[maxn];
struct Point{
double x;
double y;
}p[maxn];
double Dis(Point A, Point B){
return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}
struct node{
int u;
int v;
double dis;
bool operator < (const node& e) const{
return dis > e.dis;
}
};
void init(){
for(int i = 1; i <= P; i++) fa[i] = i;
}
int Find(int x){
return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
bool Union(int x, int y){
int newx = Find(x);
int newy = Find(y);
if(newx == newy) return 0;
fa[newy] = newx;
return 1;
}
void read(){
scanf("%d%d", &S, &P);
init();
for(int i = 1; i <= P; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
}
void Kruscal(){
priority_queue<node> pq;
for(int i = 1; i <= P; i++)
for(int j = i+1; j <= P; j++)
pq.push((node){i, j, Dis(p[i], p[j])});
int m = 0;
while(!pq.empty()){
node no = pq.top(); pq.pop();
if(Union(no.u, no.v)) ret[m++] = no.dis;
}
printf("%.2f\n", ret[m-S]);
}
int main()
{
scanf("%d", &N);
while(N--){
read();
Kruscal();
}
return 0;
}