-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra's shortest path
35 lines (32 loc) · 1.2 KB
/
Dijkstra's shortest path
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
// problem url: https://www.codingninjas.com/codestudio/problems/920469?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website&leftPanelTab=0
// mode: Medium
// Dijkstra's shortest path
#include <bits/stdc++.h>
struct comp{
bool operator()(pair<int,int> a, pair<int,int> b){
return a.second > b.second;
}
};
vector<int> dijkstra(vector<vector<int>> &vec, int vertices, int edges, int source) {
vector<pair<int,int>> adgList[vertices];
for(vector<int> v: vec){
adgList[v[0]].push_back({v[1], v[2]});
adgList[v[1]].push_back({v[0], v[2]});
}
vector<int> minDis(vertices, INT_MAX);
priority_queue<pair<int,int>, vector<pair<int,int>>, comp> pq;
pq.push({source, 0});
minDis[source] = 0;
while(!pq.empty()){
int currNode = pq.top().first;
int currDisTillNode = pq.top().second;
pq.pop();
for(pair<int,int> neighbour: adgList[currNode]){
if(currDisTillNode + neighbour.second < minDis[neighbour.first]){
minDis[neighbour.first] = currDisTillNode + neighbour.second;
pq.push({neighbour.first, minDis[neighbour.first]});
}
}
}
return minDis;
}