Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- C++
- algorithm
- UE5
- const
- softeer
- baekjoon
- 티스토리챌린지
- Frustum
- 줄 세우기
- winapi
- IFileDialog
- 언리얼엔진5
- 1563
- directx
- DirectX11
- UnrealEngine4
- 프로그래머스
- 2294
- 백준
- Programmers
- 팰린드롬 만들기
- RVO
- UnrealEngine5
- RootMotion
- Unreal Engine5
- GeeksForGeeks
- 오블완
- NRVO
- C
- DeferredRendering
Archives
- Today
- Total
Game Develop
[Algorithm]Baekjoon 4386 :: 별자리 만들기 본문
https://www.acmicpc.net/problem/4386
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
|
using namespace std;
struct Node
{
float distance;
int node1;
int node2;
};
bool cmp(const Node& a, const Node& b)
{
return a.distance < b.distance;
}
int parents[100001] = { 0 };
int findParent(int node)
{
if (node == parents[node]) return node;
return parents[node] = findParent(parents[node]);
}
void unionParents(int a, int b)
{
a = findParent(a);
b = findParent(b);
if (a < b) parents[b] = a;
else parents[a] = b;
}
bool isSameParents(int a, int b)
{
a = findParent(a);
b = findParent(b);
if (a == b) return true;
return false;
}
int n;
vector<pair<float, float>> positions;
vector<Node> nodes;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i)
{
parents[i] = i;
}
for (int i = 0; i < n; ++i)
{
float x, y;
cin >> x >> y;
positions.push_back({ x, y });
}
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
float xGap = powf(positions[i].first - positions[j].first, 2);
float yGap = powf(positions[i].second - positions[j].second, 2);
float distance = sqrt(xGap + yGap);
nodes.push_back({ distance,i,j });
}
}
sort(nodes.begin(), nodes.end(), cmp);
float answer = 0.0f;
for (int i = 0; i < nodes.size(); ++i)
{
if (isSameParents(nodes[i].node1, nodes[i].node2) == false)
{
unionParents(nodes[i].node1, nodes[i].node2);
answer += nodes[i].distance;
}
}
printf("%.2f", answer);
}
|
cs |
전형적인 MST문제. 소수점 둘째자리수까지만 출력해줘야 하는것을 잊지말자.
'Algorithm > Baekjoon' 카테고리의 다른 글
[Algorithm]Baekjoon 14003 :: 가장 긴 증가하는 부분수열 5 (0) | 2024.02.24 |
---|---|
[Algorithm]Baekjoon 10775 :: 공항 (1) | 2024.02.24 |
[Algorithm]Baekjoon 2887 :: 행성 터널 (0) | 2024.02.22 |
[Algorithm]Baekjoon 2623번 :: 음악프로그램 (0) | 2024.02.22 |
[Algorithm]Baekjoon 2568번 : 전깃줄 - 2 (0) | 2024.02.21 |