-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortest_path.cpp
More file actions
40 lines (39 loc) · 1.08 KB
/
Copy pathShortest_path.cpp
File metadata and controls
40 lines (39 loc) · 1.08 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
// Dijkstra's algorithm (Greedy approach)
// uses priority_queue, used when all edge weights are positive
#include<vector>
#include<iostream>
#include<queue>
#include<climits>
#define ll long long
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<pair<ll,ll>>> graph(n);
vector<ll> distance(n, LLONG_MAX);
for(int i = 0; i < m; ++i) {
ll from, to, cost;
cin >> from >> to >> cost;
graph[--from].push_back({cost, --to});
}
distance[0] = 0;
priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> nodeHolder;
nodeHolder.push({0, 0});
while(!nodeHolder.empty()) {
pair<ll,ll> curr = nodeHolder.top();
nodeHolder.pop();
ll currNode = curr.second;
for(pair<ll,ll> pr : graph[currNode]) {
ll costToAdjNode = pr.first;
ll adjNode = pr.second;
if(distance[currNode] + costToAdjNode < distance[adjNode]) {
distance[adjNode] = distance[currNode] + costToAdjNode;
nodeHolder.push({distance[adjNode], adjNode});
}
}
}
for(ll dist : distance) {
cout << dist << " ";
}
return 0;
}