Problem
The Department of National De-fence (DND) wishes to connect several northernoutpostsbya wire-less network. Two different com-munication technologies are to beused in establishing the network:every outpost will have a radiotransceiver and some outposts willin addition have a satellite channel.Any two outposts with a satel-lite channelcan communicate viathe satellite, regardless of their lo-cation. Otherwise, two outpostscan communicate by radio only ifthe distance between them does notexceed D, which depends of thepower of the transceivers. Higherpower yields higher D but costsmore. Due to purchasing andmaintenance considerations, thetransceivers at the outposts mustbe identical; that is, the value of Dis the same for every pair of out-posts.Your job is to determine theminimum D required for thetransceivers. There must be atleast one communication path (di-rect or indirect) between every pairof outposts.
题目大意:
南极有n个科研站,要用卫星或无线电把他们连起来,无线电的费用随着距离增加而增加,并且长传播距离为d,现在有s个卫星,任意两个安装了卫星的设备无论距离多远都可以直接通信,求一个方案使得d最小。
Solution
对于这道题,我们可以转化我:求边界边权最少的边后变成s个联通块后,使用卫星连接。
边权最少,我们可以使用Kruscak来进行枚举边,知道有s个连通分量后就退出即可。
代码实现非常简单。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
#include<cmath>
#include<algorithm>
#define Mp make_pair
using namespace std;
int x[1000];
int y[1000];
int fa[1000];
struct edge{
int u,v;
double val;
}a[1000000];
bool cmp(edge p1,edge p2){
return p1.val<p2.val;
}
int get(int x){
if (fa[x]==x) return x;
else return fa[x]=get(fa[x]);
}
void work()
{
int s,n,tot=0,cnt=0;
double Max=0;
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
memset(fa,0,sizeof(fa));
scanf("%d%d",&s,&n);
for (int i=1;i<=n;++i)
scanf("%d%d",x+i,y+i);
for (int i=1;i<=n;++i)
for (int j=i+1;j<=n;++j)
a[++tot]=edge{i,j,sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]))};
for (int i=0;i<=n;++i) fa[i]=i;
sort(a+1,a+tot+1,cmp);
for (int i=1;i<=tot;++i)
{
int fu=get(a[i].u);
int fv=get(a[i].v);
if (fu==fv) continue;
Max=max(Max,a[i].val);
fa[fu]=fv;
if (++cnt==n-s) break;
}
printf("%.2lf\n",Max);
}
int main(void)
{
int T;
cin>>T;
for (int i=1;i<=T;++i) work();
return 0;
}