Skip to content

Commit 2fe55ac

Browse files
authored
Create 11 April Dijkstra Algorithm (#764)
2 parents c28c4fa + 82937c2 commit 2fe55ac

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

11 April Dijkstra Algorithm

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
vector<int> dijkstra(int V, vector<vector<int>> &edges, int src) {
4+
// Code here
5+
vector<vector<pair<int,int>>>v(V);
6+
for(auto &x:edges){
7+
v[x[0]].push_back({x[2],x[1]});
8+
v[x[1]].push_back({x[2],x[0]});
9+
}
10+
vector<int>ans(V,INT_MAX);
11+
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>pq;
12+
pq.push({0,src});
13+
ans[src]=0;
14+
while(!pq.empty()){
15+
auto x=pq.top();
16+
int i=x.second;
17+
int d=x.first;
18+
pq.pop();
19+
for(auto &y:v[i]){
20+
if(ans[y.second]>y.first+d){
21+
ans[y.second]=y.first+d;
22+
pq.push({ans[y.second],y.second});
23+
}
24+
}
25+
}
26+
return ans;
27+
}
28+
};

0 commit comments

Comments
 (0)