Skip to content

Commit

Permalink
Merge pull request #62 from AlgoLeadMe/16-g0rnn
Browse files Browse the repository at this point in the history
16-g0rnn
  • Loading branch information
g0rnn authored Feb 13, 2025
2 parents bf12044 + 27c9035 commit 6423439
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions g0rnn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@



| 16μ°¨μ‹œ | 2025.01.18 | λ‹€μ΅μŠ€νŠΈλΌ | [μ΅œλ‹¨κ²½λ‘œ](https://www.acmicpc.net/problem/1753) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/62 |


---
50 changes: 50 additions & 0 deletions g0rnn/dijkstra/16-g0rnn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Created by κΉ€κ· ν˜Έ on 2025. 1. 18..
//
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;

int v, e, s;
int a, b, w;
vector<vector<pair<int, int>>> graph;
priority_queue<pair<int, int>> pq; // { weight, node }
vector<int> dist(v+1, INT_MAX);

// pq의 곡간 λ³΅μž‘λ„λŠ” O(E)
void dijkstra() {
dist = vector<int>(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<vector<pair<int, int>>>(v+1);
for(int i=0;i<e;i++) {
cin>>a>>b>>w;
graph[a].emplace_back(b, w);
}
dijkstra();
for (int i=1;i<dist.size();i++) {
if (dist[i] == INT_MAX) cout<<"INF\n";
else cout<<dist[i]<<'\n';
}
return 0;
}

0 comments on commit 6423439

Please sign in to comment.