数据结构与算法|dijkstra 路径搜索算法的c++简单实现

参考 【数据结构与算法|dijkstra 路径搜索算法的c++简单实现】基于知乎文章https://zhuanlan.zhihu.com/p/51112799
数据结构与算法|dijkstra 路径搜索算法的c++简单实现
文章图片

codes

#include #include #include #include using namespace std; int main(int argc, char *args[]) { #define COL_SIZE 6 #define ROW_SIZE 6 int map[COL_SIZE][ROW_SIZE]; for(int col=0; col > route; for(int index=0; index t_set; t_set.push_back(start_index); std::cout << "start dist min array:" << " "<< dist_min[0] << " "<< dist_min[1] << " "<< dist_min[2] << " "<< dist_min[3] << " "<< dist_min[4] << " "<< dist_min[5] << std::endl; while(t_set.size() < ROW_SIZE) { //find_left_min_value int next_t_node = -1; for(int index=0; index dist_min[index]) { next_t_node = index; } } } } if(next_t_node == -1) { std::cout << "cannot find next t node " << std::endl; break; }std::cout << "find next t node index=" << next_t_node << std::endl; t_set.push_back(next_t_node); for(auto &t:t_set) std::cout << " " << t ; std::cout << std::endl; auto &one_route_node = route.at(next_t_node); //one_route_node.push_back(next_t_node); print_route(); int prev_weight = 0; auto iter = one_route_node.begin(); auto iter_end = one_route_node.end(); for(; iter!=iter_end; ) { auto iter_next = iter+1; if(iter_next == iter_end) break; if(map[*iter][*iter_next] == INT_MAX) //not reach { prev_weight = INT_MAX; break; } else prev_weight += map[*iter][*iter_next]; iter++; } if(prev_weight == INT_MAX) { std::cout << "cur path not reach, continue " << std::endl; continue; }//update dist min array for(int row=0; row

    推荐阅读