-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy path[16]_3.cpp
76 lines (63 loc) · 1.58 KB
/
[16]_3.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
#include <bits/stdc++.h>
using namespace std;
int n, m;
int parent[1001];
vector<pair<int, int> > locations;
vector<pair<double, pair<int, int> > > edges;
// 두 점 사이의 거리를 계산하는 함수
double getDistance(pair<int, int> p1, pair<int, int> p2) {
long long a = p1.first - p2.first;
long long b = p1.second - p2.second;
return sqrt((a * a) + (b * b));
}
int getParent(int a) {
if (a == parent[a]) return a;
return parent[a] = getParent(parent[a]);
}
void unionParent(int a, int b) {
a = getParent(a);
b = getParent(b);
if (a < b) parent[b] = a;
else parent[a] = b;
}
int compareParent(int a, int b) {
a = getParent(a);
b = getParent(b);
if (a == b) return true;
return false;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
locations.push_back(make_pair(x, y));
}
// 모든 점 사이의 거리를 계산하여 간선(Edge) 리스트에 저장
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
edges.push_back(make_pair(getDistance(locations[i], locations[j]), make_pair(i + 1, j + 1)));
}
}
for (int i = 1; i <= n; i++) {
parent[i] = i;
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
unionParent(a, b);
}
// 간선의 길이를 기준으로 오름차순 정렬
sort(edges.begin(), edges.end());
double result = 0;
for (int i = 0; i < edges.size(); i++) {
double cost = edges[i].first;
int a = edges[i].second.first;
int b = edges[i].second.second;
if (!compareParent(a, b)) {
unionParent(a, b);
result += cost;
}
}
printf("%0.2f", result);
}