Skip to content

Commit a23d392

Browse files
authoredOct 2, 2022
Create Dijkstra's
1 parent c183ee9 commit a23d392

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎Dijkstra's

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
vector <int> dijkstra(int V, vector<vector<int>> adj[], int S)
2+
{
3+
// Code here
4+
priority_queue<pair<int,int>,vector<pair<int,int>>, greater<pair<int,int>>> minh ;
5+
vector<int> dis(V,INT_MAX-1) ;
6+
dis[S] = 0 ;
7+
minh.push({0,S}) ;
8+
while(!minh.empty())
9+
{
10+
auto it = minh.top() ;
11+
minh.pop() ;
12+
int a = it.first ;
13+
int b = it.second ;
14+
15+
for(auto i : adj[b])
16+
{
17+
int m = i[0] ;
18+
int n = i[1] ;
19+
if(dis[m] > a + n ){
20+
dis[m] = a+n ;
21+
minh.push({dis[m],m}) ;
22+
}
23+
}
24+
}
25+
26+
return dis ;
27+
}

0 commit comments

Comments
 (0)
Please sign in to comment.