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
- Computer science
- 정석
- cs
- 문제풀이
- 코테
- 개발
- 자료구조
- 컴공과
- 구현
- 스택
- 브루트포스
- 너비우선탐색
- 북리뷰
- 컴퓨터공학과
- 알고리즘
- DP
- Operating System
- bfs
- c++
- 오퍼레이팅시스템
- 오에스
- OS
- 정석학술정보관
- coding
- 백준
- 컴공
- 코딩
- vector
- 그래프
- Stack
Archives
- Today
- Total
Little Jay
[C++] 백준 1753 - 최단경로 본문
다익스트라 문제
기록용
#include <bits/stdc++.h>
#define endl '\n'
#define INF 987654321
#define pii pair<int, int>
using namespace std;
int V, E, start;
vector<pii> graph[20010];
int dist[20010] = { 0, };
priority_queue<pii, vector<pii>, greater<pii>> pq;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> V >> E;
cin >> start;
for (int i = 0; i < E; i++) {
int u, v, w; cin >> u >> v >> w;
graph[u].push_back({ w, v });
}
for (int i = 1; i < V + 1; i++)
dist[i] = INF;
dist[start] = 0;
pq.push({ dist[start], start });
while (!pq.empty()) {
int distance = pq.top().first;
int current_vertex = pq.top().second;
pq.pop();
if (dist[current_vertex] < distance) continue;
for (int i = 0; i < graph[current_vertex].size(); i++) {
int next_vertex = graph[current_vertex][i].second;
int next_weight = graph[current_vertex][i].first;
if (distance + next_weight < dist[next_vertex]) {
dist[next_vertex] = distance + next_weight;
pq.push({ dist[next_vertex], next_vertex });
}
}
}
for (int i = 1; i < V + 1; i++) {
if (dist[i] == INF) cout << "INF" << endl;
else cout << dist[i] << endl;
}
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 16955번 - 오목, 이길 수 있을까? (0) | 2022.05.13 |
---|---|
[C++] 백준 23304번 - 아카라카 (0) | 2022.05.13 |
[C++] 백준 17127번 - 벚꽃이 정보섬에 피어난 이유 (0) | 2022.05.09 |
[C++] 백준 10552번 - DOM (0) | 2022.05.03 |
[C++] 백준 13565번 - 침투 (0) | 2022.05.03 |
Comments