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
题意:
n个前哨,s个卫星频道。任意两个前哨可以通过卫星频道或者无线电来通信,前者无视距离,后者最大距离为d,与无线电装置的能量相关。每个前哨的无线电能量都相同,问在所有前哨都可以直接或间接通信的情况下,最小的d是多少
分析:
显然,此题是最小生成树问题。特殊的是,有s个卫星频道可以无视距离。
将n前哨的最小生成树求出并对n-1条边排序,后s-1条边使用卫星,那么剩余边中最大的边长即为所求的d。
问题也就可以转化为求最小生成树第k大边问题,在本题是,求第n-s大边的问题。
#include<stdio.h>
#include<math.h>
int m,n,book[510];
double sum,e[510][510],dis[510];
double inf = 99999999.0;
void Prim()
{
int i,j,k;
double min,t;
for(i = 1; i <= n; i ++)
{
dis[i] = e[1][i];
book[i] = 0;
}
dis[1] = 0;
book[1] = 1;
for(i = 1; i < n; i ++)
{
min = inf;
for(j = 1; j <= n; j ++)
if(book[j] == 0 && dis[j] < min)
{
min = dis[j];
k = j;
}
book[k] = 1;
for(j = 1; j <= n; j ++)
if(book[j] == 0 && dis[j] > e[k][j])
dis[j] = e[k][j];
}
for(i = 1; i < n; i ++)
for(j = 1; j <= n-i; j ++)
if(dis[j] > dis[j+1])
{
t = dis[j];
dis[j] = dis[j+1];
dis[j+1] = t;
}
}
int main()
{
int i,j,T;
double x[510],y[510];
scanf("%d",&T);
while(T --)
{
sum = 0.0;
scanf("%d%d",&m,&n);
for(i = 1; i <= n; i ++)
for(j = 1; j <= n; j ++)
if(i == j)
e[i][j] = 0.0;
else
e[i][j] = inf;
for(i = 1; i <= n; i ++)
{
scanf("%lf%lf",&x[i],&y[i]);
for(j = 1; j <= n; j ++)
e[i][j] = e[j][i] = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
Prim();
printf("%.2lf\n",dis[n-m+1]);
}
return 0;
}