数据结构与算法|dijkstra 路径搜索算法的c++简单实现
参考 【数据结构与算法|dijkstra 路径搜索算法的c++简单实现】基于知乎文章https://zhuanlan.zhihu.com/p/51112799
图
文章图片
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
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- Docker应用:容器间通信与Mariadb数据库主从复制
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量
- 第326天
- Shell-Bash变量与运算符
- 画解算法(1.|画解算法:1. 两数之和)
- 逻辑回归的理解与python示例
- Guava|Guava RateLimiter与限流算法
- 我和你之前距离
- CGI,FastCGI,PHP-CGI与PHP-FPM