Arctic Network POJ 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.

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

题目意思:有P个哨所和S个卫星,要建立所有哨所之间的联系。有卫星的两个哨所之间可以任意通信;否则一个哨所只能和距离它小于等于D的哨所通信。给出P个哨所的坐标,求D的最小值。
解题思路:为了使这P个哨所建立起联系,我们需要树状图,所以需要使用最小生成树的算法,将所有点建立起联系。但是为了所得到的D最小,对于最小生成树种那些边长较长的边,我们可以使用卫星进行联系,有S个卫星,这样就将最小生成树划分为了S个割集,割集之间使用卫星联系,割集之内使用无线联系,求割集内的点之间的最大值即为最小的D。
 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxs 550
using namespace std;
int pre[maxs];
int m,k;
int s,p;
double dis[maxs*maxs];
struct node///各个哨所的坐标
{
int x;
int y;
} a[maxs];
struct edge///哨所之间联系构成边
{
int u;
int v;
double w;
} e[maxs*maxs];
bool my_cmp(edge a,edge b)
{
return a.w<b.w;
}
bool my_cmp2(double a,double b)
{
return a>b;
}
double Getdis(node a,node b)///获得各个边的距离
{
return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
}
int Find(int x)
{
if(x!=pre[x])
{
pre[x]=Find(pre[x]);
}
return pre[x];
}
void Union(int a,int b)
{
int x=Find(a);
int y=Find(b);
if(x>y)
{
pre[x]=y;
}
else
{
pre[y]=x;
}
}
void Kruskal()
{
memset(dis,0.0,sizeof(dis));
int i,counts;
counts=;
for(i=; i<=m; i++)
{
int fx=Find(e[i].u);
int fy=Find(e[i].v);
if(fx!=fy)
{
pre[fx]=fy;
counts++;
dis[counts]=e[i].w;
if(counts==p-)///p个点,p-1条边
{
break;
}
}
}
sort(dis+,dis+counts+,my_cmp2);
printf("%.2f\n",dis[s]);///最小的D为所有边中排除前S大后最大的那一个
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&p);
for(i=; i<=p; i++)
{
pre[i]=i;
}
m=;
for(i=; i<=p; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
for(i=; i<=p; i++)
{
for(j=i+; j<=p; j++)
{
m++;
e[m].u=i;
e[m].v=j;
e[m].w=Getdis(a[i],a[j]);
}
}
sort(e+,e+m+,my_cmp);
Kruskal();
}
return ;
}
 
上一篇:虚拟机centos7配置本地yum源


下一篇:-aborted-exception-during-the-exce