The Department of National Defence(DND) wishes to connect severalnorthern outposts by a wirelessnetwork. Two different communicationtechnologies are to beused in establishing the network:every outpost will have a radiotransceiver and some outposts willin addition have a satellite channel.Any two outposts with a satellitechannel can communicate viathe satellite, regardless of their location.Otherwise, two outpostscan communicate by radio only ifthe distance between them does notexceed D, which depends of thepower of the transceivers. Higherpower yields higher D but costsmore. Due to purchasing andmaintenance considerations, thetransceivers at the outposts mustbe identical; that is, the value of Dis the same for every pair of outposts.Your job is to determine theminimum D required for thetransceivers. There must be atleast one communication path (director indirect) between every pairof outposts.InputThe first line of input contains N, the number of test cases. The first line of each test case contains1 ≤ S ≤ 100, the number of satellite channels, and S < P ≤ 500, the number of outposts. P linesfollow, giving the (x, y) coordinates of each outpost in km (coordinates are integers between 0 and10,000).OutputFor each case, output should consist of a single line giving the minimum D required to connect thenetwork. Output should be specified to 2 decimal points.Sample Input12 40 1000 3000 600150 750Sample Output212.13
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct STR
{
int x,y;
}a[501];
struct STRU
{
int x,y;double d;
}b[250002];
int tree[501];
int find(int m)
{
if(m==tree[m])
return m;
else
return tree[m]=find(tree[m]);
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx>fy)
tree[fx]=fy;
else
tree[fy]=fx;
}
bool comp(STRU p,STRU q)
{
return p.d<q.d;
}
int main()
{
int t,n,m,i,j,k;double dist,sum1,sum2,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(i=0;i<n;i++)
{
scanf("%d %d",&a[i].x,&a[i].y);
}
k=0;
memset(tree,0,sizeof(tree));
for(i=0;i<n;i++){tree[i]=i;}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
sum1=(a[i].x-a[j].x)*(a[i].x-a[j].x);
sum2=(a[i].y-a[j].y)*(a[i].y-a[j].y);
dist=sqrt(sum1+sum2);
b[k].d=dist;
b[k].x=i;
b[k].y=j;
k++;
}
}
sort(b,b+k,comp);int s=0;
for(i=0;i<k;i++)
{
if(find(b[i].x)!=find(b[i].y))
{
merge(b[i].x,b[i].y);
s++;
if(s==n-m)
{
ans=b[i].d;
}
}
}
printf("%.2lf\n",ans);
}
return 0;
}