acm-Arctic Network(最小生成树)

来源:互联网 发布:数据录入员工作的细节 编辑:程序博客网 时间:2024/05/18 02:04

Arctic Network
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 10070 Accepted: 3334

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.

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

12 40 1000 3000 600150 750

Sample Output

212.13

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define MAXN 502using namespace std;struct Pos{int x,y;}pos[MAXN];struct Edg{int u,v;double w;}edg[MAXN*MAXN/2];int parent[MAXN];double d[MAXN];bool operator < (const Edg &x,const Edg &y){return x.w<y.w;}double getSqrt(int x1,int y1,int x2,int y2){returnsqrt((double)(y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));}int Find(int x){if(x!=parent[x])return parent[x]=Find(parent[x]);return x;}int main(){int N;scanf("%d",&N);while(N--){int S,P;scanf("%d%d",&S,&P);for(int i=0;i<P;i++){scanf("%d%d",&pos[i].x,&pos[i].y);}int m=0;for(int i=0;i<P;i++){for(int j=i+1;j<P;j++){edg[m].u=i;edg[m].v=j;edg[m++].w=getSqrt(pos[i].x,pos[i].y,pos[j].x,pos[j].y);}}sort(edg,edg+m);int count=0;for(int i=0;i<P;i++) parent[i]=i;for(int i=0;i<m;i++){int o=Find(edg[i].u);int p=Find(edg[i].v);if(o!=p){d[count++]=edg[i].w;parent[o]=p;}}printf("%.2lf\n",d[P-S-1]);}return 0;}


0 0
原创粉丝点击