-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra_list.cc
66 lines (52 loc) · 1.53 KB
/
dijkstra_list.cc
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
/**
* Djikstra's shortest path algo. for adjacency list representation of
* undirected, connected and weighted graphs.
*/
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pairInt;
void djikstra(auto& adj, int v, int s) {
priority_queue<pairInt, vector<pairInt>, greater<pairInt>> heap;
vector<int> dist(v, INT_MAX);
vector<int> parent(v);
dist[s] = 0;
parent[s] = -1;
heap.push(make_pair(s, dist[s]));
while (!heap.empty()) {
int u = heap.top().first;
heap.pop();
for (auto it = adj[u].begin(); it != adj[u].end(); ++it) {
int v = (*it).first;
int dist_v = (*it).second;
// Update distance of v from source if there's a shorter path
// to v via u.
if (dist[v] > dist[u] + dist_v) {
dist[v] = dist[u] + dist_v;
// Update parent of v as u.
parent[v] = u;
// Push v and its updated distance in min heap.
heap.push(make_pair(v, dist[v]));
}
}
}
// Print the shortest path edges with their respective weights.
for (int i = 0; i < v; i++)
if (parent[i] != -1)
cout << parent[i] << " -- " << i << " : " << dist[i] << endl;
}
int main() {
int vertices, edges, source;
cin >> vertices >> edges >> source;
vector<vector<pairInt>> adj(vertices);
int u, v, wt;
for (int i = 0; i < edges; i++) {
cin >> u >> v >> wt;
adj[u].push_back(make_pair(v, wt));
adj[v].push_back(make_pair(u, wt));
}
djikstra(adj, vertices, source);
return 0;
}
// Time complexity : O(E * Log V)).
// #TODO Can be further improved by using a fibonaci heap instead of using
// a binary heap.