forked from ritikbanger/Hacktoberfest2022-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
27 lines (25 loc) · 747 Bytes
/
dijkstra.cpp
File metadata and controls
27 lines (25 loc) · 747 Bytes
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
vector <int> dijkstra(int V, vector<vector<int>> adj[], int S)
{
// Code here
priority_queue<pair<int,int>,vector<pair<int,int>>, greater<pair<int,int>>> minh ;
vector<int> dis(V,INT_MAX-1) ;
dis[S] = 0 ;
minh.push({0,S}) ;
while(!minh.empty())
{
auto it = minh.top() ;
minh.pop() ;
int a = it.first ;
int b = it.second ;
for(auto i : adj[b])
{
int m = i[0] ;
int n = i[1] ;
if(dis[m] > a + n ){
dis[m] = a+n ;
minh.push({dis[m],m}) ;
}
}
}
return dis ;
}