uva 10369 Arctic Network (最小生成树加丁点变形)

The Department of National Defence(DND)wishestoconnectseveral 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 inadditionhaveasatellitechannel. 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 thedistancebetweenthemdoesnot 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 (directorindirect)betweeneverypair 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

分析:

有n个点给出坐标,点和点之间可以用无线电或者卫星通信,每个点都有无线电收发器可进行无线电通信,但是只有m个点有卫星通信功能。卫星通信的距离可以无限大,但无线电通信的距离不能超过D,超过D的部分将使通信费用增加。要使通信费用最少。

其实就是求一次最小生成树,m个点有卫星通信,那么就会有m-1条边的通信距离无限大,其实就是这m-1条边不用计算费用。而剩下的边中,找出最大边作为D值,这样剩下的所有的边都不会大于D,那么不会增加通信费用。在构建MST过程中保存下所有的边权值,然后按升序排序,除掉最后的m-1条边(也就是最大的m-1条边,这些边用卫星通信),最大的那条就是D

code:

#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
using namespace std;
typedef long long LL;
#define INF 1000000
#define max_v 505
struct node
{
double x,y;
}p[];
double g[max_v][max_v];
int n,m;
void init()
{
for(int i=;i<n;i++)
for(int j=;j<n;j++)
g[i][j]=INF;
}
void prim(int s)
{
double lowcost[n];
int used[n];
for(int i=;i<n;i++)
{
lowcost[i]=g[s][i];
used[i]=;
} used[s]=;
for(int i=;i<n;i++)
{
int j=;
for(int k=;k<n;k++)
{
if(!used[k]&&lowcost[k]<lowcost[j])
j=k;
}
used[j]=;
for(int k=;k<n;k++)
{
if(!used[k]&&g[j][k]<lowcost[k])
{
lowcost[k]=g[j][k];
}
}
}
sort(lowcost+,lowcost+n+);
printf("%0.2lf\n",lowcost[n-m+]);
}
double f(int i,int j)
{
double x=p[i].x-p[j].x;
double y=p[i].y-p[j].y;
return sqrt(x*x+y*y);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&m,&n);
for(int i=;i<n;i++)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
}
init();
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
{
g[i][j]=g[j][i]=f(i,j);
}
}
prim();
}
return ;
}
上一篇:Asp.Net生命周期系列二


下一篇:使用Q进行同步的Promises操作