当前位置:网站首页>最少换乘次数
最少换乘次数
2022-06-12 08:54:00 【马踏飞燕&lin_li】
最少换乘次数
问题描述:
设某城市有n个车站,并有m条公交线路连接这些车站。设这些公交车都是单向的,这n个车站被顺序编号为0~n-1。编号程序,输入该城市的公交线路数,车站个数,以及各公交线路上的各站编号。
实现要求:
求得从站0出发乘公交车至站n一1的最少换车次数。
思路:
BFS:我们把同一条线路上的所有车站之间全部连一条边,这样就可以直接利用bfs求得最短距离,因为bfs只要到达终点就一定是最短的。
单源最短路径算法:经典的Dijstra算法求最短路径
#include<iostream>
#include<sstream>
#include<cstring>
#include<algorithm>
#include<queue>
//第一行有两个数字M和N,表示开通了M条单程巴士线路,总共有N个车站。
//从第二行到第M+1行依次给出了第1条到第M条巴士线路的信息,其中第i+1行给出的是第i条巴士线路的信息,
// 从左至右按运行顺序依次给出了该线路上的所有站号,相邻两个站号之间用一个空格隔开。
using namespace std;
const int N=510,inf=0x3f3f3f3f;
int n,m; //n个车站
bool g[N][N];
int dis[N]; //用于存放最短路径
void bfs()
{
memset(dis,0x3f,sizeof dis);
queue<int> Q;
Q.push(1);
dis[1]=0;
while(Q.size())
{
int u=Q.front();
Q.pop();
for(int i=1;i<=n;i++)
if(g[u][i] && dis[i]>dis[u]+1)
{
dis[i]=dis[u]+1;
Q.push(i);
}
}
}
void dijstra(int start)
{
//dijstra算法开始
//visited数组初始化
int visited[n+1];
memset(visited,0,sizeof(visited));
visited[start]=1;
//dist数组初始化
// int dist[n+1];
for(int i=1;i<=n;i++){
if(g[start][i]) dis[i] = 1;
else dis[i] = inf;
}
for(int i=2;i<=n;i++){
int min = 99999, u;
//寻找当前状态下路径最小的边
for(int i=1;i<=n;i++){
if(!visited[i] && dis[i]<min){
min = dis[i];
u = i;
}
}
//将u加入到s集合,visited[u]=1;
visited[u]=1;
//更新最短路径
for(int i=1;i<=n;i++){
if(!visited[i] && g[u][i] && dis[u]+1<dis[i]){
dis[i] = dis[u]+1;
}
}
}
}
int main()
{
printf("=====================开始建图====================\n");
printf("请输入公交线路数 and 公交车站数:\n");
cin>>m>>n;
int to = m;
string line;
getline(cin,line);
while(m--)
{
printf("请输入第%d条线路所经过的车站,用空格隔开:\n",to-m);
getline(cin,line);
stringstream ssin(line);
int p[N],cnt=0;
while(ssin>>p[cnt]) cnt++;
for(int i=0;i<cnt;i++)
for(int j=i+1;j<cnt;j++)
g[p[i]][p[j]]=true;
}
// bfs();
dijstra(1);
// 因为同一条线路内的各站台之间的边权均为1,所以跑最短路时,同一线路内至多经过两站。
// 又因为边权均为1,因此最短路可以用bfs来跑。
// 这样我们得到的结果是坐过的公交的最少数量,而换乘次数还要再减1。
printf("=====================查询开始====================\n");
while(true){
printf("请输入需要到达哪个目的地,-1表示退出:\n");
int tar; cin>>tar;
if(tar==-1) break;
if(tar>n){
printf("输入不合法,请重新输入\n");
continue;
}
if(dis[tar]==inf) puts("NO");
else cout<<"恭喜!有车可以到达该目的地,最少换乘次数为:"<<max(dis[tar]-1,0)<<endl;
}
printf("=====================欢迎使用该系统====================\n");
return 0;
}
//测试样例
//3 7
//6 7
//4 7 3 6
//2 1 3 5
//2

边栏推荐
- 分库分表会带来读扩散问题?怎么解决?
- JVM learning notes: three local method interfaces and execution engines
- Dynamically create and submit forms
- IP, DNS, domain name, URL, hosts
- Configuration and principle of MSTP
- 第六章-包含多个段的程序
- Adjust SVG width and height
- Knowledge points of 2022 system integration project management engineer examination: project cost management
- 处理异常数据
- 第三章 寄存器 (内存访问)
猜你喜欢

【进阶指针一】字符数组&数组指针&指针数组
![[essence] explain in detail the memory management mechanism in QT](/img/7d/0d83158c6b0574dd3b3547b47af67e.jpg)
[essence] explain in detail the memory management mechanism in QT

Priority issues

Build personal blog and web.

Oracle installation details (verification)

《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测
![[advanced pointer 2] array parameter transfer & pointer parameter transfer & function pointer & function pointer array & callback function](/img/90/447d601a8c338cdd5a6674a2dc59ae.png)
[advanced pointer 2] array parameter transfer & pointer parameter transfer & function pointer & function pointer array & callback function

Building a cluster: and replacing with error

Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low

Error: what if the folder cannot be deleted when it is opened in another program
随机推荐
Difference between binary GB and gib
(P14) use of the override keyword
【字符集九】gbk拷贝到Unicode会乱码?
ip、DNS、域名、URL、hosts
JS to refresh the page after loading
Popular understanding of time domain sampling and frequency domain continuation
Background color translucent
Code generation tool Autocode for XML Publishing
Specify 404 and 500 error reporting pages.
Shell基本语法--算数运算
torch. logical_ And() method
Loading circling effect during loading
《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测
了结非对称密钥
[open source project] easycmd command graphical software
(p19-p20) delegate constructor (proxy constructor) and inheritance constructor (using)
[new planning]
Dynamic segment tree leetcode six hundred and ninety-nine
Dynamically create and submit forms
《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现