-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj4386.cpp
105 lines (90 loc) · 1.99 KB
/
boj4386.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
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
using p = pair<int, int>;
using pd = pair<double, double>;
const int MAX = 101;
inline void Quick_IO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int parent[MAX];
int ranks[MAX];
int find(int a){
if(parent[a]==a) return a;
else{
return parent[a] = find(parent[a]);
}
}
void _union(int a, int b){
int x = find(a);
int y = find(b);
if(x==y) return;
if(ranks[x]>ranks[y]) {
swap(x, y);
}
parent[x] = y;
if(ranks[x]==ranks[y]) {
ranks[y]++;
}
}
struct edge {
p coord;
double distance;
edge(p a, double b) {
coord = a;
distance = b;
}
};
struct compare {
bool operator()(edge a, edge b) {
return a.distance > b.distance;
}
};
int N;
pd arr[101];
priority_queue<edge, vector<edge>, compare> pq;
bool visited[101];
double dist(double x1, double y1, double x2, double y2) {
double d1, d2;
d1 = pow(x1-x2, 2);
d2 = pow(y1-y2, 2);
return sqrt(d1 + d2);
}
int main() {
Quick_IO();
cin >> N;
cout.precision(6);
for (int i = 1; i <= N; ++i) parent[i] = i;
double a, b;
for (int i = 0; i < N; ++i) {
cin >> a >> b;
arr[i] = pd(a, b);
}
for (int i = 0; i < N; ++i) {
for (int j = i+1; j < N; ++j) {
pq.push(edge(p(i, j), dist(arr[i].first, arr[i].second, arr[j].first, arr[j].second)));
}
}
int counts = 0;
double answer = 0.0;
while(!pq.empty()){
if(counts==N) break;
auto [currCoord, currDistance] = pq.top();
auto [currX, currY] = currCoord;
// cout<<currX<<" "<<currY<<" "<<currDistance<<'\n';
pq.pop();
if(find(currX)==find(currY)){
continue;
}else{
counts++;
_union(currX, currY);
answer += currDistance;
}
}
cout<<fixed<<answer<<'\n';
return 0;
}