Graph.h
#pragma once
#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
#define Maxsize 20
//定义Vex结构体,存储图的顶点信息
struct Vex {
int num;//景点编号
char name[20];//景点名字
char desc[1024];//景点介绍
};
//定义Edge结构体,存储图的边的信息
struct Edge {
int vex1, vex2;//边的两个顶点
int weight;//权值
};
//定义图的存储
struct Graph {
int m_aAdjMatrix[Maxsize][Maxsize];//邻接矩阵
Vex m_aVexs[Maxsize];//顶点数组
int m_nVexNum;//顶点个数
};//Graph对象,用于存储景区景点图
typedef struct Path {
int road[20];//保存当前路径
Path* next;//保存下一条路径
}*PathList;//指向结构体的指针
void Init();//初始化图结构
int InsertVex(Vex sVex);//将顶点添加到顶点数组中
int InsertEdge(Edge sEdge);//将边保存在邻接矩阵中
Vex GetVex(int nVex);//查询指定顶点信息
int FindEdge(int nVex, Edge aEdge[]);//查询与指定顶点相连的边
int GetVexnum();//获取当前顶点数
void DFS(int nVex, bool bVisited[], int& nIndex, PathList& pList);
void DFSTraverse(int nVex, PathList& pList);
// 寻找最短路径
int FindShortpath(int nVexStart, int nVexEnd, Edge aPath[]);
// 通过 Prim 算法构建最小生成树
void FindMinTree(Edge aPath[]);
#endif // !GRAPH_H_INCLUDED
Tourism.h
#pragma once
#ifndef TOURISM_H_INCLUDED
#define TOURISM_H_INCLUDED
void CreatGraph();//读取文件,创建景区景点图
void GetSpotInfo();//查询指定景点信息,显示到相邻景点的距离
void TravelPath();//
void FindShortPath();
void DesignPath();
#endif // !TOURISM_H_INCLUDED
Graph.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Graph.h"
#include"Tourism.h"
#include<cstring>
#include<iostream>
Graph m_Graph;
#define Maxsize 20
#define ok 1
#define error 0
using namespace std;
void Init()
{
m_Graph.m_nVexNum = 0;
for (int i = 0; i < Maxsize; i++) {
m_Graph.m_aVexs[i].num = -1;
memset(m_Graph.m_aVexs[i].name, '\0', sizeof(m_Graph.m_aVexs[i].name));
memset(m_Graph.m_aVexs[i].desc, '\0', sizeof(m_Graph.m_aVexs[i].desc));
for (int j = 0; j < Maxsize; j++) {
m_Graph.m_aAdjMatrix[i][j] = 0;
}
}
}
int InsertVex(Vex sVex)
{
if (m_Graph.m_nVexNum == 32767)
return error;
m_Graph.m_aVexs[m_Graph.m_nVexNum++] = sVex;
return ok;
}
int InsertEdge(Edge sEdge)
{
m_Graph.m_aAdjMatrix[sEdge.vex1][sEdge.vex2] = sEdge.weight;
m_Graph.m_aAdjMatrix[sEdge.vex2][sEdge.vex1] = sEdge.weight;
return ok;
}
Vex GetVex(int nVex)
{
return m_Graph.m_aVexs[nVex];
}
int FindEdge(int nVex, Edge aEdge[])
{
return 0;
}
int GetVexnum()
{
return 0;
}
void DFS(int nVex, bool isVisited[], int& nIndex, PathList& pList)
{
isVisited[nVex] = true; // 修改为已访问
pList->road[nIndex++] = nVex; // 访问顶点nVex
int num = 0; // 被访问过的节点数
for (int i = 0; i < m_Graph.m_nVexNum; i+