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;
}
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;
}
相关文章推荐
- POJ 2349 Arctic Network 最小生成树 Prim
- Prim最小生成树【poj 2349 Arctic Network;poj 1287 NetWorking】
- POJ 2349 Arctic Network(最小生成树之Prim)
- zoj 1914 || poj 2349 Arctic Network【最小生成树 kruskal && prim】
- POJ 2349 Arctic Network(最小生成树 kruscal or prim)
- poj2349——Arctic Network(最小生成树+prim)
- POJ 2349 Arctic Network 最小生成树题解
- 文章标题 POJ 2349:Arctic Network ( 最小生成树Kruskal算法+并查集)
- POJ-2349 Arctic Network (最小生成树)
- POJ-2349-Arctic Network [最小生成树]
- 2349 Arctic Network prim最小生成树 基础
- POJ 2349 Arctic Network (最小生成树)
- Arctic Network(POJ 2349)(最小生成树Kruskal)
- poj2349 Arctic Network ——最小生成树入门题_Prim算法
- POJ - 2349 Arctic Network (最小生成树分块)
- poj 2349 (最小生成树)Arctic Network
- POJ 2349 Arctic Network (最小生成树)
- POJ 2349 Arctic Network(最小生成树,第k大边权,基础)
- POJ 2349 Arctic Network(最小生成树)
- POJ 2349 Arctic Network【中文版题目】【最小生成树+思维】