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& ...
随机推荐
- 使用Mybatis时报错Cause: java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符
首先保证sql语句在oracle中的编写是正确的,然后在配置文件中插入时就报这样的错误.有可能是因为sql语句后面多了“:”分号,在标签中写分号是错的.如果我写成了 insert into emplo ...
- mysql应该看的blog
一个比较系统的学习mysql的网站:http://www.runoob.com/mysql/mysql-data-types.html
- RabbitMQ C# driver stops receiving messages
http://stackoverflow.com/questions/12499174/rabbitmq-c-sharp-driver-stops-receiving-messages
- 三十八 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)介绍以及安装
elasticsearch(搜索引擎)介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticse ...
- SpringMVC4整合CXF发布WebService
SpringMVC4整合CXF发布WebService版本:SpringMVC 4.1.6,CXF 3.1.0项目管理:apache-maven-3.3.3 pom.xml <project x ...
- 从无到有开发自己的Wordpress博客主题---Wordpress主题的构造
在这篇教程中,主要是对Wordpress的主题的构造进行分析,以方便今后的开发工作. 本来打算就引用一下别人已经有的文档就好了,但还是想从头到尾捋一遍,也方便自己梳理学习. 1.Wordpress主题 ...
- AS3里面的错误代码
ActionScript 3 出现2048安全策略服务,一种原因是因为843端口下发策略文件有问题,另一种原因是Socket端口有问题,可以用telnet来测试. 其它AS3错误代码的意义可以在官网文 ...
- tslib: Selected device is not a touchscreen (must support ABS_X and ABS_Y events)
/************************************************************************************ * tslib: Selec ...
- Android开发的基础知识点
1.Android开发的四大组件: Activity:android应用程序上看到的一页. Service:运行在后台,可以其他组件交互(音乐播放器). BroadcoastReceiver:用来对外 ...
- IQ信号理解
可参考http://wenku.baidu.com/link?url=Y3plyK9lgl96QowljJkWhgVaUGbH11j178DkId_vcl9z1V5cjl9ycTiB4Ym4iaypL ...