1131 Subway Map
题意:给出起点和终点,计算求出最短路径(最短路径即所经过的站点最少的),若最短路径不唯一,则选择其中换乘次数最少的一条线路。
思路:本题虽然也是求最短路径,但是此路径是不带权值的,路径长度即所经过的边数,故可以用DFS来求解,而不是用一般的Dijkstra之类的。相信若只是求最短路径,大多数人都会做,就是从起点start开始深度遍历,遍历到终点end时,与全局变量进行比较、更新。本题的关键是,更新最优路径时需要比较“换乘次数”,如何求解它呢?我是这么思考的——首先,考虑用一个二维数组int mp[maxn][maxn]来存储站点与线路的关系,如mp[6666][8432]=4,表示6666->8432是4号线,但考虑到站点编号的范围最大达到9999,也就是数组得开10000*10000,这显然是无法承受的,故选用unordered_map,令unordered_map<int,unordered_map<int,int>> mp,操作和普通的数组一样。(我发现这个unordered_map真的是非常好用,很多题目都可以用,这里不细说,有兴趣的查看文档进行学习)。那么,怎么算是“换乘”呢?假设前一个站是pre,当前站是curr,下一个站是next,若mp[pre][curr]≠mp[curr][next],说明需要一次换乘,顺序遍历路径path的所有站点,即可求出换乘次数。最后,本题的输出也是比较麻烦,但思路和求换乘次数的方法是一样的。具体请看代码,关键处都有注释。
ps.代码中尽量不要出现中文注释,因为在中文输入法下,若不小心在某一行开头输入了一个空格(难以发现),这会导致编译出错,产生“error: stray '\241' in program”的错误信息。
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <unordered_map>
using namespace std;
;
const int Inf=0x7fffffff;
unordered_map<int,unordered_map<int,int>> mp;//存储两站点间的地铁线,如mp[6666][8432]=4,表示6666->8432是4号线
bool vis[maxn];//在DFS中标记结点是否已经被访问过
vector<int> graph[maxn];//邻接表存储地铁线路图
int k,n,m,s,e,minDistance,minTransfer;//地铁线条数,每条地铁的站点数,查询次数,查询的起点和终点,最短距离,最少换乘数
vector<int> path,tmpPath;//path存放最优路径,tmpPath存放临时路径
int getTransferCnt(vector<int>& path)
{
;
];
;i+<path.size();i++){//注意,这里的判定是i+1<path.size()
];
if(mp[pre][curr]!=mp[curr][next]) changeCnt++;
pre=curr;//记得更新
}
return changeCnt;
}
void dfs(int s)
{
vis[s]=true;
tmpPath.push_back(s);
if(s==e){
int tmpTransfer=getTransferCnt(tmpPath);
< minDistance){
minDistance=tmpPath.size()-;
minTransfer=tmpTransfer;
path=tmpPath;
} == minDistance && tmpTransfer < minTransfer){
minTransfer=tmpTransfer;
path=tmpPath;
}
return;
}
for(auto next:graph[s]){
if(vis[next] == true) continue;
dfs(next);
tmpPath.pop_back();
vis[next]=false;
}
}
void printPath(vector<int>& path)
{
//换乘次数为0时,只需要输出起点和终点,单独输出。这里minTransfer是全局变量,在调用该函数前已经确定
){
],b=path[path.size()-];//also b=path.back();
printf(]][path[]],a,b);//注意,这里线路不能是mp[a][b],因为站点a、b不一定是相邻的!
return;
}
];//表示当前这条线路的起始站
],curr,next;
;i+<path.size();i++){
curr=path[i],next=path[i+];
if(mp[pre][curr]!=mp[curr][next]) {
printf("Take Line#%d from %04d to %04d.\n",mp[pre][curr],start,curr);
start=curr;//出现换乘,记得更新起始站
}
pre=curr;
}
//输出最后一次换乘至终点的线路
printf(]][path[path.size()-]],start,path[path.size()-]);
}
int main()
{
//freopen("pat.txt","r",stdin);
scanf("%d",&k);
;i<=k;i++){
int pre,curr;
scanf("%d%d",&n,&pre);
;j<n;j++){
scanf("%d",&curr);
graph[pre].push_back(curr);
graph[curr].push_back(pre);
mp[pre][curr]=mp[curr][pre]=i;
pre=curr;
}
}
scanf("%d",&m);
while(m--){
scanf("%d%d",&s,&e);
//每次查询前千万记得初始化
memset(vis,false,sizeof(vis));
path.clear();
tmpPath.clear();
minDistance=Inf,minTransfer=Inf;
dfs(s);
printf("%d\n",minDistance);
printPath(path);
}
;
}
1131 Subway Map的更多相关文章
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级——1131 Subway Map (30 分)
可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 ...
- 1131 Subway Map DFS解法 BFS回溯!
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- 1131 Subway Map(30 分)
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- PAT 1131 Subway Map
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- PAT甲级1131 Subway Map【dfs】【输出方案】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805347523346432 题意: 告诉你一个地铁线路图,站点都是 ...
- PAT 1131. Subway Map (30)
最短路. 记录一下到某个点,最后是哪辆车乘到的最短距离.换乘次数以及从哪个位置推过来的,可以开$map$记录一下. #include<map> #include<set> #i ...
- PAT_A1131#Subway Map
Source: PAT A1131 Subway Map (30 分) Description: In the big cities, the subway systems always look s ...
- 1131(★、※)Subway Map
思路:DFS遍历 #include <iostream> #include <map> #include <vector> #include <cstdio& ...
随机推荐
- LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- 绘制三角形(sass)
绘制三角形 /// draw triangle/// @param {type} $type [''] - triangleUp triangleDown triangleLeft triangleR ...
- Eclipse中Preference打开后找不到Server项解决方案。
该解决方案是假设你已经安装好了JDK,tomcat,eclipse,突然在Eclipse的配置时找不到选择菜单栏中的window——preferences-server——runtime enviro ...
- Tomcat和Servlet
Tomcat Tomcat是什么,Tomcat是目前市场上主流Web服务器之一,是用Java语言开发的项目.Tomcat支持Servlet和JSP的规范,它由一组嵌套的层次和组件组成.结构如下图 所有 ...
- git远程分支回退
[本地代码回退] git reset --hard commit-id :回滚到commit-id,讲commit-id之后提交的commit都去除 git reset --hard HEAD~3:将 ...
- Qt5学习笔记——QRadioButton与QbuttonGroup
[我是小标题:使用QToolButton实现radio button功能.] QRadioButton是什么? 下图是Windows系统中典型的radio button显示效果. QRadio ...
- Could not find a valid gem 'rails' (>= 0), here is why
很长一段时间之前 Ruby Rails入门--windows下搭建Ruby Rails Web开发环境 ,由于后来将Ruby的安装文件从 C 盘移动到了 D 盘,也修改了 Path 环境变量,ruby ...
- 数据结构之最小生成树Prim算法
普里姆算法介绍 普里姆(Prim)算法,是用来求加权连通图的最小生成树算法 基本思想:对于图G而言,V是所有顶点的集合:现在,设置两个新的集合U和T,其中U用于存放G的最小生成树中的顶点,T存放G的最 ...
- 深入了解zookeeper(三)
一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过ZooKeeper对分布式系统进行Master选举,来解决分布式系统的单点故障,如图所示. 那么我们继续分析一下 ...
- 如何加快MyEclipse的启动速度
学习java开发的朋友对Myeclipse应该不陌生,MyEclipse企业级工作平台(MyEclipseEnterprise Workbench ,简称MyEclipse)是对EclipseIDE的 ...