Skip to content

Instantly share code, notes, and snippets.

@sunprinceS
Last active December 5, 2017 20:28
Show Gist options
  • Save sunprinceS/dea6b01e77cb3fe1f40c6779569d592e to your computer and use it in GitHub Desktop.
Save sunprinceS/dea6b01e77cb3fe1f40c6779569d592e to your computer and use it in GitHub Desktop.
std::vector<int> dijkstra(const Graph& g,int s){
std::vector<int> dist(g.num_v,INT_MAX);
std::vector<bool> b_set(g.num_v,false);
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > dist_pq;
dist[s] = 0;
dist_pq.push(make_pair(dist[s],s));
while(!dist_pq.empty()){
int cur_node = dist_pq.top().second;
dist_pq.pop();
if(!b_set[cur_node]){
b_set[cur_node] = true;
for(auto e: g.nodes[cur_node].out_edges){
if( !b_set[e.edge.second]&& dist[e.edge.second] > dist[cur_node] + e.weight){
dist[e.edge.second] = dist[cur_node] + e.weight;
dist_pq.push(make_pair(dist[e.edge.second],e.edge.second));
}
}
}
}
return dist;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment