/
DijkstraAlgoUsingSet.cpp
62 lines (45 loc) · 1.66 KB
/
DijkstraAlgoUsingSet.cpp
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
Problem statement
You have been given an undirected graph of ‘V’ vertices (labeled 0,1,..., V-1)
and ‘E’ edges. Each edge connecting two nodes (‘X’,’Y’) will have a weight denoting
the distance between node ‘X’ and node ‘Y’.
Your task is to find the shortest path distance from the source node,
which is the node labeled as 0, to all vertices given in the graph.
//code
#include <bits/stdc++.h>
#include<unordered_map>
#include<list>
#include<set>
vector<int> dijkstra(vector<vector<int>> &vec, int vertices, int edges, int source) {
//printing the adjcacency List
unordered_map<int, list<pair<int,int> > > adj;
for(int i=0;i<edges;i++){
int u = vec[i][0];
int v = vec[i][1];
int w = vec[i][2];
adj[u].push_back(make_pair(v,w));
adj[v].push_back(make_pair(u,w));
}
vector<int> dist(vertices);
for(int i=0;i<vertices;i++)
dist[i] = INT_MAX;
set<pair<int,int>> st;
dist[source] = 0;
st.insert(make_pair(0,source));
while(!st.empty()){
auto top = *(st.begin());
int nodeDistance = top.first;
int topNode = top.second;
st.erase(st.begin());
for(auto neighbour : adj[topNode]){
if(nodeDistance + neighbour.second < dist[neighbour.first]){
auto record = st.find(make_pair(dist[neighbour.first], neighbour.first));
if(record != st.end()){
st.erase(record);
}
dist[neighbour.first] = nodeDistance + neighbour.second;
st.insert(make_pair(dist[neighbour.first], neighbour.first));
}
}
}
return dist;
}