Description
The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.
Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.
Input
The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).
Output
For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.
Sample Input
1
2 4
0 100
0 300
0 600
150 750
Sample Output
212.13
题目大意:有S颗卫星和P个哨所,有卫星的两个哨所之间可以任意通信;否则,一个哨所只能和距离它小于等于D的哨所通信。给出卫星的数量和P个哨所的坐标,求D的最小值。
思路:
//最小生成树prim算法
//先找出一个最小最小生成树的边集合,在里面除去最长的边(卫星数个)
//则剩下最大的边就是最大的距离D
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAX = 1000;
const int INF = 0x3f3f3f3f;
//最小生成树prim算法
//先找出一个最小最小生成树的边集合,在里面除去最长的边(卫星数个)
//则剩下最大的边就是最大的距离D
struct A{
double arcs[MAX][MAX];//权值-->距离
int arcnum; //边的数目
bool visit[MAX]; //顶点的访问情况
int vexnum; //顶点的数目
int vex[MAX][2]; //顶点的信息-->坐标
}G;
double d[MAX];//选中集与未选中集的距离
int S;//卫星数
double prim(void){
double min,ans[MAX];
int v;
for(int i=0 ; i<G.vexnum ; i++){
d[i] = G.arcs[0][i];
G.visit[i] = false;//每个点未访问
}
//起始点加入选中集
G.visit[0]=true;
for(int i=0 ; i<G.vexnum-1 ; i++){//再选n-1个定点即选n-1条边
//找出一个最短距离的点
min=INF;
for(int j=0 ; j<G.vexnum ; j++){
if(!G.visit[j] && d[j]<min){
v=j;
min = d[j];
}
}
//已访问
G.visit[v]=true;
ans[i] = min;
//更新选中点集与未选中点集的距离
for(int j=0 ; j<G.vexnum ; j++){
if(!G.visit[j] && G.arcs[v][j]<d[j]){
d[j]= G.arcs[v][j];
}
}
}
sort(ans,ans+G.vexnum-1);
return ans[G.vexnum-1-S];
}
int main(void){
int T;
cin>>T;
while(T--){
cin>>S>>G.vexnum;
//初始化图
for(int i=0 ; i<G.vexnum ; i++){
for(int j=0 ; j<G.vexnum ; j++){
G.arcs[i][j] = (i==j)?0:INF;
}
}
//输入顶点信息
for(int i=0 ; i<G.vexnum ; i++){
cin>>G.vex[i][0]>>G.vex[i][1];
}
//赋权值
for(int i=0 ; i<G.vexnum ; i++){
for(int j=0 ; j<i ; j++){
G.arcs[i][j] = sqrt((G.vex[i][0]-G.vex[j][0])*(G.vex[i][0]-G.vex[j][0])+
(G.vex[i][1]-G.vex[j][1])*(G.vex[i][1]-G.vex[j][1]));
G.arcs[j][i] = G.arcs[i][j];
}
}
printf("%.2f\n",prim());
}
return 0;
}