-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdjikstra.cpp
48 lines (44 loc) · 1.37 KB
/
djikstra.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
/*
Problem link: https://practice.geeksforgeeks.org/problems/implementing-dijkstra-set-1-adjacency-matrix/1
Editorial: https://takeuforward.org/data-structure/dijkstras-algorithm-using-priority-queue-g-32/
Video: https://youtu.be/V6H1qAeB-l4
*/
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
//Function to find the shortest distance of all the vertices
//from the source vertex S.
vector <int> dijkstra(int V, vector<vector<int>> adj[], int S)
{
priority_queue<pair<int, int>,
vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> dist(V+1, 1e9);
dist[S] = 0;
vector<int> parent(V+1);
for(int i = 1; i <= V; i++)
parent[i] = i;
pq.push({0, S});
while(!pq.empty())
{
auto it = pq.top();
int u = it.second;
int currDist = it.first;
pq.pop();
for(auto v: adj[u])
{
int node = v[0];
int edgeWeight = v[1];
if(currDist + edgeWeight < dist[node])
{
dist[node] = currDist + edgeWeight;
pq.push({dist[node], node});
parent[node] = u;
}
}
}
// if there is no path b/w src and destination
return dist;
}
};