Arctic Network
Arctic Network
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 3
Problem 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.
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,需要记录每次挑选出来的min,然后排序,代码如下
#include<stdio.h> #include<string.h> #define INF 0xfffffff #include<math.h> #include<stdlib.h> #include<algorithm> using namespace std; int cmp(double a,double b) { if(a>b) return 1; return 0; } double x[1010],y[1010],map[1010][1010],ans[1010]; int v[1010]; double dis(double x1,double y1,double x2,double y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } int main() { int t; scanf("%d",&t); while(t--) { int s,p,i,j,flag,cot=0; double min; scanf("%d%d",&s,&p); for(i=1;i<=p;i++) { for(j=1;j<=p;j++) { map[i][j]=INF; } } for(i=1;i<=p;i++) { scanf("%lf%lf",&x[i],&y[i]); for(j=1;j<i;j++) { map[i][j]=map[j][i]=dis(x[i],y[i],x[j],y[j]); } } memset(v,0,sizeof(v)); v[1]=1; for(i=1;i<=p;i++) { flag=-1; min=INF; for(j=1;j<=p;j++) { if(!v[j]&&map[1][j]<min) { min=map[1][j]; flag=j; } } if(flag==-1) break; v[flag]=1; ans[cot++]=min; for(j=1;j<=p;j++) { if(!v[j]&&map[1][j]>map[flag][j]) map[1][j]=map[flag][j]; } } sort(ans,ans+cot,cmp); printf("%.2lf\n",ans[p-s-1]); } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步