-
Notifications
You must be signed in to change notification settings - Fork 3
[Week11] 크루스칼, 프림 - 하은 #90
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
Open
leeeha
wants to merge
1
commit into
main
Choose a base branch
from
haeun
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <utility> | ||
| #include <algorithm> | ||
| #include <cmath> | ||
| #define MAX 1001 | ||
| using namespace std; | ||
|
|
||
| int n, m, k; // 정점, 간선, 발전소의 개수 | ||
| int parent[MAX]; // 루트 노드를 저장하는 테이블 | ||
| bool elec[MAX]; // 발전소가 설치된 정점 표시 | ||
| vector<pair<int, pair<int, int>>> edges; // 간선 정보 | ||
| int result = 0; // 최소 비용 | ||
|
|
||
| int findParent(int x){ | ||
| // 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀 호출 | ||
| if(x == parent[x]) return x; | ||
| return parent[x] = findParent(parent[x]); | ||
| } | ||
|
|
||
| void unionParent(int cost, int a, int b){ | ||
| a = findParent(a); | ||
| b = findParent(b); | ||
|
|
||
| // 두 정점 모두 발전소와 연결된 경우 | ||
| if(elec[a] && elec[b]) | ||
| return; | ||
| // 둘 중에 하나만 발전소와 연결된 경우 | ||
| else if(elec[a] && !elec[b]){ | ||
| // 발전소가 있는 지점에 연결 | ||
| parent[b] = a; | ||
| result += cost; | ||
| }else{ | ||
| parent[a] = b; | ||
| result += cost; | ||
| } | ||
| } | ||
|
|
||
| void solution(){ | ||
| // 간선 비용에 따라 오름차순 정렬 | ||
| sort(edges.begin(), edges.end()); | ||
|
|
||
| // 부모 테이블 초기화 | ||
| for(int i = 1; i <= n; i++){ | ||
| parent[i] = i; | ||
| } | ||
|
|
||
| for(int i = 0; i < edges.size(); i++){ | ||
| int cost = edges[i].first; | ||
| int u = edges[i].second.first; | ||
| int v = edges[i].second.second; | ||
|
Comment on lines
+49
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. u v 변수네이밍을 사용해서 조금 더 직관적이고 깔끔하네요 배워갑니다! |
||
|
|
||
| // 사이클을 형성하지 않으면서 | ||
| if(findParent(u) != findParent(v)){ | ||
| // 발전소에 연결되어 있지 않은 정점이 있으면 연결! | ||
| unionParent(cost, u, v); | ||
| } | ||
| } | ||
|
|
||
| cout << result; // 최소 비용 출력 | ||
| } | ||
|
|
||
| int main(){ | ||
| ios_base::sync_with_stdio(0); | ||
| cin.tie(0); | ||
|
|
||
| // 정점, 간선, 발전소의 개수 | ||
| cin >> n >> m >> k; | ||
|
|
||
| // 발전소가 설치된 정점 표시하기 | ||
| for(int i = 0; i < k; i++){ | ||
| int idx; | ||
| cin >> idx; | ||
| elec[idx] = true; | ||
| } | ||
|
|
||
| // 간선 정보 입력 | ||
| for(int i = 0; i < m; i++){ | ||
| int u, v, w; | ||
| cin >> u >> v >> w; | ||
| edges.push_back({w, {u, v}}); | ||
| } | ||
|
|
||
| solution(); | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <utility> | ||
| #include <algorithm> | ||
| using namespace std; | ||
|
|
||
| int v, e; // 노드와 간선의 개수 (최대 10만개) | ||
| int parent[100001]; // 부모 테이블 초기화 | ||
| vector<pair<int, pair<int, int>>> edges; // 모든 간선을 담을 배열 | ||
| int result = 0; | ||
|
|
||
| // 특정 원소가 속한 집합 찾아내기 | ||
| int findParent(int x){ | ||
| // 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀 호출 | ||
| if(x == parent[x]) return x; | ||
| return parent[x] = findParent(parent[x]); | ||
| } | ||
|
|
||
| // 두 원소가 속한 집합을 합치기 | ||
| void unionParent(int a, int b){ | ||
| a = findParent(a); | ||
| b = findParent(b); | ||
|
|
||
| // 더 작은 번호가 부모 노드가 되도록 | ||
| if(a < b) parent[b] = a; | ||
| else parent[a] = b; | ||
| } | ||
|
|
||
| int main(){ | ||
| ios_base::sync_with_stdio(0); | ||
| cin.tie(0); | ||
|
|
||
| cin >> v >> e; | ||
|
|
||
| // 부모 테이블 초기화 | ||
| for(int i = 1; i <= v; i++){ | ||
| parent[i] = i; | ||
| } | ||
|
|
||
| // 모든 간선에 대한 정보 입력 받기 | ||
| for(int i = 0; i < e; i++){ | ||
| int a, b, cost; | ||
| cin >> a >> b >> cost; | ||
| edges.push_back({cost, {a, b}}); | ||
| } | ||
|
|
||
| // 간선을 비용 순으로 정렬 | ||
| sort(edges.begin(), edges.end()); | ||
|
|
||
| // 간선을 하나씩 확인하면서 | ||
| for(int i = 0; i < edges.size(); i++){ | ||
| int cost = edges[i].first; | ||
| int a = edges[i].second.first; | ||
| int b = edges[i].second.second; | ||
|
|
||
| // 사이클이 발생하지 않는 경우에만 MST에 포함시키기 | ||
| if(findParent(a) != findParent(b)){ | ||
| unionParent(a, b); | ||
| result += cost; | ||
| } | ||
| } | ||
|
|
||
| cout << result; | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <utility> | ||
| #include <algorithm> | ||
| using namespace std; | ||
|
|
||
| int v, e; // 노드와 간선의 개수 | ||
| int parent[1001]; // 부모 테이블 초기화 | ||
| vector<pair<int, pair<int, int>>> edges; // 모든 간선을 담을 배열 | ||
| int result = 0; | ||
|
|
||
| // 특정 원소가 속한 집합 찾아내기 | ||
| int findParent(int x){ | ||
| // 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀 호출 | ||
| if(x == parent[x]) return x; | ||
| return parent[x] = findParent(parent[x]); | ||
| } | ||
|
|
||
| // 두 원소가 속한 집합을 합치기 | ||
| void unionParent(int a, int b){ | ||
| a = findParent(a); | ||
| b = findParent(b); | ||
|
|
||
| // 더 작은 번호가 부모 노드가 되도록 | ||
| if(a < b) parent[b] = a; | ||
| else parent[a] = b; | ||
| } | ||
|
|
||
| int main(){ | ||
| ios_base::sync_with_stdio(0); | ||
| cin.tie(0); | ||
|
|
||
| cin >> v >> e; | ||
|
|
||
| // 부모 테이블 초기화 | ||
| for(int i = 1; i <= v; i++){ | ||
| parent[i] = i; | ||
| } | ||
|
|
||
| // 모든 간선에 대한 정보 입력 받기 | ||
| for(int i = 0; i < e; i++){ | ||
| int a, b, cost; | ||
| cin >> a >> b >> cost; | ||
| edges.push_back({cost, {a, b}}); | ||
| } | ||
|
|
||
| // 간선을 비용 순으로 정렬 | ||
| sort(edges.begin(), edges.end()); | ||
|
|
||
| // 간선을 하나씩 확인하면서 | ||
| for(int i = 0; i < edges.size(); i++){ | ||
| int cost = edges[i].first; | ||
| int a = edges[i].second.first; | ||
| int b = edges[i].second.second; | ||
|
|
||
| // 사이클이 발생하지 않는 경우에만 MST에 포함시키기 | ||
| if(findParent(a) != findParent(b)){ | ||
| unionParent(a, b); | ||
| result += cost; | ||
| } | ||
| } | ||
|
|
||
| cout << result; | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <utility> | ||
| #include <algorithm> | ||
| #include <cmath> | ||
| #define MAX 1001 | ||
| using namespace std; | ||
|
|
||
| int n; // 별의 개수 | ||
| int parent[MAX]; // 루트 노드를 저장하는 테이블 | ||
| vector<pair<float, float>> coord; // 별의 좌표 | ||
| vector<pair<float, pair<int, int>>> edges; // 정점 a와 b 사이의 거리 c | ||
| float result = 0; // 최소 비용 | ||
|
|
||
| // 특정 원소가 속한 집합 찾아내기 | ||
| int findParent(int x){ | ||
| // 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀 호출 | ||
| if(x == parent[x]) return x; | ||
| return parent[x] = findParent(parent[x]); | ||
| } | ||
|
|
||
| // 두 원소가 속한 집합을 합치기 | ||
| void unionParent(int a, int b){ | ||
| a = findParent(a); | ||
| b = findParent(b); | ||
|
|
||
| // 더 작은 번호가 부모 노드가 되도록 | ||
| if(a < b) parent[b] = a; | ||
| else parent[a] = b; | ||
| } | ||
|
|
||
| int main(){ | ||
| ios_base::sync_with_stdio(0); | ||
| cin.tie(0); | ||
|
|
||
| cin >> n; // 별의 개수 | ||
|
|
||
| for(int i = 0; i < n; i++){ | ||
| parent[i] = i; // 부모 테이블 초기화 | ||
| } | ||
|
|
||
| for(int i = 0; i < n; i++){ | ||
| float x, y; | ||
| cin >> x >> y; | ||
| coord.push_back({x, y}); // 별의 좌표 | ||
| } | ||
|
|
||
| for(int i = 0; i < n; i++){ | ||
| float x1 = coord[i].first; | ||
| float y1 = coord[i].second; | ||
|
|
||
| for(int j = i + 1; j < n; j++){ | ||
| float x2 = coord[j].first; | ||
| float y2 = coord[j].second; | ||
|
Comment on lines
+49
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 float 자료형을 거의 안썼던 것 같은데 (제 기억에는 소수점이 부정확,,, 했던 것 같기도 ,,?) 혹시 사용하신 이유 여쭤봐도 괜찮을까요? 배우려구요! |
||
|
|
||
| // 두 정점 사이의 거리 구하기 | ||
| float dist = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2)); | ||
| edges.push_back({dist, {i, j}}); | ||
| } | ||
| } | ||
|
|
||
| // 간선의 비용에 따라 오름차순 정렬 | ||
| sort(edges.begin(), edges.end()); | ||
|
|
||
| for(int i = 0; i < edges.size(); i++){ | ||
| float cost = edges[i].first; | ||
| int a = edges[i].second.first; | ||
| int b = edges[i].second.second; | ||
|
|
||
| if(findParent(a) != findParent(b)){ | ||
| unionParent(a, b); | ||
| result += cost; | ||
| } | ||
| } | ||
|
|
||
| cout << result; | ||
|
|
||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elec배열을 초기화해주는 로직이 없는데 괜찮을까요? 선언하면서 동시에 0으로 초기화해둬도 좋을 것 같아요!