forked from piyush-kash/Hacktober2021-cpp-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra-SingleSourceShortestPath.cpp
82 lines (68 loc) · 2.18 KB
/
dijkstra-SingleSourceShortestPath.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Dijkstra's Single Source Shortest Path Algorithm
#include <bits/stdc++.h>
#define INF 10000
using namespace std;
class Graph {
map<int, list<pair<int, int>>> m;
bool isDirected;
public:
Graph(bool directs) {
isDirected = directs;
}
void addEdge(int v1, int v2, int weight) {
m[v1].push_back(make_pair(v2, weight));
if (!isDirected) m[v2].push_back(make_pair(v1, weight));
}
void print() {
cout << "Graph represented as adjacency list:\n";
for (auto member : m) {
cout << member.first << " --> ";
for (auto i : member.second) {
cout << " (" << i.first << ", " << i.second << ")";
}
cout << "\n";
}
cout << "\n";
}
void DijkstraPath(int source) {
unordered_map<int, int> distance;
for (auto element : m) {
distance[element.first] = INT_MAX;
}
set<pair<int, int>> newset; // (Distance, Node)
distance[source] = 0;
newset.insert(make_pair(0, source));
while (!newset.empty()) {
auto pair = *(newset.begin()); // get first pair
int element = pair.second;
int dist = pair.first;
newset.erase(newset.begin());
for (auto cpair : m[element]) {
if (dist + cpair.second < distance[cpair.first]) {
int destination = cpair.first;
auto first = newset.find(make_pair(distance[destination], destination));
if (first != newset.end()) newset.erase(first);
distance[destination] = dist + cpair.second;
newset.insert(make_pair(distance[destination], destination));
}
}
}
cout << "Dijkstra distances: "
<< "\n";
for (auto dist : distance) {
cout << dist.first << " -> " << dist.second << "\n";
}
}
};
int main() {
bool isDirected = false;
Graph g(isDirected);
g.addEdge(1, 2, 1);
g.addEdge(1, 4, 7);
g.addEdge(1, 3, 4);
g.addEdge(4, 3, 2);
g.addEdge(3, 2, 1);
g.print();
g.DijkstraPath(1);
return 0;
}