Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5-dhlee777 #21

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dhlee777/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)|



Expand Down
60 changes: 60 additions & 0 deletions dhlee777/mst/최소스패닝트리.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include<iostream>
#include<queue>
using namespace std;
int parent[10001]; //�θ��带 �������ִ� �迭
int sum, cnt = 0;
int v_num, e_num;
priority_queue < pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>>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;
}