-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path10034.cpp
More file actions
74 lines (68 loc) · 944 Bytes
/
10034.cpp
File metadata and controls
74 lines (68 loc) · 944 Bytes
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
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
long i,j,k,u,v,n,t,p[110],r[110];
double x[110],y[110],cst;
struct ss
{
long beg,end;
double cost;
}T[11000];
bool cmp(ss aa,ss bb)
{
return aa.cost<bb.cost;
}
void link(long x,long y)
{
if(r[x]>=r[y])
p[y]=x,r[x]++;
else
p[x]=y,r[y]++;
}
long find(long x)
{
if(p[x]==x)
return x;
else return p[x]=find(p[x]);
}
int main()
{
scanf("%ld",&t);
while(t--)
{
scanf("%ld",&n);
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&x[i],&y[i]);
p[i]=i;
r[i]=1;
}
k=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
T[k].beg=i;
T[k].end=j;
T[k++].cost=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
}
}
sort(T,T+k,cmp);
cst=0;
for(i=0;i<k;i++)
{
u=find(T[i].beg);
v=find(T[i].end);
if(u!=v)
{
cst+=sqrt(T[i].cost);
link(u,v);
}
}
printf("%.2lf\n",cst);
if(t)
printf("\n");
}
return 0;
}