您的位置:首页 > 其它

Arctic Network (最小生成树)

2018-01-31 15:55 106 查看
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


题中说有在北极有几个地方需要保持信息连接,卫星可以任意远的两个地方连接,而无线电则有距离限制,求需要的无线电的辐射长度最小是多少!

分析下题:首先建最小树,距离远的用卫星连接,而剩下的距离中的最大值则为满足条件的无线电辐射长度!

prim算法

#include<stdio.h>
#include<math.h>
#include<string.h>
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;
int n,m;
int vis[1005];
double dis[1005],pre[1005][1005];
double u[1005],v[1005];
double Dis(double x1,double x2,double y1,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void prim()
{
int i,j,indx;
double min;
memset(vis,0,sizeof(vis));
vis[0]=1;
for(i=1;i<m;i++)
{
dis[i]=pre[0][i];
}
for(j=0;j<m;j++)
{
indx=0;
min=inf;
for(i=0;i<m;i++)
{
if(!vis[i] && dis[i]<min)
{
min=dis[i];
indx=i;
}
}
vis[indx]=1;
for(i=0;i<m;i++)
{
if(!vis[i]&&dis[i]>pre[indx][i])
dis[i]=pre[indx][i];
}
}
sort(dis,dis+m);        //从小到大排序
printf("%.2f\n",dis[m-n]);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%lf%lf",&u[i],&v[i]);
}
memset(pre,0,sizeof(pre));
for(int i=0;i<m;i++)
{
for(int j=0;j<i;j++)
{
pre[i][j]=pre[j][i]=Dis(u[i],u[j],v[i],v[j]);
}
}
prim();
}
return 0;
}

Kruskal算法

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int per[260000],n,m;
double a[260000];
struct node
{
int x,y;
double w;
}s[260000];
bool cmp(node x,node y)
{
return x.w<y.w;
}
int find(int x)
{
int i,j,t=x;
while(t!=per[t])
t=per[t];
i=x;
while(i!=t)
{
j=per[i];
per[i]=t;
i=j;
}
return t;
}

int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
double x[600],y[600];
scanf("%d%d",&m,&n);
for(i=0;i<=n;i++)
per[i]=i;
for(i=0;i<n;i++)
scanf("%lf%lf",&x[i],&y[i]);
int k=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
s[k].x=i;
s[k].y=j;
s[k].w=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
k++;
}
}
sort(s,s+k,cmp);
int t=0;
for(int i=0;i<k;i++)
{
int fx=find(s[i].x);
int fy=find(s[i].y);
if(fx!=fy)
{
per[fx]=fy;
a[t]=s[i].w;
t++;
}

}
printf("%.2f\n",a[t-m]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: