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 |
Tags
- 그래프
- OS
- DP
- 오에스
- 코딩
- Computer science
- 컴공과
- 브루트포스
- Stack
- cs
- 문제풀이
- 정석
- 개발
- 너비우선탐색
- Operating System
- 북리뷰
- 컴공
- coding
- vector
- c++
- bfs
- 알고리즘
- 스택
- 컴퓨터공학과
- 코테
- 정석학술정보관
- 구현
- 자료구조
- 오퍼레이팅시스템
- 백준
Archives
- Today
- Total
Little Jay
Prim Algorithm 본문
백준 1197번 https://www.acmicpc.net/problem/1197
#include <bits/stdc++.h>
#define endl '\n'
#define pii pair<int, int>
using namespace std;
int V, E;
int a, b, c;
bool visited[10001];
vector<vector<pii>> graph;
priority_queue<pii, vector<pii>, greater<>> pq;
int answer = 0;
void prim(int i) {
visited[i] = true;
for (auto u : graph[i]) {
if (!visited[u.second]) pq.push({u.first, u.second});
}
while (!pq.empty()) {
auto temp = pq.top();
pq.pop();
if (!visited[temp.second]) {
answer += temp.first;
prim(temp.second);
return;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> V >> E;
graph.resize(V + 1);
for (int i = 0; i < E; i++) {
cin >> a >> b >> c;
graph[a].push_back({ c, b });
graph[b].push_back({ c, a });
}
prim(1);
cout << answer << endl;
return 0;
}
연습용
'Univ > Algorithm' 카테고리의 다른 글
Floyd-Warshall (0) | 2022.06.04 |
---|
Comments