题目链接:http://poj.org/problem?id=2349
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.
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).
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.
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条边,结果计算样例的结果是200,其实是卫星信道的个数,换句换说每两个卫星信道才可以消去一条边。 处
理每两点之间的边,使用克鲁斯卡尔算法一条一条的加入,直至加入的这条边是第p-s条边时结束。因为一共需要p-1条边而使用s个卫星接收
信道可以使s-1条边免费,所以只需p-1-(s-1)即p-s条边即可。
*/
#include<cstdio>
#include<math.h>
#include<algorithm> using namespace std;
struct NODE{
int x,y;
}node[]; struct EDGE{
int u,v;
double w;
}edge[]; int cmp(struct EDGE a,struct EDGE b){
return a.w<b.w;
}
int f[];
int merge(int v,int u);
int getf(int v);
int main()
{
int t,s,p,i,j,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&p);
for(i=;i<=p;i++){
scanf("%d%d",&node[i].x,&node[i].y);
} k=;
for(i=;i<=p-;i++){
for(j=i+;j<=p;j++){
edge[k].u=i;
edge[k].v=j;
edge[k].w=sqrt(1.0*(node[i].x-node[j].x)*(node[i].x-node[j].x) +
1.0*(node[i].y-node[j].y)*(node[i].y-node[j].y));
}
}
sort(edge,edge+k,cmp); /*for(i=0;i<k;i++)
printf("%lf\n",edge[i].w);*/ for(i=;i<=p;i++)
f[i]=i; int cou=;
for(i=;i<k;i++){
if(merge(edge[i].u,edge[i].v)){
//printf("选择了第%d这条边%.2lf\n",i,edge[i].w);
cou++;
}
if(cou == p-s){
printf("%.2lf\n",edge[i].w);
break;
}
}
}
return ;
} int getf(int v)
{
return f[v]==v?v:f[v]=getf(f[v]);
} int merge(int v,int u)
{
int t1,t2;
t1=getf(v);
t2=getf(u);
if(t1 != t2){
f[t2]=t1;
return ;
}
return ;
}