Floyd算法使用递归输出path路径

定义一个邻接矩阵

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∠(°ゝ°) 。

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值