转载:https://blog.csdn.net/qq_35644234/article/details/60875818

Floyd算法,实质是三重循环,借道中转

 1 //三重循环,用于计算每个点对的最短路径
 2     //dis[row][temp] + dis[temp][col] 意思是  v1-v7 = v1-v3-v3-v7   
 3     int temp = 0;      //temp就是借道,实现最外层循环
 4     int select = 0;
 5     for (temp = 0; temp < this->vexnum; temp++) {
 6         for (row = 0; row < this->vexnum; row++) {     //中间层循环
 7             for (col = 0; col < this->vexnum; col++) {
 8                 //为了防止溢出,所以需要引入一个select值
 9                 select = (dis[row][temp] == INT_MAX || dis[temp][col] == INT_MAX) ? INT_MAX : (dis[row][temp] + dis[temp][col]);
10                 if (this->dis[row][col] > select) {
11                     //更新我们的D矩阵
12                     this->dis[row][col] = select;
13                     //更新我们的P矩阵
14                     this->path[row][col] = this->path[row][temp];
15                 }
16             }
17         }
18     }
View Code

Dijkstra算法,实质就是贪婪算法,每一步只取最小的

详细讲解:https://blog.csdn.net/qq_35644234/article/details/60870719