forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.cpp
122 lines (100 loc) · 2.45 KB
/
Dijkstra.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph.
It is applicable for:
* Both directed and undirected graphs
* All edges must have None-Negative weights
* Graph must be connected
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef pair<int, int> pii;
// Graph -> Vector - Pair
vii *Graph;
// It Stores the Distance of every other node from start.
vi Distance;
void Dijkstra(int start, int N)
{
// min heap
priority_queue<pii, vector<pii>, greater<pii>> Q;
Distance.assign(N, INF);
Distance[start] = 0;
Q.push({0, start});
while (!Q.empty())
{
int v1 = Q.top().second;
Q.pop();
for (auto &c : Graph[v1])
{
int v2 = c.first;
int weight = c.second;
if (Distance[v2] > Distance[v1] + weight)
{
Distance[v2] = Distance[v1] + weight;
Q.push({Distance[v2], v2});
}
}
}
}
int main()
{
// N - total no of nodes, M - no. of edges
// v1, v2 and weight are the end vertices and the weight associated with an edge
// start is the sStarting Point from where we have to find Shortest Path
int N, M, v1, v2, weight, start;
cin >> N >> M;
Graph = new vii[N + 1];
for (int i = 0; i < M; ++i)
{
cin >> v1 >> v2 >> weight;
Graph[v1].push_back({v2, weight});
Graph[v2].push_back({v1, weight});
}
cin >> start;
Dijkstra(start, N);
for (int i = 0; i < N; i++)
cout << "Distance From " << start << " to Node " << i << " is " << Distance[i] << " " << endl;
cout << endl;
return 0;
}
/*
Test Cases:
Input 1 :
5
5
0 1 17
0 2 2
0 3 9
0 4 24
0 5 28
1
Output 1 :
Distance From 1 to Node 0 is 17
Distance From 1 to Node 1 is 0
Distance From 1 to Node 2 is 19
Distance From 1 to Node 3 is 26
Distance From 1 to Node 4 is 41
Input 2 :
4
4
0 1 11
0 2 2
0 3 31
0 4 24
0 5 28
1
Output 2 :
Distance From 0 to Node 0 is 0
Distance From 0 to Node 1 is 11
Distance From 0 to Node 2 is 2
Distance From 0 to Node 3 is 31
Time complexity : O(ElogV) where E is no. of Edges and V are no. of Vertices
Space Complexity: O(V^2)
*/