forked from anishpatil31/CP-Templates-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra Algorithm.cpp
39 lines (33 loc) · 961 Bytes
/
Dijkstra Algorithm.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
// Code for finding single source shortest path using dijkstra algorithm.
// Time Complexity - O(n.logn + m.logn) where n is the number of vertices and m is the number of edges in the graph.
// Used for directed/undirected graphs with non-negative edge weights.
int dist[LIM], parent[LIM];
vii adj[LIM];
void dijkstra(int s)
{
for (int i = 0; i < LIM; i++)
{
dist[i] = INF;
parent[i] = -1;
}
dist[s] = 0;
set<pii> nodes;
nodes.insert({0, s});
while (!nodes.empty())
{
int v = nodes.begin()->second;
nodes.erase(nodes.begin());
for (auto edge : adj[v])
{
int to = edge.first;
int len = edge.second;
if (dist[v] + len < dist[to])
{
nodes.erase({dist[to], to});
dist[to] = dist[v] + len;
parent[to] = v;
nodes.insert({dist[to], to});
}
}
}
}