-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path10369 - Arctic Network.cpp
78 lines (66 loc) · 1.36 KB
/
10369 - Arctic Network.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct edge
{
int u,v;
double w;
} E;
int prev[510];
int getParent(int i)
{
if(i==prev[i]) return i;
return (prev[i]=getParent(prev[i]));
}
int isUnion(int a,int b)
{
return getParent(a)==getParent(b);
}
void makeUnion(int a,int b)
{
prev[getParent(a)] = getParent(b);
}
int cmp(const void*a,const void*b)
{
if((*(E*)a).w<(*(E*)b).w) return -1;
return 1;
}
double distance(int x1,int y1,int x2,int y2)
{
return (sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ));
}
int main()
{
int n,e,i,d,t,s,j;
double mst;
E x[200005];
int dx[510],dy[510];
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&s,&n) ;
for(i=0; i<n; i++)prev[i]=i;
for(i=0; i<n; i++)
scanf("%d %d",&dx[i],&dy[i]);
e=0;
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
x[e].u=i;
x[e].v=j;
x[e].w=distance(dx[i],dy[i],dx[j],dy[j]);
e++;
}
qsort(x,e,sizeof(E),&cmp);
d =mst= 0;
for(i=0; i<e && d<n-s; i++)
if(!isUnion(x[i].u,x[i].v))
{
makeUnion(x[i].u,x[i].v);
if(mst<x[i].w)mst=x[i].w;
d++;
}
printf("%.2lf\n",mst);
}
return 0;
}