#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VERTICES 20
#define INFINITY 9999
typedef struct {
char name[50];
char code[10];
char intro[200];
} Vertex;
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];
void initGraph() {
// 初始化邻接矩阵
// 将所有路径长度初始化为 INFINITY
for (int i = 0; i < MAX_VERTICES; i++) {
for (int j = 0; j < MAX_VERTICES; j++) {
if (i == j) {
adjacencyMatrix[i][j] = 0;
}
else {
adjacencyMatrix[i][j] = INFINITY;
}
}
}
}
void addEdge(int start, int end, int weight) {
// 添加路径
adjacencyMatrix[start][end] = weight;
adjacencyMatrix[end][start] = weight; // 如果是无向图,需要加上这一句
}
void removeEdge(int start, int end) {
// 删除路径
adjacencyMatrix[start][end] = INFINITY;
adjacencyMatrix[end][start] = INFINITY;
}
void addVertex(Vertex vertices[], int* vertexCount, char name[], char code[], char intro[]) {
// 添加新景点
Vertex newVertex;
strcpy(newVertex.name, name);
strcpy(newVertex.code, code);
strcpy(newVertex.intro, intro);
vertices[*vertexCount] = newVertex;
(*vertexCount)++;
}
void removeVertex(Vertex vertices[], int* vertexCount, int index) {
// 删除景点
for (int i = index; i < *vertexCount - 1; i++) {
vertices[i] = vertices[i + 1];
}
(*vertexCount)--;
}
void updateVertex(Vertex vertices[], int index, char name[], char code[], char intro[]) {
// 修改景点信息
strcpy(vertices[index].name, name);
strcpy(vertices[index].code, code);
strcpy(vertices[index].intro, intro);
}
void displayVertexInfo(Vertex vertex) {
// 显示景点信息
printf("Name: %s\n", vertex.name);
printf("Code: %s\n", vertex.code);
printf("Introduction: %s\n", vertex.intro);
printf("\n");
}
void dijkstra(int start, int end, int vertexCount) {
// 存储最短路径的距离和访问状态
int distance[MAX_VERTICES], visited[MAX_VERTICES];
// 存储最短路径的前驱节点
int path[MAX_VERTICES];
// 初始化距离和访问状态
for (int i = 0; i < vertexCount; i++) {
distance[i] = INFINITY;
visited[i] = 0;
path[i] = -1;
}
distance[start] = 0;
// 迭代求解最短路径
for (int count = 0; count < vertexCount - 1; count++) {
// 找到当前未访问的距离最小的节点
int minDistance = INFINITY, nextVertex = -1;
for (int i = 0; i < vertexCount; i++) {
if (!visited[i] && distance[i] < minDistance) {
minDistance = distance[i];
nextVertex = i;
}
}
// 标记该节点为已访问
visited[nextVertex] = 1;
// 更新与当前节点相邻节点的最短距离
for (int i = 0; i < vertexCount; i++) {
if (!visited[i] && adjacencyMatrix[nextVertex][i] != INFINITY &&
distance[nextVertex] + adjacencyMatrix[nextVertex][i] < distance[i]) {
distance[i] = distance[nextVertex] + adjacencyMatrix[nextVertex][i];
path[i] = nextVertex;
}
}
}
// 输出最短路径
if (distance[end] == INFINITY) {
printf("No path found.\n");
}
else {
printf("Shortest path length: %d\n", distance[end]);
printf("Shortest path: %d", end);
int temp = end;
while (path[temp] != -1) {
printf(" <- %d", path[temp]);
temp = path[temp];
}
printf("\n");
}
}
void menu()
{
printf("**********************************************\n");
printf("**********欢迎来到太原学院导游管理系统********\n");
printf("******输入“1”查询校园内景点的相关信息*******\n");
printf("******输入“2”查询校园内景点之间的最短路径***\n");
printf("******输入“0”退出太原学院导游管理系统*******\n");
printf("**********************************************\n");
}
int main() {
Vertex vertices[MAX_VERTICES];
int vertexCount = 0;
// 初始化校园地图和景点信息
initGraph();
// 在这里添加初始化景点信息和路径的代码
//memcpy(vertices[1].name, "图书馆", sizeof(char) * 2);
//strcpy(vertices[1].code, "1");
//strcpy(vertices[1].intro, "caochang");
addVertex(vertices, &vertexCount, "图书馆", "0", "这是学生阅读的书籍地方");
addVertex(vertices, &vertexCount, "超市", "1", "这是学生购物的地方");
addVertex(vertices, &vertexCount, "食堂", "2", "这是学生吃饭的地方");
addVertex(vertices, &vertexCount, "教学楼1", "3", "这是艺术系学生上课的地方");
addVertex(vertices, &vertexCount, "教学楼2", "4", "这是数学系学生上课的地方");
addVertex(vertices, &vertexCount, "教学楼3", "5", "这是文旅系学生上课的地方");
addVertex(vertices, &vertexCount, "教学楼4", "6", "这是财经系学生上课的地方");
addVertex(vertices, &vertexCount, "教学楼5", "7", "这是计科系学生上课的地方");
addEdge(0, 1, 10);
addEdge(1, 2, 5);
addEdge(0, 2, 1);
addEdge(0, 3, 6);
addEdge(0, 4, 2);
addEdge(5, 7, 5);
addEdge(5, 1, 8);
addEdge(2, 7, 14);
addEdge(6, 2, 7);
addEdge(0, 7, 18);
addEdge(2, 7, 7);
int l;
int input = 0;
do
{
menu();
int startVertex, endVertex; // 起始顶点的索引
int a = scanf("%d", &input);
if (a == 1)
{
switch (input)
{
case 1:
printf("请输入所要查找的校园景点\n");
printf("****查询***图书馆***相关信息请输入'0'****\n");
printf("****查询***超市***相关信息请输入'1'****\n");
printf("****查询***食堂*****相关信息请输入'2'****\n");
printf("****查询***教学楼1*****相关信息请输入'3'****\n");
printf("****查询***教学楼2*****相关信息请输入'4'****\n");
printf("****查询***教学楼3*****相关信息请输入'5'****\n");
printf("****查询***教学楼4*****相关信息请输入'6'****\n");
printf("****查询***教学楼5*****相关信息请输入'7'****\n");
scanf("%d", &l);
displayVertexInfo(vertices[l]);
break;
case 2:
printf("请输入所要查找的校园两景点之间的最短路径\n");
printf("****图书馆***用'0'表示****\n");
printf("****超市*****用'1'表示****\n");
printf("****食堂*****用'2'表示****\n");
printf("***教学楼1***用'3'表示****\n");
printf("***教学楼2***用'4'表示****\n");
printf("***教学楼3***用'5'表示****\n");
printf("***教学楼4***用'6'表示****\n");
printf("***教学楼5***用'7'表示****\n");
scanf("%d %d", &startVertex, &endVertex);
dijkstra(startVertex, endVertex, vertexCount);
break;
case 0:
printf("感谢您使用太原学院导游管理系统\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
}
} while (input);
return 0;
}
void initGraph();函数是为了存放两个节点的路径的长度
void dijkstra(int start, int end, int vertexCount);函数为迪杰斯特拉算法的实现