1.背景
基于下图找出任意两点间最短路,并输出路径
采用Dijistra+邻接矩阵实现
#include<bits/stdc++.h>
using namespace std;
#define MAXV 40 //最大城市数
int prearr[40];//前驱节点数组
double cost[MAXV][MAXV];//邻接矩阵用于存储无向加权图
double dis[MAXV];//距离数组用于记录从某点出发到达各点的最短路径
bool vis[MAXV];//访问数组记录前面是否已经求出到该点的最短路
void init()
{
fill(dis,dis+40,100);//初始化距离设为足够大
fill(vis,vis+40,0);
fill(cost[0],cost[0]+40*40,100);//无关系的两条路距离设为足够大
cost[7][1] = 0.5;//存图
cost[8][7] = 2.5;
cost[16][7] = 0.5;
cost[16][15] = 1;
cost[23][16] =10;
cost[8][2] = 1.5;
cost[24][16] = 3;
cost[3][2] = 3.5;
cost[9][8] = 4.5;
cost[17][16] = 2.5;
cost[25][24] = 5;
cost[9][2] = 3;
cost[25][16] = 4;
cost[31][24] = 4;
cost[31][25] = 2;
cost[25][17] = 3;
cost[17][9] = 1.5;
cost[4][3] = 1;
cost[10][9] = 4;
cost[18][17] = 1;
cost[26][25] = 2;
cost[32][25] = 3;
cost[18][9] = 2;
cost[9][4] = 2;
cost[10][4] = 2;
cost[18][10] = 1.5;
cost[26][18] = 6;
cost[32][26] = 1;
cost[37][36] = 20;
cost[33][32] = 7;
cost[27][26] = 4.5;
cost[19][18] = 2;
cost[11][10] = 1;
cost[5][4] = 1;
cost[32][27] = 3;
cost[19][10] = 2.5;
cost[11][4] = 1;
cost[37][33] = 1;
cost[33][27] = 2.5;
cost[27][19] = 2;
cost[19][11] = 3;
cost[11][5] = 1;
cost[38][37] = 9;
cost[34][33] = 7.5;
cost[28][27] = 3;
cost[20][19] = 1.5;
cost[12][11] = 2;
cost[6][5] = 5;
cost[11][6] = 4.5;
cost[19][12] = 3;
cost[28][19] = 2.5;
cost[34][27] = 3;
cost[34][28] = 6;
cost[28][20] = 1.5;
cost[20][12] = 2;
cost[12][6] = 1.5;
cost[13][12] = 1;
cost[21][20] = 1;
cost[29][28] = 4.5;
cost[34][29] = 7.5;
cost[20][13] = 1.5;
cost[29][21] = 2;
cost[21][13] = 0.5;
cost[14][13] = 4;
cost[22][21] = 3;
cost[22][13] = 3.5;
cost[35][30] = 1;
cost[30][22] = 1;
cost[22][14] = 1.5;
for(int i = 1;i<=38;i++)
for(int j = 1;j<=38;j++)
{
if(i==j)
cost[i][j] = 0;
if(i>j)
cost[j][i] = cost[i][j];
}
}
int st;
int outputpath(int s)
{
char name[40][40] = {{"0"},{"HongKong"},{"FuZhou"},{"NingBo"},{"HangZhou"},{"ShangHai"},{"QingDao"},{"ShenZhen"},{"XiaMen"},{"NanChang"},{"HeFei"},{"NanJing"},{"JiNan"},{"TianJin"},{"DaLian"},{"AoMen"},{"GuangZhou"},{"ChangSha"},{"WuHan"},{"ZhenZhou"},{"ShiJiaZhuang"},{"BeiJing"},{"ShenYang"},{"HaiKou"},{"NanNing"},{"GuiYang"},{"ChongQing"},{"XiAn"},{"TaiYuan"},{"HuHeHaoTe"},{"ChangChun"},{"KunMing"},{"ChengDU"},{"LanZhou"},{"YinChuan"},{"HaErBin"},{"LaSa"},{"XiNing"},{"WuLuMuQi"}};
int a[40];
int n = 1;
while(s!=st)
{
a[n] = s;
s = prearr[s];
n++;
}
printf("The min path is :\n");
printf("%s",name[st]);
for(int i = n-1;i>=1;i--)
{
printf(" -> %s",name[a[i]]);
}
}
void dijkstra(int s)//该算法适用于边权不为负的情况
{
for(int j = 1;j<=38;j++)
{
if(cost[s][j]!=100)//取一边即可
{
dis[j] = cost[s][j];
prearr[j] = s;
}
}
dis[s] = 0;//源点距离置为0
while(1)
{
int v = -1;
for(int u = 1;u<=38;u++)//循环找出周边点距离最小的
{
if(!vis[u]&&(v==-1||dis[u]<dis[v]))
v = u;
}
if(v==-1)
break;
vis[v] = 1;
for(int u = 1;u<=38;u++)
{
if(dis[v]+cost[v][u]<dis[u])
{
dis[u] = dis[v] + cost[v][u];
prearr[u] = v;
}
}
}
cout<<"Please input destination city \n";
int choice;
scanf("%d",&choice);
printf("The min distance is %.1lf\n",dis[choice]);
outputpath(choice);
}
int main()
{
init();
cout<<"HongKong-1 FuZhou-2 NingBo-3 HangZhou-4 \nShangHai-5 QingDao-6 ShenZhen-7 XiaMen-8 \nNanChang-9 HeFei-10 NanJing-11 JiNan-12 \nTianJin-13 Dalian-14 AoMen-15 GuangZhou-16 \nChangSha-17 WuHan-18 ZhenZhou-19 ShiJiaZhuang-20 \nBeiJing-21 ShenYang-22 HaiKou-23 NanNing-24 \nGuiYang-25 ChongQing-26 XiAn-27 TaiYuan-28\nHuHeHaoTe-29 ChangChun-30 KunMing-31 ChengDu-32 \nLanZhou-33 YinChuan-34 HaErBin-35 LaSa-36 \nXiNing-37 WuLuMuQi-38 \n";
cout<<"Please input depature city\n";
cin>>st;
dijkstra(st);
}