-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
24 lines (24 loc) · 765 Bytes
/
Copy pathDijkstra.cpp
File metadata and controls
24 lines (24 loc) · 765 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
template<class T>
T dijkstra(T src, vector<vector<pair<T,T>>> &g,T &destination,int totalNodes){
vector<T> dist(totalNodes+1,inf);
set<pll>s;
dist[src]=0;
s.insert(pair<T,T>{0,src});
while(!s.empty()){
auto p =*s.begin();
T node_dis = p.first;
T node = p.second;
s.erase(s.begin());
for(auto &child:g[node]){
if(node_dis+child.second<dist[child.first]){
auto f = s.find(pair<T,T>{dist[child.first],child.first});
if(f!=s.end()){
s.erase(f);
}
dist[child.first]=node_dis+child.second;
s.insert(pll{dist[child.first],child.first});
}
}
}
return dist[destination];
}