Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16-g0rnn #62

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions g0rnn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |


---
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;
}
Loading