Skip to content

Commit

Permalink
Merge pull request #70 from AlgoLeadMe/19-miniron-v
Browse files Browse the repository at this point in the history
19-miniron-v
  • Loading branch information
miniron-v authored Mar 6, 2024
2 parents 20e5f24 + 8cb9a79 commit 014f1c9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions miniron-v/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
| 16차시 | 2024.02.18 | 수학 | [](https://www.acmicpc.net/problem/1081) | [#61](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/61) |
| 17차시 | 2024.02.21 | DP | [제곱수의 합](https://www.acmicpc.net/problem/1699) | [#64](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/64) |
| 18차시 | 2024.02.24 | 이분 탐색 | [K번째 수](https://www.acmicpc.net/problem/1300) | [#66](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/66) |
| 19차시 | 2024.02.27 | 다익스트라 | [최단경로](https://www.acmicpc.net/problem/1753) | [#70](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/70) |
---
58 changes: 58 additions & 0 deletions miniron-v/다익스트라/1753-최단경로.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>
#include <vector>
#include <queue>

const int INF = 3000001;

std::vector<int> dijkstra(int start, int n, std::vector<std::vector<std::pair<int, int>>>& graph) {
std::vector<int> min_weight(n + 1, INF);
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> pq;

min_weight[start] = 0;
pq.push({ 0, start });

while (pq.empty() == false) {
int cur_weight = pq.top().first;
int cur_node = pq.top().second;
pq.pop();

for (auto& pair : graph[cur_node]) {
int next_node = pair.first;
int next_weight = cur_weight + pair.second;

if (next_weight < min_weight[next_node]) {
min_weight[next_node] = next_weight;
pq.push({ next_weight, next_node});
}
}
}

return min_weight;
}

int main() {
// 입력
int V, E, start;
std::cin >> V >> E >> start;

std::vector<std::vector<std::pair<int, int>>> graph(V + 1);
for (int i = 0; i < E; ++i) {
int u, v, w;
std::cin >> u >> v >> w;

graph[u].push_back({v, w});
}

// 계산
std::vector<int> min_weight = dijkstra(start, V, graph);

// 출력
for (int i = 1; i < min_weight.size(); ++i) {
if (min_weight[i] == INF) {
std::cout << "INF\n";
continue;
}

std::cout << min_weight[i] << "\n";
}
}

0 comments on commit 014f1c9

Please sign in to comment.