您的位置:首页 > 其它

POJ 2349 Arctic Network(最小生成树 Prim)

2018-03-16 13:20 549 查看
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.InputThe 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).OutputFor 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 Input1
2 4
0 100
0 300
0 600
150 750
Sample Output212.13
思路:
用一个ans数组存储,构成最小树的所有边长,然后对边长进行排序。 PS. 输出使用 %.2f  玄学问题?
代码:#include<stdio.h>
#include<math.h>
#include<algorithm>
#define For(a,b,c) for(int a = b; a <= c; a++)
#define MAXN 505

int book[MAXN], x[MAXN], y[MAXN];
double ans[MAXN], e[MAXN][MAXN], dis[MAXN];

double distance(int i, int j)
{
return sqrt( (x[i]-x[j])*(x[i]-
4000
x[j]) + (y[i]-y[j])*(y[i]-y[j]) );
}

int main()
{
int case_time, S, P;
scanf("%d",&case_time);
while(case_time--)
{
scanf("%d%d",&S,&P);
For(i,1,P)
{
book[i] = 0;
ans[i] = 0;
scanf("%d%d",&x[i],&y[i]);
For(j,1,i)
{
e[i][j] = e[j][i] = distance(i,j);
}
}

int j, cnt = 1;
double minn;

For(i,1,P)
dis[i] = e[1][i];

book[1] = 1;

while(cnt++ < P)
{
minn = 0x3f3f3f3f;
For(i,1,P)
if(!book[i] && minn > dis[i]) minn = dis[i], j = i;

book[j] = 1;
ans[cnt-2] = minn;

For(i,1,P)
if(!book[i] && dis[i] > e[j][i]) dis[i] = e[j][i];
}

// For(i,0,P-1)printf("%lf\n",ans[i]);
std::sort(ans,ans+P);

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