Repository navigation
Expand file tree
/
Copy pathDijkstras_algorithm.cpp
More file actions
78 lines (62 loc) · 1.88 KB
/
Copy pathDijkstras_algorithm.cpp
File metadata and controls
78 lines (62 loc) · 1.88 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <vector>
#include <queue>
#include <limits>
#include <iostream>
using namespace std;
// const INF = INT_MAX;
vector<int> sol(int n, vector<vector<pair<int, int>>>& graph, int start) {
// Initialize distances with infinity
vector<int> dist(n, numeric_limits<int>::max()); //(n, INF)
dist[start] = 0;
// Min-heap priority queue to select the edge with the smallest weight
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.emplace(0, start);
while (!pq.empty()) {
int current_dist = pq.top().first;
int u = pq.top().second;
pq.pop();
// If the distance is greater than the recorded distance, skip processing
if (current_dist > dist[u]) continue;
// Explore neighbors
// for(auto [v,w] : graph[u]) {}
for (const auto& neighbor : graph[u]) {
int v = neighbor.first;
int weight = neighbor.second;
// Relaxation step
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.emplace(dist[v], v);
}
}
}
// Replace infinity with -1 for unreachable nodes
// for(int& d : dist) { if(d==INF) d = -1; }
for (int i = 0; i < n; i++) {
if (dist[i] == numeric_limits<int>::max()) {
dist[i] = -1;
}
}
return dist;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<pair<int, int>>> graph(n);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
graph[u].emplace_back(v, w);
}
int start;
cin >> start;
// please don't modify the main function
vector<int> result = sol(n, graph, start);
for(int i = 0; i < n; i++) {
if(i == n - 1) {
cout << result[i];
} else {
cout << result[i] << " ";
}
}
return 0;
}