-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10034.cpp
More file actions
81 lines (76 loc) · 1.85 KB
/
10034.cpp
File metadata and controls
81 lines (76 loc) · 1.85 KB
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
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define inf 10000000
#define mem(a, b) memset(a, b, sizeof(a))
#define NN 111
struct edge {
int u, v;
double w;
};
struct node {
double x, y;
};
node a[NN + 7];
int pr[NN + 7];
vector<edge> e;
bool comp(edge n, edge m) { return n.w < m.w; }
int root(int n) {
if (pr[n] == n) return n;
return root(pr[n]);
}
double mst(int n) {
int i, j, k;
for (i = 0; i <= n; i++) pr[i] = i;
sort(e.begin(), e.end(), comp);
int count = 0;
double sum = 0;
for (i = 0; i < e.size() && count < n - 1; i++) {
int u = root(e[i].u);
int v = root(e[i].v);
if (u != v) {
pr[u] = v;
count++;
sum += e[i].w;
}
}
return sum;
}
int main() {
// freopen("C:\\Users\\Maruf Tuhin\\Desktop\\in.txt","r",stdin);
// ios_base::sync_with_stdio(false);
int i, j, k, l, n, r, c, u, v, w;
edge ed;
node nd;
int tc, t = 1, m;
double x;
scanf("%d", &tc);
while (tc--) {
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%lf%lf", &a[i].x, &a[i].y);
for (i = 1; i <= n; i++)
for (j = i + 1; j <= n; j++) {
x = ((a[i].x - a[j].x) * (a[i].x - a[j].x)) + ((a[i].y - a[j].y) * (a[i].y - a[j].y));
x = sqrt((double)x);
ed.u = i;
ed.v = j;
ed.w = x;
e.push_back(ed);
}
double sum = mst(n);
if (t != 1) puts("");
t++;
printf("%.2lf\n", sum);
e.clear();
}
cin >> n;
return 0;
}