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
- bfs
- 정석
- 문제풀이
- 북리뷰
- coding
- Computer science
- 알고리즘
- cs
- vector
- DP
- Stack
- 백준
- 컴퓨터공학과
- OS
- 스택
- 개발
- 오퍼레이팅시스템
- 오에스
- 코테
- Operating System
- 너비우선탐색
- 컴공
- 코딩
- 구현
- 브루트포스
- 정석학술정보관
- 컴공과
- c++
- 그래프
- 자료구조
Archives
- Today
- Total
Little Jay
[C++] 백준 5972번 - 택배 배송 본문
간단한 다익스트라를 활용한 웰노운 문제
크게 어려운 문제는 아니었지만 다익스트라가 무향그래프일때 동시에 그래프를 업데이트 하는 것을 잊지 말자.
#include <bits/stdc++.h>
#define endl '\n'
#define INF 987654321
#define pii pair<int, int>
using namespace std;
int n, m;
vector<pii> v[50001];
int dist[50001];
priority_queue<pii, vector<pii>, greater<>> pq;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b, w; cin >> a >> b >> w;
v[a].push_back({ w, b });
v[b].push_back({ w, a });
}
for (int i = 1; i <= n; i++) {
dist[i] = INF;
}
dist[1] = 0;
pq.push({ dist[1], 1 });
while (!pq.empty()) {
int distance = pq.top().first;
int curr = pq.top().second;
pq.pop();
if (dist[curr] < distance) continue;
for (int i = 0; i < v[curr].size(); i++) {
int next_dist = v[curr][i].first;
int next = v[curr][i].second;
if (distance + next_dist < dist[next]) {
dist[next] = distance + next_dist;
pq.push({ dist[next], next });
}
}
}
cout << dist[n] << endl;
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 13273번 - 로마숫자 (0) | 2022.07.30 |
---|---|
[C++] 백준 9009번 - 피보나치 (0) | 2022.07.25 |
[C++] 백준 11559번 - Puyo Puyo (0) | 2022.07.21 |
[C++] 백준 2174 - 로봇 시뮬레이션 (0) | 2022.07.19 |
[C++] 백준 20055번 - 컨베이어 벨트 위의 로봇 (0) | 2022.07.18 |
Comments