Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion dohyeondol1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
| 14์ฐจ์‹œ | 2025.05.16 | ํž™ | [๋” ๋งต๊ฒŒ](https://school.programmers.co.kr/learn/courses/30/lessons/42626)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/54|
| 15์ฐจ์‹œ | 2025.05.22 | DFS | [์„์œ  ์‹œ์ถ”](https://school.programmers.co.kr/learn/courses/30/lessons/250136)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/60|
| 16์ฐจ์‹œ | 2025.05.24 | BFS | [ํ† ๋งˆํ† ](https://www.acmicpc.net/problem/7576)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/61|
| 17์ฐจ์‹œ | 2025.06.26 | ๊ธฐํ•˜ํ•™ | [์„ ๋ถ„ ๊ต์ฐจ 2](https://www.acmicpc.net/problem/17387)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/65|
| 17์ฐจ์‹œ | 2025.06.26 | ๊ธฐํ•˜ํ•™ | [์„ ๋ถ„ ๊ต์ฐจ 2](https://www.acmicpc.net/problem/17387)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/65|
| 18์ฐจ์‹œ | 2025.07.04 | ๊ธฐํ•˜ํ•™ | [Water Testing](https://www.acmicpc.net/problem/16057)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/70|
| 19์ฐจ์‹œ | 2025.07.09 | ๊ทธ๋ž˜ํ”„ ์ด๋ก  | [์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ](https://www.acmicpc.net/problem/1197)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/76|
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;

int prim(int V, const vector<vector<pair<int, int>>>& adj) {
vector<bool> visited(V, false);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;

int totalWeight = 0;
pq.push({0, 0});

while(!pq.empty()) {
auto [weight, u] = pq.top();
pq.pop();

if(visited[u]) continue;
visited[u] = true;
totalWeight += weight;

for(auto& [w, v] : adj[u]) {
if(!visited[v]) {
pq.push({w, v});
}
}
}

return totalWeight;
}

int main() {
cin.tie(nullptr)->sync_with_stdio(false);

int V, E;
cin >> V >> E;
vector<vector<pair<int, int>>> adj(V);

for(int i = 0; i < E; ++i) {
int A, B, C;
cin >> A >> B >> C;

A--; B--;
adj[A].push_back({C, B});
adj[B].push_back({C, A});
}

int mstWeight = prim(V, adj);
cout << mstWeight << '\n';
return 0;
}