定义一个邻接矩阵
int graph[][7] = {
{0,2,2,Infinity,Infinity,Infinity,Infinity},
{2,0,Infinity,1,5,Infinity,Infinity},
{2,Infinity,0,6,Infinity,3,Infinity},
{Infinity,1,6,0,2,1,Infinity},
{Infinity,5,Infinity,2,0,Infinity,Infinity},
{Infinity,Infinity,3,1,Infinity,0,3},
{Infinity,Infinity,Infinity,Infinity,7,3,0}
};
具体图的信息如下图所示:
定义一个二维数组存储路径信息
int path[7][7];
//将路径初始化
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
path[i][j] = -1;
}
}
Floyd算法计算最短路径
//考虑加入第k个节点时
for (int k = 0; k < 7; k++) {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (graph[i][j] > graph[i][k]+ graph[k][j]) {
graph[i][j] = graph[k][j] + graph[i][k];
path[i][j] = k;
}
}
}
}
额外的函数
我们需要一个能返回用户输入顶点的下标(index)的函数
int returnIndex(char Location) {
const string Locations = "ABCDEFG";
//返回索引的下标
for (int i = 0; i < Locations.size(); i++) {
if (Locations[i] == Location) {
return i;
}
}
return -1;
}
递归输出路径(path)
void findPath(int path[][7], int newNum, int end) {
const string Locations = "ABCDEFG";
if (path[newNum][end] == -1) {
return ;
}
findPath(path, path[newNum][end], end);
cout << Locations[path[newNum][end]] << "--";
}
添加用户输入
char starting_point, terminal_point;
cout << "please enter your starting point(uppercase):" << "";
cin >> starting_point;
int start = returnIndex(starting_point);
cout << "please enter your terminal point(uppercase):" << "";
cin >> terminal_point;
int end = returnIndex(terminal_point);
cout << "start is :" << start << endl;
cout << "end is :" << end << endl;
完整代码如下
#include<iostream>
using namespace std;
void findPath(int path[][7], int newNum, int end) {
const string Locations = "ABCDEFG";
if (path[newNum][end] == -1) {
return;
}
//因为栈是先进后出,所以先查找,再输出这样结果就是顺序的了
findPath(path, path[newNum][end], end);
cout << Locations[path[newNum][end]] << "--";
}
int returnIndex(char Location) {
const string Locations = "ABCDEFG";
//返回索引的下标
for (int i = 0; i < Locations.size(); i++) {
if (Locations[i] == Location) {
return i;
}
}
return -1;
}
int main() {
const string Locations = "ABCDEFG"; //顶点名称
const int Infinity = 99999;
//邻接矩阵
int graph[][7] = {
{0,2,2,Infinity,Infinity,Infinity,Infinity},
{2,0,Infinity,1,5,Infinity,Infinity},
{2,Infinity,0,6,Infinity,3,Infinity},
{Infinity,1,6,0,2,1,Infinity},
{Infinity,5,Infinity,2,0,Infinity,Infinity},
{Infinity,Infinity,3,1,Infinity,0,3},
{Infinity,Infinity,Infinity,Infinity,7,3,0}
};
int path[7][7];
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
path[i][j] = -1;
}
}
//考虑加入第k个节点时
for (int k = 0; k < 7; k++) {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (graph[i][j] > graph[i][k] + graph[k][j]) {
graph[i][j] = graph[k][j] + graph[i][k];
path[i][j] = k;
}
}
}
}
cout << "最短路径数值:" << endl;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
cout << graph[i][j] << " ";
}
cout << endl;
}
cout << "最短路径要经过的顶点信息:" << endl;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
cout << path[i][j] << " ";
}
cout << endl;
}
char starting_point, terminal_point;
cout << "please enter your starting point(uppercase):" << "";
cin >> starting_point;
int start = returnIndex(starting_point);
cout << "please enter your terminal point(uppercase):" << "";
cin >> terminal_point;
int end = returnIndex(terminal_point);
cout << "start is :" << start << endl;
cout << "end is :" << end << endl;
if (start == -1 || end == -1) {
cout << "你输入了不存在的顶点信息,程序结束。" << endl;
return 0;
}
int value = path[end][start];
cout << starting_point << " to " << terminal_point << " distance is " << graph[start][end] << endl;
cout << "route guidances :" << "";
if (value == -1) {
cout << "直接通行,无需绕路" << endl;
}
else {
cout << starting_point << "--";
findPath(path, value, start);
cout << Locations[value] << "--";
cout << terminal_point << endl;
}
return 0;
}
运行结果
A到G的最短距离是7,走向是A–B–D–F–G,返回结果与预期相等正。
内容仅供参考,salute∠(°ゝ°) 。