-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10034.cpp
96 lines (93 loc) · 2.34 KB
/
10034.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<bits/stdc++.h>
#define N 200005
#define ll long long
#define llu unsigned long long int
#define pare pair<int,int>
#define mod 1000000007
using namespace std;
int ct=0,cases,n,m;
///dir array for Queen int dxq[10] = {-1,-1,-1,1,1,1,0,0};
///dir array for Queen int dyq[10] = {-1,0,1,-1,0,1,-1,1};
///dir array for knight int dxk[10] = {-2,-2,-1,-1,1,1,2,2};
///dir array for knight int dyk[10] = {1,-1,-2,2,-2,2,-1,1};
/*int leap(int year){
if( year%4 == 0 && year%100 == !0 )
return 1;
else if (year%100 == 0 && year%400 == 0)
return 1;
else
return 0;
}*/
struct edge{
double x,y,cost;
int u,v;
}A[5055];
int B[5055];
int fnd(int r){
if(B[r] == r)return r;
else return r = fnd(B[r]);
}
bool compare(edge a,edge b){
return a.cost < b.cost;
}
double mst(){
int i,j,k,tot = 0;
double res = 0;
for(i=0;i<=n;i++){
B[i] = i;
}
while(tot != n - 1){
for(i=0;i<m;i++){
int u = fnd(A[i].u);
int v = fnd(A[i].v);
if(u != v){
B[u] = v;
res = res + A[i].cost;
tot++;
}
}
}
return res;
}
struct nothing{
double x,y;
}cord[105];
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int fl = 0;
scanf("%d",&cases);
while(cases--){
memset(A,0,sizeof A);
memset(cord,0,sizeof cord);
scanf("%d",&n);
double a,b;
int i,j,k;
for(i=0;i<n;i++){
scanf("%lf %lf",&cord[i].x,&cord[i].y);
}
k = 0;
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
double x = abs(cord[i].x - cord[j].x);
double y = abs(cord[i].y - cord[j].y);
// cout<<x<<" "<<y<<endl;
double dist = sqrt((x*x) + (y*y));
//cout<<dist<<endl;
A[k].u = i;
A[k].v = j;
A[k].cost = dist;
k++;
}
}
//cout<<"ss"<<endl;
m = k;
sort(A,A + k,compare);
//for(i=0;i<m;i++){
// cout<<A[i].cost<<endl;
//}
double ans = mst();
printf("%.2lf\n",ans);
if(cases)printf("\n");
}
}