c++解决动态规划最短路径

问题陈述

进行动态规划求解0到12的最短路径
c++解决动态规划最短路径_第1张图片

代码实现

#include
const int N = 20;
const int MAX = 1000;
int arc[N][N];               
int Backpath(int n);
int creatGraph();

int main()
{
	int n = creatGraph( );
	int pathLen = Backpath(n);
	cout<<"最短路径的长度是:"<<pathLen<<endl;
	return 0;
}
int creatGraph()
{
	int i, j, k;
	int weight;
	int vnum, arcnum;
	cout<<"请输入顶点的个数和边的个数:";
	cin>>vnum>>arcnum;
	for (i = 0; i < vnum; i++)
		for (j = 0; j < vnum; j++)
			arc[i][j] = MAX;
	for (k = 0; k < arcnum; k++)
	{
		cout<<"请输入边的两个顶点和权值:";
		cin>>i>>j>>weight;
		arc[i][j] = weight;
	}
	return vnum;
}
int Backpath(int n)
{
	int i, j, temp;
	int cost[N];
	int path[N];
	for(i = 0; i < n; i++)
	{
		cost[i]=MAX;
		path[i]=-1;
	}
	cost[0] = 0;
	for(j = 1; j < n; j++)
	{
		for(i = j - 1; i >= 0; i--)
		{
			if(arc[i][j]+cost[i]<cost[j])
			{cost[j]=arc[i][j]+cost[i];
			path[j]=i;
			}
		}
	}
	cout<<n-1;         
	i = n-1;
	while (path[i] >= 0)
	{
		cout<<"<-"<<path[i];
		i = path[i];
	}
	cout<<endl;
	return cost[n-1];
}

实验结果

c++解决动态规划最短路径_第2张图片
c++解决动态规划最短路径_第3张图片
如图所示最短路径为:0,2,5,8,11,12
最短路径值为:3+7+3+2+3=18

你可能感兴趣的:(c++解决动态规划最短路径)