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
- ๊ตฌํ
- ์์ ์ ๋ ฌ
- CVE
- dfs
- ์คํ
- ๊ทธ๋ฆฌ๋
- DP
- error
- ์ด๋ถ ํ์
- GCP
- ์๋ฎฌ๋ ์ด์
- ๋ฐฑํธ๋ํน
- dynamic debugging
- ๋ฌธ์์ด
- Reversing
- ๋์ ํฉ
- ๋ถํ ์ ๋ณต
- JPA
- OS
- Spring
- ์ฌ๊ท
- ๋งต
- thymeleaf
- java
- web
- c++
- BFS
- ์ต๋จ ๊ฒฝ๋ก
- ์ฐ์ ์์ ํ
- ๋ฐ์ดํฌ์คํธ๋ผ
Archives
- Today
- Total
hades
[Baekjoon] 1197๋ฒ: ์ต์ ์คํจ๋ ํธ๋ฆฌ ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1197
๐ ์ค๊ณ
๋จ์ํ, ์ต์ ์คํจ๋ ํธ๋ฆฌ๋ฅผ ๊ตฌํํ๋ ๋ฌธ์ ์ด๋ค.
์ต์ ์คํจ๋ ํธ๋ฆฌ๋ฅผ ์ด๋ป๊ฒ ๊ตฌํํ๋์ง ๋ณต๊ธฐํด๋ณด์.
1. ๊ฐ์ ์ ๋ณด๋ฅผ ๋น์ฉ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ค. ๋ณธ์ธ์ ์ฐ์ ์์ ํ๋ฅผ ์ฌ์ฉํ์๋ค.
2. ์ฐ์ ์์ ํ์์ ํ๋์ฉ ๊บผ๋ด์(= ๋น์ฉ์ด ์์ ๊ฐ์ฐ๋ถํฐ) ๋ ์ ์ ์ ์ฐ๊ฒฐ ์๋ํ๋ค. ๋ ์ ์ ์ ๋ถ๋ชจ๊ฐ ๊ฐ์ ๊ฒฝ์ฐ, ์ด๋ฏธ ์ฐ๊ฒฐ๋์ด ์๋ค๋ ๋ป์ด๋ฏ๋ก ๋์ด๊ฐ๋ค. ๋ ์ ์ ์ ๋ถ๋ชจ๊ฐ ๋ค๋ฅธ ๊ฒฝ์ฐ, ์ฐ๊ฒฐ๋์ด ์์ง ์๋ค๋ ์๋ฏธ์ด๋ฏ๋ก union์ ํตํด ์ฐ๊ฒฐํ๊ณ ๊ฐ์ค์น์ ๋น์ฉ์ ์ถ๊ฐํ๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int v, e, a, b, c, result = 0;
vector<int> parent(10001);
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> edges;
int find_parent(int x) {
if (parent[x] != x) {
return find_parent(parent[x]);
}
return x;
}
void union_parent(int a, int b) {
if (a > b) {
parent[b] = a;
}
else {
parent[a] = b;
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> v >> e;
for (int i = 1; i <= v; i++) {
parent[i] = i;
}
for (int i = 0; i < e; i++) {
cin >> a >> b >> c;
edges.push({ c, a, b });
}
while (!edges.empty()) {
int c = edges.top()[0];
int a = edges.top()[1];
int b = edges.top()[2];
edges.pop();
a = find_parent(a);
b = find_parent(b);
if (a != b) {
union_parent(a, b);
result += c;
}
}
cout << result << "\n";
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 17144๋ฒ: ๋ฏธ์ธ๋จผ์ง ์๋ ! (0) | 2024.09.05 |
---|---|
[Baekjoon] 9935๋ฒ: ๋ฌธ์์ด ํญ๋ฐ (0) | 2024.09.04 |
[Baekjoon] 9663๋ฒ: N-Queen (0) | 2024.08.28 |
[Baekjoon] 1932๋ฒ: ์ ์ ์ผ๊ฐํ (0) | 2024.08.19 |
[Baekjoon] 15666๋ฒ: N๊ณผ M (12) (0) | 2024.08.13 |