Qt地铁智慧换乘系统浅学( 三 )最少路径和最少换乘实现

2023-11-20 22:30

本文主要是介绍Qt地铁智慧换乘系统浅学( 三 )最少路径和最少换乘实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本算法全都基于广度优先

  • 概念
  • 最短路径实现
    • 所用容器
    • 算法思路
  • 最少换乘实现
    • 所需容器
    • 算法思路
  • 成果展示
  • 代码实现
    • 判断是最短路径还是最少换乘
    • 最短路径代码实现
    • 最少换乘代码实现
    • 根据所得List画出线路
  • ui界面的维护(前提条件)
    • 界面
    • 初始化combox控件
    • 建立槽函数

概念

概念这里不过多介绍,很多文章介绍
大体意思是队列思想,每次入队相邻的节点,按照队列以此调用
这里如果想要实现最短路,最少换乘的话,需要用到优先队列
在以上的基础上,对队列进行dist距离的排序,或者trans换乘次数的排序
每次去除最少的,也类似于贪心

最短路径实现

所用容器

typedef std::pair<int,int> PII;  //dist trans
typedef std::pair<QString,QString> nodea; //stationname linename
typedef std::pair<PII,nodea> PLL;  //dist trans ..... ....
QMap<QString,int> dist; //距离表
QMap<QString,int> trans; //换乘
QMap<nodea,bool> final; //是否遍历过
QMap<QString,nodea>pre;  //父节点维护
std::priority_queue<PLL,std::vector<PLL >, std::greater<PLL > > q;  ///优先队列 每次取dist最少的节点

算法思路

这里我们实现了最短路径下最少换乘


1 将换乘表 和 距离表全部设置为INTMAX2 将起点的 换成表 和 距离表 设置为 0
3 将出发站信息存储到优先队列q中 { {00}{startstr , linename} } 注意(出发点可能有多个,出发点有可能属于多个线路)
4 将pre[startstr]赋值为{ "00","00"} 方便还原路径时截止
5 进入while循环 每次取top值 ,为换乘最少的站 ,判断final[top.second]是否为true,如果已经遍历过 那么continue ,否则 final[top.second] = true
6 遍历这个站临近的站点 l , 然后遍历站点i 与 l 所属的线路 k  我们设nodea j ={ l,k}   这时候我们讨论  设now = top.second
(1) 如果dist[j.first] > dist[now .first] + dp[now.first][j.first]  则更新 ,   pre[j.first] = now;如果大于则 更新 ,并且 pre[j.frist] =  now ; 距离表同样也更新 然后入队列(2) 如果dist[j.first] == dist[now .first] + dp[now.first][j.first]我们需要判断换乘最少!如果k==now.second 说明不需要换乘 我们判断trans[j.first] 是否大于 trans[now.first]即可  如果大于则更新如果k!=now.second 说明需要换乘 我们判断trans[j.first] 是否大于 trans[now.first]+1 即可 如果大于则更新7 最后等待while执行完毕 我们获得了 pre[ endstr ]的信息,然后循环调用pre 直到00结束

最少换乘实现

所需容器

QMap<QString,int> dist; //距离表QMap<QString,int> trans; //换乘QMap<nodea,bool> final; //是否遍历过typedef std::pair<int,int> PII;  //换乘 距离typedef std::pair<QString,QString> nodea; //站点名 和 线路名typedef std::pair<PII,nodea> PLL; // 某个站点的换成次数 和 距离 以及父站点和所属线路QMap<QString,nodea>pre;  //还原最少换乘的工具std::priority_queue<PLL,std::vector<PLL >, std::greater<PLL > > q; //优先队列 优先使用换乘最少的站点

算法思路

算法思路1 将换乘表 和 距离表全部设置为INTMAX2 将起点的 换成表 和 距离表 设置为 0
3 将出发站信息存储到优先队列q中 { {00}{startstr , linename} } 注意(出发点可能有多个,出发点有可能属于多个线路)
4 将pre[startstr]赋值为{ "00","00"} 方便还原路径时截止
5 进入while循环 每次取top值 ,为换乘最少的站 ,判断final[top.second]是否为true,如果已经遍历过 那么continue ,否则 final[top.second] = true
6 遍历这个站临近的站点 l , 然后遍历站点i 与 l 所属的线路 k  我们设nodea j ={ l,k}      这时候我们讨论  设now = top.second
(1)如果站点i的线路 与 k 一样 那么换乘次数不会增加 这个时候我们判断 trans[j.first] 是否大于 trans[i] 如果大于则 更新 ,并且 pre[j.frist] =  now ; 距离表同样也更新 然后入队列如果 换乘次数相等的话  我们判断距离表 , 如果dist[j.first] > dist[now .first] + dp[now.first][j.first]则更新 pre[j.first] = now;(2) 如果站点i的线路 与 k不一样 那么我们的换乘就有可能+1这个时候我们判断trans[j.first]  是否大于 trans[now.frist] +1 如果大于 则更新 并且 pre[j.first] = { now.first,k} ; 距离表同样也更新 这里如果pre[j.frist] = now就会出错,问题在后续讲解如果 transp[j.irst]  == trans[now.first] +1  我们就判断距离表 如果dist[j.first] > dist[now .first] + dp[now.first][j.first] 则更新,pre[j.first] = {now.first,k} 
7 最后等待while执行完毕 我们获得了 pre[ endstr ]的信息,然后循环调用pre 直到00结束

成果展示

在这里插入图片描述

代码实现

判断是最短路径还是最少换乘

void ChooseShortorLessPath(QGraphicsView *graphicsView,QString startstr,QString endstr,QRadioButton *sht,QRadioButton *less,QTextBrowser *textBrowser){qDebug()<<"选择开始\n";if(startstr.size()==0||endstr.size()==0){QMessageBox MyBox(QMessageBox::Warning,"警告","请选择站点",QMessageBox::Yes|QMessageBox::No);MyBox.exec();return;}QMap<QString, node>::iterator it = Station.find(startstr);if (it == Station.end()) {QMessageBox MyBox(QMessageBox::Warning,"警告","输入站点有误",QMessageBox::Yes|QMessageBox::No);MyBox.exec();return;}it =Station.find(endstr);if (it == Station.end()) {QMessageBox MyBox(QMessageBox::Warning,"警告","输入站点有误",QMessageBox::Yes|QMessageBox::No);MyBox.exec();return;}if(startstr == endstr){QMessageBox MyBox(QMessageBox::Warning,"警告","请不要输入相同站点 ",QMessageBox::Yes|QMessageBox::No);MyBox.exec();return;}if(sht->isChecked()) getshortTimePath(graphicsView,startstr,endstr,textBrowser);if(less->isChecked()) getlessTransPath(graphicsView,startstr,endstr,textBrowser);}

最短路径代码实现

typedef std::pair<int,int> PII;
typedef std::pair<QString,QString> nodea;
typedef std::pair<PII,nodea> PLL;void getshortTimePath(QGraphicsView *graphicsView,const QString startstr,const QString endstr,QTextBrowser *textBrowser){qDebug()<<"选择成功\n";const int IntMax = INT32_MAX;QMap<QString,int> dist; //距离表QMap<QString,int> trans; //换乘QMap<nodea,bool> final; //是否遍历过for(auto i:Station.keys()){dist[i] = IntMax;trans[i] = IntMax;}QMap<QString,nodea>pre;pre[startstr] = nodea{"00","00"};std::priority_queue<PLL,std::vector<PLL >, std::greater<PLL > > q;for(auto i:Station_Line[startstr]){q.push(PLL{{0,0},nodea{startstr,i}});}dist[startstr] = 0;trans[startstr] = 0;qDebug()<<"初始化完成\n";while(q.size()){PLL f= q.top();q.pop();nodea now = f.second;if(final[now]) continue;final[now] = true;for(auto i:edge[now.first]){ //相邻的站点for(auto j:mp[now.first][i]){ //和相邻站点共有的线路qDebug()<<now.first<<"-----"<<i<<"----"<<j<<" \n ";nodea newnodea = nodea{i,j};if(final[newnodea]) continue;if(dist[i] > dist[i]+dp[now.first][i]){dist[i] = dist[i]+dp[now.first][i];qDebug()<<now.first<<"---"<<i<<"......."<<dist[i]<<" \n ";if(j == now.second){ trans[i] = trans[now.first];  pre[i] = now; }else {trans[i] = trans[now.first] + 1; pre[i] = nodea{now.first,j}; }qDebug()<<i<<"---"<<pre[i]<<" \n ";q.push(PLL{PII{dist[i],trans[i]},newnodea});}else if(dist[i] == dist[now.first]+dp[now.first][i]){if(j == now.second){if(trans[i] > trans[now.first]){trans[i] = trans[now.first];pre[i] = now;q.push(PLL{PII{dist[i],trans[i]},newnodea});}}if(j != now.second){if(trans[i] > trans[now.first] + 1 ){trans[i] = trans[now.first] + 1;pre[i] = nodea{now.first,j};q.push(PLL{PII{dist[i],trans[i]},newnodea});}}}}}}qDebug()<<"最短路执行完毕\n";QList<nodea>s;int t=0;nodea u = pre[endstr];while(u.second!="00"){s.append(u);qDebug()<<"---"<<u.first<<" \n ";u=pre[u.first];t++;if(t>40) return;}qDebug()<<"还原成功\n";drawpath(graphicsView,startstr,endstr,s,textBrowser);
}

最少换乘代码实现

void getlessTransPath(QGraphicsView *graphicsView ,const QString startstr,const QString endstr,QTextBrowser *textBrowser){qDebug()<<"选择成功\n";const int IntMax = INT32_MAX;QMap<QString,int> dist; //距离表QMap<QString,int> trans; //换乘QMap<nodea,bool> final; //是否遍历过for(auto i:Station.keys()){trans[i] = IntMax;dist[i] = IntMax;}QMap<QString,nodea>pre;pre[startstr] = nodea{"00","00"};std::priority_queue<PLL,std::vector<PLL >, std::greater<PLL > > q;for(auto i:Station_Line[startstr]){q.push(PLL{{0,0},nodea{startstr,i}});}dist[startstr] = 0;trans[startstr] = 0;qDebug()<<"初始化完成\n";while(q.size()){PLL f= q.top();q.pop();nodea now = f.second;if(final[now]) continue;final[now] = true;for(auto i:edge[now.first]){ //相邻的站点for(auto j:mp[now.first][i]){ //和相邻站点共有的线路qDebug()<<now.first<<"-----"<<i<<"----"<<j<<" \n ";nodea newnodea = nodea{i,j};if(final[newnodea]) continue;if(j == now.second){if(trans[i] > trans[now.first]){dist[i] = dist[now.first] + dp[now.first][i];trans[i] = trans[now.first];pre[i] = now;q.push(PLL{PII{trans[i],dist[i]},newnodea});qDebug()<<now.first<<"---"<<i<<"......."<<dist[i]<<" \n ";}else if(trans[i] == trans[now.first]){if(dist[i] > dist[now.first]+dp[now.first][i]){dist[i] = dist[now.first]+dp[now.first][i];qDebug()<<now.first<<"---"<<i<<"......."<<dist[i]<<" \n ";pre[i] = now;q.push(PLL{PII{trans[i],dist[i]},newnodea});}}}if(j != now.second){if(trans[i] > trans[now.first] + 1 ){trans[i] = trans[now.first] + 1;dist[i] = dist[now.first] + dp[now.first][i];pre[i] = nodea{now.first,j};q.push(PLL{PII{trans[i],dist[i]},newnodea});qDebug()<<now.first<<"---"<<i<<"......."<<now<<" \n ";}else if(trans[i] == trans[now.first] + 1 ){if(dist[i] > dist[now.first]+dp[now.first][i]){dist[i] = dist[now.first]+dp[now.first][i];qDebug()<<now.first<<"---"<<i<<"......."<<dist[i]<<" \n ";pre[i] = nodea{now.first,j};q.push(PLL{PII{trans[i],dist[i]},newnodea});}}}}}}qDebug()<<"最短路执行完毕\n";QList<nodea>s;int t=0;nodea u = pre[endstr];while(u.second!="00"){s.append(u);qDebug()<<"---"<<u.first<<" \n ";u=pre[u.first];t++;if(t>40) return;}qDebug()<<"还原成功\n";drawpath(graphicsView,startstr,endstr,s,textBrowser);
}

根据所得List画出线路

void getlessTransPath(QGraphicsView *graphicsView ,const QString startstr,const QString endstr,QTextBrowser *textBrowser){qDebug()<<"选择成功\n";const int IntMax = INT32_MAX;QMap<QString,int> dist; //距离表QMap<QString,int> trans; //换乘QMap<nodea,bool> final; //是否遍历过for(auto i:Station.keys()){trans[i] = IntMax;dist[i] = IntMax;}QMap<QString,nodea>pre;pre[startstr] = nodea{"00","00"};std::priority_queue<PLL,std::vector<PLL >, std::greater<PLL > > q;for(auto i:Station_Line[startstr]){q.push(PLL{{0,0},nodea{startstr,i}});}dist[startstr] = 0;trans[startstr] = 0;qDebug()<<"初始化完成\n";while(q.size()){PLL f= q.top();q.pop();nodea now = f.second;if(final[now]) continue;final[now] = true;for(auto i:edge[now.first]){ //相邻的站点for(auto j:mp[now.first][i]){ //和相邻站点共有的线路qDebug()<<now.first<<"-----"<<i<<"----"<<j<<" \n ";nodea newnodea = nodea{i,j};if(final[newnodea]) continue;if(j == now.second){if(trans[i] > trans[now.first]){dist[i] = dist[now.first] + dp[now.first][i];trans[i] = trans[now.first];pre[i] = now;q.push(PLL{PII{trans[i],dist[i]},newnodea});qDebug()<<now.first<<"---"<<i<<"......."<<dist[i]<<" \n ";}else if(trans[i] == trans[now.first]){if(dist[i] > dist[now.first]+dp[now.first][i]){dist[i] = dist[now.first]+dp[now.first][i];qDebug()<<now.first<<"---"<<i<<"......."<<dist[i]<<" \n ";pre[i] = now;q.push(PLL{PII{trans[i],dist[i]},newnodea});}}}if(j != now.second){if(trans[i] > trans[now.first] + 1 ){trans[i] = trans[now.first] + 1;dist[i] = dist[now.first] + dp[now.first][i];pre[i] = nodea{now.first,j};q.push(PLL{PII{trans[i],dist[i]},newnodea});qDebug()<<now.first<<"---"<<i<<"......."<<now<<" \n ";}else if(trans[i] == trans[now.first] + 1 ){if(dist[i] > dist[now.first]+dp[now.first][i]){dist[i] = dist[now.first]+dp[now.first][i];qDebug()<<now.first<<"---"<<i<<"......."<<dist[i]<<" \n ";pre[i] = nodea{now.first,j};q.push(PLL{PII{trans[i],dist[i]},newnodea});}}}}}}qDebug()<<"最短路执行完毕\n";QList<nodea>s;int t=0;nodea u = pre[endstr];while(u.second!="00"){s.append(u);qDebug()<<"---"<<u.first<<" \n ";u=pre[u.first];t++;if(t>40) return;}qDebug()<<"还原成功\n";drawpath(graphicsView,startstr,endstr,s,textBrowser);
}

ui界面的维护(前提条件)

界面

在这里插入图片描述

初始化combox控件

void initcombox(QComboBox *combox1,QComboBox *combox2){QStringList sta_name_list;for(auto &i:Station.keys())sta_name_list.append(i);std::sort(sta_name_list.begin(),sta_name_list.end(),[](const QString &s1, const QString &s2){return (s1.localeAwareCompare(s2) < 0);});combox1->addItems(sta_name_list);combox2->addItems(sta_name_list);QLineEdit *line1 = new QLineEdit;combox1->setLineEdit(line1);combox1->lineEdit()->clear();QLineEdit *line2 = new QLineEdit;combox2->setLineEdit(line2);combox2->lineEdit()->clear();
}

建立槽函数

connect(ui->pushButton,&QPushButton::clicked,this,[=]{ChooseShortorLessPath(ui->graphicsView,ui->comboBox->lineEdit()->text(),ui->comboBox_2->lineEdit()->text(),ui->radioButton,ui->radioButton_2,ui->textBrowser);});

这篇关于Qt地铁智慧换乘系统浅学( 三 )最少路径和最少换乘实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/397911

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg