最短路径课程设计(安阳旅游导航系统)C语言完整代码

功能:
       1.显示地图
       2.输入起始点和目的地序号
       3.最短路径长度
       4.最短路径
        5.退出

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
 #define wuqiongda 10000//假设无穷大为10000
//存储最短路径值
int ShortestPathvalue[10][10] = { 0 };
//存储具体路径
int ShortestPathmatrix[10][10] = { 0 };
//地点信息
char _mapName[10][50] = { "殷墟","袁林","文字博物馆","长春观","羑里城","岳飞庙","红旗渠",
"太行大峡谷","二帝陵","明福寺"
};

//距离信息,_distance[0][1] = 50;代表从下标为0到下表为1地点距离为50
int _distance[10][10] = { 0 };
//边表结点
typedef struct EdgeNode {
    //顶点对应的下标
    int adjvex;
    //权值
    int weight;
    //指向下一个邻接点
    struct EdgeNode* next;
} edgeNode;
 
//顶点表结点
typedef struct VertexNode {
    //顶点数据
    char data[50];
    //边表头指针
    edgeNode* firstedge;
}AdjList[100];
 
//集合
typedef struct {
    AdjList adjList;
    //顶点数和边数
    int numVertexes, numEdges;
} GraphAdjList;
 

    void ShowALGraph(GraphAdjList* G);
    void Test();
    //初始化地图
    void InitMap(GraphAdjList* G);
    //创建地图
    void CreateALGraph(GraphAdjList* G);
    //计算各个顶点之间最短路径
    void ShortestPath_Floyd(GraphAdjList* G, int P[10][10], int D[10][10]);
    //输出最短路径
    void ShowShortestResult(int begin, int end);

//创建地图
void CreateALGraph(GraphAdjList* G) {
    edgeNode* e;
    int i,j;
    //读入顶点信息,建立顶点表
    for ( i = 0; i < G->numVertexes; i++)
    {
        //读入顶点信息
        strcpy(G->adjList[i].data, _mapName[i]);
        //将边表置为空表
        G->adjList[i].firstedge = NULL;
    }
    //建立边表(头插法)
    for ( i = 0; i < G->numVertexes; i++)
    {
        for ( j = 0; j < i; j++)
        {
            int temp;
            if (_distance[i][j] != 0 || _distance[j][i] != 0)
            {
                if (_distance[i][j] != 0)  //如果矩阵_distance[i][j]这个位置 != 0,赋值给_distance[j][i] 
                {
                    temp = _distance[i][j];
                    _distance[j][i] = _distance[i][j];
                }
                else                       //如果矩阵_distance[j][i]这个位置 != 0,赋值给_distance[i][j] 
                {
                    temp = _distance[j][i];
                    _distance[i][j] = _distance[j][i];
                }
               // 因为是无向图,所以要插2次 
                //把新节点头插法插入到邻接表 
                e =  (struct EdgeNode*)malloc(sizeof(struct EdgeNode));
                e->adjvex = j;
                e->next = G->adjList[i].firstedge;
                e->weight = temp;
                G->adjList[i].firstedge = e;
               // 把新节点头插法插入到邻接表 
                e =  (struct EdgeNode*)malloc(sizeof(struct EdgeNode));
                e->adjvex = i;
                e->next = G->adjList[j].firstedge;
                e->weight = temp;
                G->adjList[j].firstedge = e;
            }
 
        }
    }
 
}
 
//初始化地图基本数据
void InitMap(GraphAdjList* G) {
    //输入顶点数和边数
    G->numVertexes = 10;
    G->numEdges = 12;
    //输入边的权值
    _distance[0][1] = 13;
    _distance[0][2] = 15;
    _distance[2][4] = 5;
    _distance[4][5] = 14;
    _distance[5][6] = 11;
    _distance[7][8] = 9;
    _distance[7][9] = 6;
    _distance[8][9] = 8;
    _distance[9][1] = 7;
    _distance[8][1] = 10;
    _distance[3][5] = 16;
    _distance[1][3] = 17;
}
 
//弗洛伊德算法
void ShortestPath_Floyd(GraphAdjList* G, int P[10][10], int D[10][10]) {//D数组存权值,
    int k,v,w;
    //初始化D(存储最短路径值)与P(P数组存终点第一个前驱的下标) 
    for ( v = 0; v < G->numVertexes; ++v)
    {
        for ( w = 0; w < G->numVertexes; ++w)
        {
            if (_distance[v][w] == 0 && v != w) {
                _distance[v][w] = wuqiongda;   //如果没有路径
            }
            D[v][w] = _distance[v][w];
            P[v][w] = w;
        }
    }
    
    for ( k = 0; k < G->numVertexes; ++k)        //k为中间顶点下标 
    {
        for ( v = 0; v < G->numVertexes; ++v)      //v开始顶点下标
        {
            for ( w = 0; w < G->numVertexes; ++w)   //w结束顶点下标
            {
                if (D[v][w] > D[v][k] + D[k][w])
                {
                    D[v][w] = D[v][k] + D[k][w];
                    P[v][w] = P[v][k];
                }
            }
        }
    }
 
}
 
 //输出最短路径长度
 void length(int begin, int end){
 	
printf("\n%s",_mapName[begin]);  printf("-->");   printf("%s",_mapName[end] ); printf(" 的最短距离为");   printf("%d\n",ShortestPathvalue[begin][end]); 	

 }
 
 //输出最短路径
void ShowShortestResult(int begin, int end) {
    int temp;
    temp = ShortestPathmatrix[begin][end];
    printf("具体路径为:"); printf("%s",_mapName[begin]);  printf("——>");
    while (temp != end) {
        printf("%s",_mapName[temp]); printf("——>");
        temp = ShortestPathmatrix[temp][end];
    }
    
    printf("%s\n", _mapName[end]);
}
 
 
 void show(){
    printf("                         --------------------------17km-------------------------\n");
    printf("                         |                                                     |\n");
 	printf("                 ----1.袁林---13km--0.殷墟----15km---2.文字博物馆            3.长春观\n");
 	printf("                 |       |                             |                       |\n");
	printf("                 |       |                            5km                     16km\n");
	printf("                 |       |                             |                       |\n");
	printf("                 |      7km                          4.羑里城-------14km----5.岳飞庙------11km---6.红旗渠\n");
	printf("                 |       |\n");
	printf("                 |       |\n");
	printf("                10km     |\n");
	printf("                 |       |\n");
	printf("                 |       |\n");
 	printf("                 |    9.明福寺------------------6km--------------------7.太行大峡谷\n");
    printf("                 |       |                                                   |\n");
	printf("                 |      8km                                                  |\n");
	printf("                 |       |                                                   |\n");
 	printf("                 -----8.二帝陵------------------9km--------------------------|");
 }
 
int main() {
    int begin, end, i;
    GraphAdjList* GA = (GraphAdjList*)malloc(sizeof(GraphAdjList));
    InitMap(GA);
    CreateALGraph(GA);
    ShortestPath_Floyd(GA, ShortestPathmatrix, ShortestPathvalue);
    int option=1;
 
	while (option) {
		printf("\n\n------------------------------安阳旅游导航系统功能界面如下---------------------------------------\n");
        printf("             ---------------------1.显示地图--------------------             \n");
        printf("             ---------------------2.输入起始点和目的地序号--------------------   \n");
		printf("             ---------------------3.最短路径长度--------------------             \n");
        printf("             ---------------------4.最短路径----------------------             \n");
        printf("             ---------------------5.退出-------------------------             \n\n");
        scanf("%d",&option);
	
	
	switch(option){
	
	case 1:
			{  
			show();
        break;
			}
	
	case 2:
			{
				printf("请输入起始点和目的地的序号(以空格间隔):");
			scanf("%d%d",&begin,&end);
				break;
			}
	
		case 3:
			{
			length(begin,end);
			break;
			}
			
			case 4:
			{
				ShowShortestResult(begin,end);
				break;
			}
			
			
			
			case 5:break;
		
	}
	if(option==5) break;
	}
	
 
    return 0;
}
 

eddc307429554b11ab99416a6299e14d.png

 

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值