Arctic Network

xiaoxiao2021-02-27  101

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

这道题就是让我们去找最小生成树,然后说可以用卫星接口来代替一些边,那我们自然就想到每次代替树中最长的边,这样才能是我们的距离变短么。我们只需要记录下来,那些边构成了这棵树,然后将这些边从大到小排个序,就好了。

#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> #include<queue> #define INF 0x3f3f3f3f using namespace std; int sate,radio; int pre[510]; double a[510]; struct edge { int u,v; double w; }Edge[250010]; struct point { int x; int y; }point[510]; int cmp(edge a,edge b) { return a.w<b.w; } int cmp2(double a,double b) { return a>b; } int find_root(int x) { return pre[x]==x?x:pre[x]=find_root(pre[x]); } int main() { int t; scanf("%d",&t); while(t--) { memset(Edge,0,sizeof(Edge)); memset(pre,0,sizeof(pre)); memset(point,0,sizeof(point)); scanf("%d %d",&sate,&radio); for(int i=0;i<radio;i++) { scanf("%d %d",&point[i].x,&point[i].y); pre[i]=i; } int cnt=0; for(int i=0;i<radio;i++) { for(int j=i+1;j<radio;j++) { int x1=point[i].x,y1=point[i].y; int x2=point[j].x,y2=point[j].y; double r=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); Edge[cnt].u=i,Edge[cnt].v=j,Edge[cnt++].w=sqrt(r);//sqrt()里面好像一直都在提示用double 类型 } } sort(Edge,Edge+cnt,cmp); int num=0; for(int i=0;i<cnt;i++) { int x,y,rx,ry; x=Edge[i].u,y=Edge[i].v; rx=find_root(x),ry=find_root(y); if(rx!=ry) { pre[ry]=rx; a[num++]=Edge[i].w;//将这些用过的边我们都先存起来 } } sort(a,a+num,cmp2); int index=sate-1; printf("%.2f\n",a[index]); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-16009.html

最新回复(0)