-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path1552 - Rescue in Free Fall.cpp
78 lines (60 loc) · 1.25 KB
/
1552 - Rescue in Free Fall.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
#include <stdio.h>
#include <math.h>
#include <memory.h>
#include <vector>
#include <algorithm>
using namespace std;
struct point {
int x, y;
point(int x, int y) :
x(x), y(y) {}
};
struct edge {
int from, to, cost;
edge(int from, int to, int cost) :
from(from), to(to), cost(cost) {}
bool operator<(const edge &e) const {
return cost < e.cost;
}
};
int n, ta, ds[501];
double to;
vector<edge> edges;
vector<point> points;
int dist(point a, point b) {
return ((a.x - b.x) * (a.x - b.x)) + ((a.y - b.y) * (a.y - b.y));
}
int find(int x) {
return ds[x] == x ? x : x = find(ds[x]);
}
int main() {
int t;
scanf("%d", &t);
while(t-- != 0) {
to = ta = 0;
edges.clear();
points.clear();
scanf("%d", &n);
for(int i = 0, a, b; i < n; ++i) {
ds[i] = i;
scanf("%d %d", &a, &b);
points.push_back(point(a, b));
}
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(i != j)
edges.push_back(edge(i, j, dist(points[i], points[j])));
sort(edges.begin(), edges.end());
for(int i = 0, a, b; i < (int)edges.size() && ta < n - 1; ++i) {
a = find(edges[i].from);
b = find(edges[i].to);
if(ds[a] != ds[b]) {
ds[a] = b;
++ta;
to += sqrt(edges[i].cost);
}
}
printf("%.2lf\n", to / 100);
}
return 0;
}