forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharcticnetwork.cpp
90 lines (70 loc) · 1.68 KB
/
arcticnetwork.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
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
struct point {
int x, y;
};
struct edge {
int p1, p2;
double dist;
};
double dist(point p1, point p2) {
return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}
int find(vector<int>& disjoint, int a) {
if(disjoint[a] == -1) {
return a;
}
disjoint[a] = find(disjoint, disjoint[a]);
return disjoint[a];
}
void join(vector<int>& disjoint, int a, int b) {
a = find(disjoint, a);
b = find(disjoint, b);
if(a == b) {
return;
}
disjoint[a] = b;
}
bool cmp(edge lhs, edge rhs) {
return lhs.dist < rhs.dist;
}
int main() {
int cases;
cin >> cases;
while(cases--) {
int sat, out;
cin >> sat >> out;
vector<point> v;
for(int i = 0; i < out; i++) {
point p;
cin >> p.x >> p.y;
v.push_back(p);
}
vector<edge> edges;
for(int i = 0; i < v.size() - 1; i++) {
for(int j = i + 1; j < v.size(); j++) {
double d = dist(v[i], v[j]);
edge e = {i, j, d};
edges.push_back(e);
}
}
sort(edges.begin(), edges.end(), cmp);
vector<int> disjoint(out, -1);
vector<double> kept;
for(auto i : edges) {
if(find(disjoint, i.p1) != find(disjoint, i.p2)) {
join(disjoint, i.p1, i.p2);
kept.push_back(i.dist);
}
}
for(int i = 1; i < sat; i++) {
kept.pop_back();
}
cout << fixed;
cout.precision(2);
cout << kept.back() << endl;
}
}