#include "Headers/Graph.h"
#include <climits>
//图论的功能功能函数
Graph::Graph()
{
// 初始化邻接矩阵为∞
for (int i = 0; i < PLACES; i++)
for (int j = 0; j < PLACES; j++)
AdjacencyMatrix[i][j] = INT_MAX;
// 对角线初始化为0
for (int i = 0; i < PLACES; i++)
{
AdjacencyMatrix[i][i] = 0;
vertex[i].place = "";
vertex[i].info = "";
}
}
// 输入地点信息
void Graph::setInfo(int num, string name, string info)
{
vertex[num].place = name;
vertex[num].info = info;
}
// 查询地点信息
string Graph::getInfo(int num)
{
string result = "";
result = result + "景点编号:" + to_string(num) + "\n";
result = result + "景点名称:" + vertex[num].place + "\n";
result = result + "景点介绍:" + vertex[num].info + "\n";
return result;
}
// 查询道路信息
string Graph::getPathInfo(int x, int y)
{
string result = "";
result = result + "道路起点:" + to_string(x) + " " + vertex[x].place + "\n";
result = result + "道路终点:" + to_string(y) + " " + vertex[y].place + "\n";
result = result + "道路长度:" + to_string(AdjacencyMatrix[x][y]) + "\n";
return result;
}
// 删除景点及其道路
void Graph::delInfo(int num)
{
for (int i = 0; i < PLACES; i++)
AdjacencyMatrix[i][num] = INT_MAX;
for (int j = 0; j < PLACES; j++)
AdjacencyMatrix[num][j] = INT_MAX;
AdjacencyMatrix[num][num] = 0;
vertex[num].place = "";
vertex[num].info = "";
}
// 输入新的路径
void Graph::setPath(int x, int y, int dis)
{
AdjacencyMatrix[x][y] = dis;
AdjacencyMatrix[y][x] = dis;
//邻接矩阵,双向图
}
// 删除路径
void Graph::delPath(int x, int y)
{
AdjacencyMatrix[x][y] = INT_MAX;
AdjacencyMatrix[y][x] = INT_MAX;
}
// 寻找路径,应该用了深度搜索之类的,引用sum
void Graph::getPath(int x, int y, string &result, int &sum, int (&v)[PLACES])
{
int rest[PLACES] = {0};
rest[x] = 1;
// 初始化路径权值
for (int i = 0; i < PLACES; i++)
{
dis[i].weight = AdjacencyMatrix[x][i];
cleanStack(dis[i].previous);
cleanStack(dis[i].backup);
}
for (int count = 1; count != PLACES; count++)
{
// 寻找权值最小顶点
int min = INT_MAX;
for (int i = 0; i < PLACES; i++)
if (rest[i] == 0)
{
min = i;
for (i += 1; i < PLACES; i++)
if (rest[i] == 0 and dis[min].weight > dis[i].weight)
min = i;
}
if (min == INT_MAX)
return;
else
rest[min] = 1;
// 更新路径权值
for (int i = 0; i < PLACES; i++)
{
if (rest[i] == 1)
continue;
if (AdjacencyMatrix[min][i] <= dis[i].weight - dis[min].weight)
{
if (AdjacencyMatrix[min][i] != dis[i].weight - dis[min].weight)
cleanStack(dis[i].previous);
dis[i].weight = dis[min].weight + AdjacencyMatrix[min][i];
dis[i].previous.push(min);
}
}
rest[min] = 1;
}
// 递归输出最短路径
int c = 0;
DFS(x, y, result, sum, v, c);
}
// 深度优先搜索输出路径
void Graph::DFS(int x, int y, string &result, int &sum, int (&v)[PLACES], int &c)
{
if (dis[y].previous.empty())
{
result = result + "\n" + to_string(x) + " " + vertex[x].place + " -> " + to_string(y) + " " + vertex[y].place;
sum += AdjacencyMatrix[x][y];
v[c++] = x;
v[c++] = y;
}
// 将节点所有分支搜索完
while (!dis[y].previous.empty())
{
DFS(x, dis[y].previous.top(), result, sum, v, c);
result = result + " -> " + to_string(y) + " " + vertex[y].place;
sum += AdjacencyMatrix[dis[y].previous.top()][y];
v[c++] = y;
dis[y].backup.push(dis[y].previous.top());
dis[y].previous.pop();
}
// 将前驱信息还原
while (!dis[y].backup.empty())
{
dis[y].previous.push(dis[y].backup.top());
dis[y].backup.pop();
}
}
// 输出所有简单路径
void Graph::getAllPath(int x, int y, bool refresh, string &result)
{
// 用数组保存简单路径
static int path[PLACES], count = 0, mark[PLACES] = {0}, start = x;
// 在反复调用递归函数时刷新静态变量
if (refresh)
{
for (int i = 0; i < PLACES; i++)
{
path[i] = INT_MAX;
mark[i] = 0;
}
count = 0;
start = x;
}
for (int i = 0; i < PLACES; i++)
{
if (AdjacencyMatrix[x][i] != INT_MAX and mark[i] == 0)
{
// 标记该节点以搜索过
mark[i] = 1;
if (x == i)
continue;
path[count++] = i;
// 输出路径
if (i == y)
{
// start 静态变量保存路径起始点
result = result + to_string(start) + " " + vertex[start].place;
int sum = 0;
int pos = start;
for (int p = 0; path[p] != INT_MAX; p++)
{
result = result + " -> " + to_string(path[p]) + " " + vertex[path[p]].place;
sum += AdjacencyMatrix[pos][path[p]];
pos = path[p];
}
result = result + "\n路径长度:" + to_string(sum) + "\n";
mark[i] = 0;
path[--count] = INT_MAX;
return;
}
getAllPath(i, y, false, result);
// 删除标记
mark[i] = 0;
path[--count] = INT_MAX;
}
}
}
// 多景点查询
void Graph::multiPath(int x, int y, int z, string &result)
{
// 保存每条路径
MultiRoad mr[ROADS];
for (int i = 0; i < ROADS; i++)
{
mr[i].flag = false;
mr[i].sum = 0;
mr[i].result = "";
}
int c1 = 0;
MultiRoad min;
min.flag = true;
min.sum = INT_MAX;
min.result = "";
multiGetAllPath(x, y, z, true, c1, mr);
for (int i = 0; i < c1; i++)
{
if (mr[i].flag == true && mr[i].sum < min.sum)
min = mr[i];
}
int c2 = c1;
multiGetAllPath(x, y, z, true, c2, mr);
for (int i = c1; i < c2; i++)
{
if (mr[i].flag == true && mr[i].sum < min.sum)
min = mr[i];
}
if (min.result == "")
{
string r1 = "";
string r2 = "";
int s1 = 0;
int s2 = 0;
int tmp[PLACES];
getPath(x, y, r1, s1, tmp);
getPath(y, z, r2, s2, tmp);
min.sum = s1 + s2;
min.result = r1 + r2;
getPath(x, z, r1, s1, tmp);
getPath(z, y, r2, s2, tmp);
if ((s1 + s2) < min.sum)
{
min.sum = s1 + s2;
min.result = r1 + r2;
}
else if ((s1 + s2) == min.sum)
{
min.result = min.result + "\n" + r1 + r2;
}
result = min.result + "\n路径长度:" + to_string(min.sum);
}
else
result = "\n" + min.result + "\n路径长度:" + to_string(min.sum);
}
// 多景点遍历路径
void Graph::multiGetAllPath(int x, int y, int z, bool refresh, int &c, MultiRoad (&mr)[ROADS])
{
// 用数组保存简单路径
static int path[PLACES], count = 0, mark[PLACES] = {0}, start = x;
// 在反复调用递归函数时刷新静态变量
if (refresh)
{
for (int i = 0; i < PLACES; i++)
{
path[i] = INT_MAX;
mark[i] = 0;
}
count = 0;
start = x;
}
for (int i = 0; i < PLACES; i++)
{
if (AdjacencyMatrix[x][i] != INT_MAX and mark[i] == 0)
{
// 标记该节点以搜索过
mark[i] = 1;
if (x == i)
continue;
path[c
没有合适的资源?快使用搜索试试~ 我知道了~
C++课设:校园导游系统,基于qt6.zip
共38个文件
png:27个
h:4个
cpp:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 41 浏览量
2023-11-05
10:11:00
上传
评论
收藏 3.56MB ZIP 举报
温馨提示
1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 -------- 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
资源推荐
资源详情
资源评论
收起资源包目录
C++课设:校园导游系统,基于qt6.zip (38个子文件)
project_ok
GuideWindow
GuideWindow.ui 43KB
main.cpp 262B
Headers
ui_GuideWindow.h 37KB
Graph.h 2KB
GuideWindow.h 614B
Paint.h 4KB
Resources
main.png 5KB
searchinfo.png 6KB
multiplaces.png 4KB
swjtu.png 69KB
auth.jpg 86KB
about.png 2KB
img
demo4.png 124KB
main.png 2KB
gui.png 124KB
Progress.png 50KB
Dijkstra.png 13KB
Floyd.png 71KB
demo3.png 129KB
build2.png 76KB
demo2.png 193KB
Qt.png 8KB
demo1.png 194KB
demo5.png 123KB
build1.png 41KB
console.png 76KB
build3.png 11KB
map.png 467KB
writeinfo.png 6KB
map2.png 830KB
findroad.png 6KB
sdu.png 65KB
allroad.png 6KB
map.png 866KB
Graph.cpp 9KB
GuideWindow.cpp 7KB
GuideWindow.pro 717B
GuideWindow.pro.user 19KB
共 38 条
- 1
资源评论
程皮
- 粉丝: 266
- 资源: 2567
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功