校园导游咨询
【问题描述】
设计一个校园导游程序,为来访的客人提供各种信息查询服务。
【基本要求】
- 设计你的学校的校园平面图,所含景点不少于10个。以图中顶点表示学校各景点,存放景点名称、代号、简介等信息;以边表示路径,存放路径长度等相关信息。
- 为来访客人提供图中任意景点的问路查询,即查询任意两个景点之间的一条最短的简单路径。
- 为来访客人提供图中任意景点相关信息的查询。
【测试数据】
由读者根据实际情况指定。
【实现提示】
一般情况下,校园的道路是双向通行的,可设校园平面图是一个无向网。顶点和边均含有相关信息。
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
#define OK 1
#define ERROR 0
#define MaxInt 32767 //极大值
#define MVNum 50 //最大顶点数
typedef int Status;
typedef string VerTexType; //假设顶点的数据类型为string类型
typedef string InfoType;
typedef int ArcType; //假设边的权值类型为整型
bool visited[MVNum]; //访问标志数组,值为"false"时代表该顶点没被访问,值为"true"时代表该顶点已被访问
typedef struct vex{
VerTexType name;
InfoType otherinfo;
}vex, *vexptr;
//图的邻接矩阵存储表示
typedef struct{
vex vexs[MVNum]; //顶点表
ArcType arcs[MVNum] [MVNum]; //邻接矩阵
int vexnum, arcnum; //图的当前点数和边数
}AMGraph;
//获取顶点位置:若G中存在顶点u,则返回该顶点在图中的位置;否则返回其他信息
int LocateVex(AMGraph G, VerTexType u){
int index = -1; //原始下标,没找到元素返回-1
for(int i = 0; i < G.vexnum; i++){ //遍历顶点数组
if(u == G.vexs[i].name){
index = i; //记录元素下标
}
}
return index; //返回下标
}
//采用邻接矩阵表示法创建无向网G,表示地图
Status CreateUDN(AMGraph &G){
G.vexnum = 13;
G.arcnum = 18;
G.vexs[0].name = "菊苑饭堂";
G.vexs[0].otherinfo = "菊苑饭堂简介";
G.vexs[1].name = "梅苑饭堂";
G.vexs[1].otherinfo = "梅苑饭堂简介";
G.vexs[2].name = "兰苑饭堂";
G.vexs[2].otherinfo = "兰苑饭堂简介";
G.vexs[3].name = "商中";
G.vexs[3].otherinfo = "商中简介";
G.vexs[4].name = "图书馆";
G.vexs[4].otherinfo = "图书馆简介";
G.vexs[5].name = "理科南楼";
G.vexs[5].otherinfo = "理科南楼简介";
G.vexs[6].name = "理科北楼";
G.vexs[6].otherinfo = "理科北楼简介";
G.vexs[7].name = "文新楼";
G.vexs[7].otherinfo = "文新楼简介";
G.vexs[8].name = "文清楼";
G.vexs[8].otherinfo = "文清楼简介";
G.vexs[9].name = "文俊东楼";
G.vexs[9].otherinfo = "文俊东楼简介";
G.vexs[10].name = "文俊西楼";
G.vexs[10].otherinfo = "文俊西楼简介";
G.vexs[11].name = "行政东楼";
G.vexs[11].otherinfo = "行政东楼简介";
G.vexs[12].name = "行政西楼";
G.vexs[12].otherinfo = "行政西楼简介";
for(int i = 0; i < G.vexnum; ++i){ //初始化邻接矩阵,边的权值均置为极大值MaxInt
for(int j = 0; j < G.vexnum; ++j){
G.arcs[i][j] = MaxInt;
}
}
G.arcs[0][2] = 200;
G.arcs[0][3] = 300;
G.arcs[1][2] = 200;
G.arcs[1][3] = 1000;
G.arcs[2][3] = 800;
G.arcs[3][4] = 400;
G.arcs[3][8] = 400;
G.arcs[4][5] = 530;
G.arcs[4][6] = 540;
G.arcs[4][8] = 350;
G.arcs[4][11] = 250;
G.arcs[4][12] = 250;
G.arcs[5][6] = 20;
G.arcs[7][8] = 10;
G.arcs[7][9] = 30;
G.arcs[7][10] = 30;
G.arcs[9][10] = 10;
G.arcs[11][12] = 50;
G.arcs[2][0] = 200;
G.arcs[3][0] = 300;
G.arcs[2][1] = 200;
G.arcs[3][1] = 1000;
G.arcs[3][2] = 800;
G.arcs[4][3] = 400;
G.arcs[8][3] = 400;
G.arcs[5][4] = 530;
G.arcs[6][4] = 540;
G.arcs[8][4] = 350;
G.arcs[11][4] = 250;
G.arcs[12][4] = 250;
G.arcs[6][5] = 20;
G.arcs[8][7] = 10;
G.arcs[9][7] = 30;
G.arcs[10][7] = 30;
G.arcs[10][9] = 10;
G.arcs[12][11] = 50;
return OK;
}
//输出景点介绍
void PrintInfo(AMGraph G, VerTexType a){
for(int i = 0; i < G.vexnum; i++){
if(G.vexs[i].name == a){
cout<< G.vexs[i].otherinfo << endl;
return;
}
}
cout << "没有此景点。" << endl;
return;
}
//景点a1到景点a2的最短路径
void ShortestPath(AMGraph G, VerTexType a1, VerTexType a2){
int v, w;
int n = G.vexnum; //n为G中顶点个数
bool S[n]; //记录从源点v0到终点vi是否已被确定最短路径长度,true表示确定,false表示尚未确定
int Path[n]; //记录从源点v0到终点vi的当前最短路径上vi的直接前驱顶点序号
int D[n]; //记录从源点v0到终点vi的当前最短路径长度
int v1 = LocateVex(G, a1);
int v2 = LocateVex(G, a2);
for(v = 0; v < n; ++v){ //n个顶点依次初始化
S[v] = false; //S初始化为空集
D[v] = G.arcs[v1][v]; //将v0到各个终点的最短路径长度初始化为弧上的权值
if(D[v] < MaxInt) Path[v] = v1; //如果v0和v之间有弧,则将v的前驱置为v0
else Path[v] = -1; //如果v0和v之间无弧,则将v的前驱置为-1
}
S[v1] = true; //将v0加入S
D[v1] = 0; //源点到源点的距离为0
/*----------初始化结束,开始主循环,每次求得v0到某个顶点v的最短路径,将v加到S集----------*/
for(int i = 1; i < n; ++i){ //对其余n-1个顶点,依次进行计算
int min = MaxInt;
for(w = 0; w < n; ++w){
if(!S[w] && D[w] < min){
v = w;
min = D[w]; //选择一条当前最短路径,终点为v
}
}
S[v] = true; //将v加入S
for(w = 0; w < n; ++w){ //更新从v_0出发到集合V-S上所有顶点的最短路径长度
if(!S[w] && (D[v] + G.arcs[v][w] < D[w])){
D[w] = D[v] + G.arcs[v][w]; //更新D[w]
Path[w] = v; //更改w的前驱为v
}
}
}
int a = v2;
cout<< G.vexs[a].name;
while(Path[a] != -1){
cout<< "<-" << G.vexs[Path[a]].name;
a = Path[a];
}
}
int main(){
AMGraph G;
int m;
VerTexType v1, v2;
VerTexType b;
CreateUDN(G);
while(1){
cout << "**************************校园导游咨询***************************" << endl;
cout << "* 【1】. 问路查询 *" << endl;
cout << "* 【2】. 景点信息查询 *" << endl;
cout << "* 【3】. 退出程序 *" << endl;
cout<< "*****************************************************************" << endl;
cin>> m;
switch(m){
case 1:{
VerTexType v1, v2;
cout << "请输入起点和终点:";
cin>> v1 >> v2;
cout << v1 << "到" << v2 << "的最短路径为:";
ShortestPath(G, v1, v2);
cout<< endl;
system("pause");
system("cls");
break;
}
case 2:{
cout << "请输入要查询的景点名称:";
cin>> b;
PrintInfo(G, b);
system("pause");
system("cls");
break;
}
case 3:
return 0;
}
}
}