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
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.
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 750Sample Output
212.13
题意:有S颗卫星和P个哨所,有卫星的两个哨所之间可以任意通信;否则,一个哨所只能和距离它小于等于D的哨所通信。给出卫星的数量和P个哨所的坐标,求D的最小值。
分析:这是一个最小生成树问题。m个哨所最多用m-1条边即可连起来,而n颗卫星可以代替n-1条边,基于贪心思想,代替的边越长,求得的D就越小。所以可以用一个数组保存加入最小生成树的边的长度,共有n-1条边,把m-1条较长的边代替掉,剩下的边中最长的即为所求。
Kruskal算法
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
ll father[510];
double ans[250100];
struct node//用来保存输入的点的坐标
{
ll x,y;
};
node a[510];
struct node1//用Z来表示两点之间的距离,即边的权值
{
ll x,y;
double z;
};
node1 edge[250100];
bool cmp(node1 aa,node1 bb)//按升序来排列,使卫星替代较长的边
{
return aa.z<bb.z;
}
double dis(ll x1,ll y1,ll x2,ll y2)//计算两点之间的距离
{
double s=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
// printf("%.2f\n",s);
return s;
}
ll find_father(ll b)
{
if(father[b]!=b)
{
father[b]=find_father(father[b]);
}
return father[b];
}
int Union(int x,int y)//合并建边
{
int p1=find_father(x);
int p2=find_father(y);
if(p1==p2)
{
return 0;
}
else
{
father[p2]=p1;
return 1;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ll n,m;
scanf("%lld%lld",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%lld%lld",&a[i].x,&a[i].y);
}
ll ant=0;
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
edge[ant].x=i;
edge[ant].y=j;
edge[ant].z=dis(a[i].x,a[i].y,a[j].x,a[j].y);
ant++;
}
}
sort(edge,edge+ant,cmp);
for(int i=0;i<=m;i++)
{
father[i]=i;
}
ll d=0;
for(int i=0;i<ant;i++)
{
if(Union(edge[i].x,edge[i].y))
{
ans[d]=edge[i].z;
d++;
}
}
printf("%.2f\n",ans[d-n]);
}
return 0;
}