|
|
|
@ -95,7 +95,7 @@
|
|
|
|
|
|
|
|
|
|
# 3. 系统实现
|
|
|
|
|
|
|
|
|
|
说明所使用的语言、开发工具等。
|
|
|
|
|
采用C++、利用VSCode实现。
|
|
|
|
|
|
|
|
|
|
介绍项目的文件结构,以及主要函数的功能。
|
|
|
|
|
|
|
|
|
@ -120,21 +120,118 @@ struct LNode {
|
|
|
|
|
|
|
|
|
|
## 3.2 核心算法的实现
|
|
|
|
|
|
|
|
|
|
描述算法的实现方法。
|
|
|
|
|
核心算法Dijkstra
|
|
|
|
|
算法floyd
|
|
|
|
|
|
|
|
|
|
可以配合程序代码加以说明。如:
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
// 冒泡排序
|
|
|
|
|
void bubble_sort(T a[], int n)
|
|
|
|
|
如以下代码,采用迪杰斯特拉算法实现求取路径最短值
|
|
|
|
|
int Ppath2(MatGrath &G,int path[],int i,int v) //前向递归查找路径上的顶点
|
|
|
|
|
{
|
|
|
|
|
......
|
|
|
|
|
int k;
|
|
|
|
|
k=path[i];
|
|
|
|
|
if (k==v)
|
|
|
|
|
return k; //找到了起点则返回
|
|
|
|
|
Ppath2(G,path,k,v); //找顶点k的前一个顶点
|
|
|
|
|
printf("%s->",G.vexs[k].sight);//输出顶点k
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int danyuan(MatGrath &G,int v)//求两点之间的最短路径
|
|
|
|
|
{
|
|
|
|
|
int dist[MAXV],path[MAXV];
|
|
|
|
|
int s[MAXV];
|
|
|
|
|
int mindis,i,j,u;
|
|
|
|
|
for (i=1; i<=G.vexnum; i++)
|
|
|
|
|
{
|
|
|
|
|
dist[i]=G.arc[v][i].length; //距离初始化
|
|
|
|
|
s[i]=0; //s[]置空
|
|
|
|
|
if (G.arc[v][i].length<INF) //路径初始化
|
|
|
|
|
path[i]=v;
|
|
|
|
|
else
|
|
|
|
|
path[i]=-1;
|
|
|
|
|
}
|
|
|
|
|
s[v]=1;
|
|
|
|
|
path[v]=0; //源点编号v放入s中
|
|
|
|
|
for (i=1; i<=G.vexnum; i++) //循环直到所有顶点的最短路径都求出
|
|
|
|
|
{
|
|
|
|
|
mindis=INF; //mindis置最小长度初值
|
|
|
|
|
for (j=1; j<=G.vexnum; j++) //选取不在s中且具有最小距离的顶点u
|
|
|
|
|
if (s[j]==0 && dist[j]<mindis)
|
|
|
|
|
{
|
|
|
|
|
u=j;
|
|
|
|
|
mindis=dist[j];
|
|
|
|
|
}
|
|
|
|
|
s[u]=1; //顶点u加入s中
|
|
|
|
|
for (j=1; j<=G.vexnum; j++) //修改不在s中的顶点的距离
|
|
|
|
|
if (s[j]==0)
|
|
|
|
|
if (G.arc[u][j].length<INF && dist[u]+G.arc[u][j].length<dist[j])
|
|
|
|
|
{
|
|
|
|
|
dist[j]=dist[u]+G.arc[u][j].length;
|
|
|
|
|
path[j]=u;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(i=1; i<=G.vexnum; i++)
|
|
|
|
|
if (s[i]==1&&v!=i)
|
|
|
|
|
{
|
|
|
|
|
printf(" 从%s到%s的最短路径长度为:%d米\t路径为:",G.vexs[v].sight,G.vexs[i].sight,dist[i]);
|
|
|
|
|
printf("%s->",G.vexs[v].sight); //输出路径上的起点
|
|
|
|
|
Ppath2(G,path,i,v); //输出路径上的中间点
|
|
|
|
|
printf("%s\n",G.vexs[i].sight); //输出路径上的终点
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Ppath1(MatGrath &G,int path[][MAXV],int v,int w) //前向递归查找路径上的顶点
|
|
|
|
|
{
|
|
|
|
|
int k;
|
|
|
|
|
k=path[v][w];
|
|
|
|
|
if (k==-1) return 0; //找到了起点则返回
|
|
|
|
|
Ppath1(G,path,v,k); //找顶点i的前一个顶点k
|
|
|
|
|
printf("%s->",G.vexs[k].sight);
|
|
|
|
|
Ppath1(G,path,k,w); //找顶点k的前一个顶点j
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
如下采用floyd算法求给出的两点之间的最少费用
|
|
|
|
|
void ShortestMoney(MatGrath &G,int v,int w)//求花费最少的路径
|
|
|
|
|
{
|
|
|
|
|
int A[MAXV][MAXV],path[MAXV][MAXV];
|
|
|
|
|
int i,j,k;
|
|
|
|
|
for (i=0; i<G.vexnum; i++)
|
|
|
|
|
for (j=0; j<G.vexnum; j++)
|
|
|
|
|
{
|
|
|
|
|
A[i][j]=G.arc[i][j].money;
|
|
|
|
|
path[i][j]=-1; //i到j没有边
|
|
|
|
|
}
|
|
|
|
|
for (k=0; k<G.vexnum; k++)
|
|
|
|
|
{
|
|
|
|
|
for (i=0; i<G.vexnum; i++)
|
|
|
|
|
for (j=0; j<G.vexnum; j++)
|
|
|
|
|
if (A[i][j]>A[i][k]+A[k][j])
|
|
|
|
|
{
|
|
|
|
|
A[i][j]=A[i][k]+A[k][j];
|
|
|
|
|
path[i][j]=k;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (A[v][w]==INF)
|
|
|
|
|
{
|
|
|
|
|
if (v!=w)
|
|
|
|
|
printf("从%d到%d没有路径\n",v,w);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf(" 从%s到%s路径费用:%d元人民币 路径:",G.vexs[v].sight,G.vexs[w].sight,A[v][w]);
|
|
|
|
|
printf("%s->",G.vexs[v].sight); //输出路径上的起点
|
|
|
|
|
Ppath1(G,path,v,w); //输出路径上的中间点
|
|
|
|
|
printf("%s\n",G.vexs[w].sight); //输出路径上的终点
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
对该算法的时间和空间复杂度进行分析。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|