node parent( node_list& v, node n ) { if( n >= v.size( ) ) { for( unsigned i = v.size( ); i <= n; i++ ) { v.push_back( i ); } } if( v[ n ] == n ) return n; v[ n ] = parent( v, v[ n ] ); return v[ n ]; }
void Dijkstra(int start, const node_list &node_list, vector<float> &min_distance, vector<int> &previous) { int n = node_list.size(); min_distance.clear(); min_distance.resize(n, max_distance); min_distance[start] = 0; previous.clear(); previous.resize(n, -1); set< pair<float, int> > vertex_queue; vertex_queue.insert(make_pair(min_distance[start], start)); while (!vertex_queue.empty()) { float dist = vertex_queue.begin()->first; int u = vertex_queue.begin()->second; vertex_queue.erase(vertex_queue.begin()); const vector<neighbor> &neighbors = node_list[u]; for (vector<neighbor>::const_iterator it = neighbors.begin(); it != neighbors.end(); it++) { int v = it->next; float distance = it->distance; float distance_through_u = dist + distance; if (distance_through_u < min_distance[v]) { vertex_queue.erase(make_pair(min_distance[v], v)); min_distance[v] = distance_through_u; previous[v] = u; vertex_queue.insert(make_pair(min_distance[v], v)); } } } }