From 9fc802743b4c5f6b249215f82232919dea0263af Mon Sep 17 00:00:00 2001 From: dhlee777 Date: Sat, 30 Mar 2024 00:16:16 +0900 Subject: [PATCH 1/2] =?UTF-8?q?2024-03-30=20=EC=B5=9C=EC=86=8C=EC=8A=A4?= =?UTF-8?q?=ED=8C=A8=EB=8B=9D=ED=8A=B8=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\353\213\235\355\212\270\353\246\254.cpp" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 "dhlee777/mst/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254.cpp" diff --git "a/dhlee777/mst/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254.cpp" "b/dhlee777/mst/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254.cpp" new file mode 100644 index 0000000..6ba373f --- /dev/null +++ "b/dhlee777/mst/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254.cpp" @@ -0,0 +1,60 @@ +#include +#include +using namespace std; +int parent[10001]; //θ带 ִ 迭 +int sum, cnt = 0; +int v_num, e_num; +priority_queue < pair>, vector>>, greater>>>q; +// ġ ĵǴ 켱ť,ť Ҵpair(ġ,1,2) ̷. +int find_parent(int a) { // Ʈ带 ã Լ + if (a == parent[a]) return a; + else + return parent[a] = find_parent(parent[a]); +} +void add(int a, int b) { // 尡 ִ Լ + a = find_parent(a); + b = find_parent(b); + if (a > b) parent[a] = b; + else parent[b] = a; +} +bool compare_union(int a, int b) { // 尡 Ǻִ Լ + a = find_parent(a); + b = find_parent(b); + return (a == b); +} +void find_min() { //ġ ּڰ ϴ Լ + while (!q.empty()) { //ť ġ 峷 pair .(greedy) + int v1 = q.top().second.first; // 1 + int v2 = q.top().second.second; // 2 + int eg = q.top().first; // ġ + q.pop(); + if (compare_union(v1, v2)) continue; // ý Ŭ ߻ϹǷ ʰ ݺ + else // ٸ ϰ( ) + { + add(v1, v2); // ش. + sum += eg; // ġ ش. + cnt++; // īƮش + } + if (cnt == v_num - 1) // -1 ̵Ǹ Ȱ̹Ƿ ݺ + break; + + } + cout << sum; //ġ +} +int main(void) { + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + int v1, v2, eg; // 1,2,ġ + cin >> v_num >> e_num; + for (int i = 0; i < e_num; i++) { // ŭ Է¹޴´ + cin >> v1 >> v2 >> eg; + q.push(make_pair(eg, make_pair(v1, v2))); //pair(ġ,1,2) ť ִ´. + } + + for (int i = 0; i < v_num; i++) { //ڱڽ θ ڱڽ ʱȭ + parent[i] = i; + } + find_min(); + return 0; +} \ No newline at end of file From 405871ca6d34f063e6533a999406fbc7eae3a7ef Mon Sep 17 00:00:00 2001 From: dhlee777 Date: Sat, 30 Mar 2024 01:37:47 +0900 Subject: [PATCH 2/2] =?UTF-8?q?2024-03-30=20=EC=B5=9C=EC=86=8C=EC=8A=A4?= =?UTF-8?q?=ED=8C=A8=EB=8B=9D=ED=8A=B8=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dhlee777/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dhlee777/README.md b/dhlee777/README.md index 2cbf92f..a897fc2 100644 --- a/dhlee777/README.md +++ b/dhlee777/README.md @@ -5,6 +5,8 @@ | 1차시 | 2024.03.12 | BFS | [숨바꼭질](https://www.acmicpc.net/problem/1697) | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/4)| | 2차시 | 2024.03.18 | DP | [쉬운계단수](https://www.acmicpc.net/problem/10844) | [#8](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/8)| | 3차시 | 2024.03.23 | union-find | [집합의 표현](https://www.acmicpc.net/problem/1717) | [#14](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/14)| +| 4차시 | 2024.03.25 | BFS | [바이러스](https://www.acmicpc.net/problem/2606) | [#17](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/17)| +| 5차시 | 2024.03.30 | mst | [최소스패닝트리](https://www.acmicpc.net/problem/1197) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/21)|