From 27c9035899cc3f36e99be76e3230860ee1d1b840 Mon Sep 17 00:00:00 2001 From: gyunho Date: Sat, 18 Jan 2025 17:52:28 +0900 Subject: [PATCH] 2025-01-18 --- g0rnn/README.md | 3 +++ g0rnn/dijkstra/16-g0rnn.cpp | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 g0rnn/dijkstra/16-g0rnn.cpp diff --git a/g0rnn/README.md b/g0rnn/README.md index c772e8f..06fc54b 100644 --- a/g0rnn/README.md +++ b/g0rnn/README.md @@ -17,4 +17,7 @@ | 13차시 | 2025.01.05 | 백트래킹 | [스타트와 링크](https://www.acmicpc.net/problem/14889) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/52 | +| 16차시 | 2025.01.18 | 다익스트라 | [최단경로](https://www.acmicpc.net/problem/1753) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/62 | + + --- diff --git a/g0rnn/dijkstra/16-g0rnn.cpp b/g0rnn/dijkstra/16-g0rnn.cpp new file mode 100644 index 0000000..8870ed1 --- /dev/null +++ b/g0rnn/dijkstra/16-g0rnn.cpp @@ -0,0 +1,50 @@ +// +// Created by 김균호 on 2025. 1. 18.. +// +#include +#include +#include +#include +using namespace std; + +int v, e, s; +int a, b, w; +vector>> graph; +priority_queue> pq; // { weight, node } +vector dist(v+1, INT_MAX); + +// pq의 공간 복잡도는 O(E) +void dijkstra() { + dist = vector(v+1, INT_MAX); + dist[s] = 0; + pq.emplace(0, s); + + while(!pq.empty()) { + int d = -pq.top().first; // 음수로 저장되어 있음 -> 최소 우선순위 큐 + int cur = pq.top().second; pq.pop(); + + if (dist[cur] < d) continue; + for(auto& [adj, wei] : graph[cur]) { + //relax + if (d + wei < dist[adj]) { + dist[adj] = d + wei; + pq.emplace(-d - wei, adj); + } + } + } +} + +int main() { + cin >> v >> e >> s; + graph = vector>>(v+1); + for(int i=0;i>a>>b>>w; + graph[a].emplace_back(b, w); + } + dijkstra(); + for (int i=1;i