PAT甲级——A1131 Subway Map【30】
In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with your computer skills! Given the starting position of your user, your task is to find the quickest way to his/her destination.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 100), the number of subway lines. Then N lines follow, with the i-th (,) line describes the i-th subway line in the format:
M S[1] S[2] ... S[M]
where M (≤ 100) is the number of stops, and S[i]'s (,) are the indices of the stations (the indices are 4-digit numbers from 0000 to 9999) along the line. It is guaranteed that the stations are given in the correct order -- that is, the train travels between S[i] and S[i+1] (,) without any stop.
Note: It is possible to have loops, but not self-loop (no train starts from S and stops at S without passing through another station). Each station interval belongs to a unique subway line. Although the lines may cross each other at some stations (so called "transfer stations"), no station can be the conjunction of more than 5 lines.
After the description of the subway, another positive integer K (≤ 10) is given. Then K lines follow, each gives a query from your user: the two indices as the starting station and the destination, respectively.
The following figure shows the sample map.
Note: It is guaranteed that all the stations are reachable, and all the queries consist of legal station numbers.
Output Specification:
For each query, first print in a line the minimum number of stops. Then you are supposed to show the optimal path in a friendly format as the following:
Take Line#X1 from S1 to S2.
Take Line#X2 from S2 to S3.
......
where X
i's are the line numbers and S
i's are the station indices. Note: Besides the starting and ending stations, only the transfer stations shall be printed.
If the quickest path is not unique, output the one with the minimum number of transfers, which is guaranteed to be unique.
Sample Input:
4
7 1001 3212 1003 1204 1005 1306 7797
9 9988 2333 1204 2006 2005 2004 2003 2302 2001
13 3011 3812 3013 3001 1306 3003 2333 3066 3212 3008 2302 3010 3011
4 6666 8432 4011 1306
3
3011 3013
6666 2001
2004 3001
Sample Output:
2
Take Line#3 from 3011 to 3013.
10
Take Line#4 from 6666 to 1306.
Take Line#3 from 1306 to 2302.
Take Line#2 from 2302 to 2001.
6
Take Line#2 from 2004 to 1204.
Take Line#1 from 1204 to 1306.
Take Line#3 from 1306 to 3001.
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
vector<int> route[];
int visit[], minCnt, minTra, n, m, k, start, end1;
unordered_map<int, int>line;
vector<int>path, tempPath;
int transferCnt(vector<int>a)
{
int cnt = -, preLine = ;
for (int i = ; i < a.size(); ++i)
{
if (line[a[i - ] * + a[i]] != preLine)
{
cnt++;//换乘了
preLine = line[a[i - ] * + a[i]];
}
}
return cnt;
}
void DFS(int node, int cnt)
{
if (node == end1 && (cnt < minCnt || (cnt == minCnt && transferCnt(tempPath) < minTra)))
{
minCnt = cnt;
minTra = transferCnt(tempPath);
path = tempPath;
}
if (node == end1)return;
for (int i = ; i < route[node].size(); ++i)
{
if (visit[route[node][i]] == )//未遍历过
{
visit[route[node][i]] = ;
tempPath.push_back(route[node][i]);//保存可行路径
DFS(route[node][i], cnt + );//经过一站
visit[route[node][i]] = ;
tempPath.pop_back();//使用回溯法
}
}
}
int main()
{
cin >> n;
for (int i = ; i <= n; ++i)
{
cin >> m >> start;
for (int j = ; j < m; ++j)
{
cin >> end1;
route[start].push_back(end1);
route[end1].push_back(start);
line[start * + end1] = line[end1 * + start] = i;//用pot1*10000+pot来表示这两个站是可行的
start = end1;
}
}
cin >> k;
while (k--)
{
cin >> start >> end1;
minCnt = minTra = INT32_MAX-;
tempPath.clear();
tempPath.push_back(start);
visit[start] = ;
DFS(start, );
visit[start] = ;
cout << minCnt << endl;
int preLine = , preTra = start;
for (int i = ; i < path.size(); ++i)
{
if (line[path[i - ] * + path[i]] != preLine)//换乘了
{
if (preLine != )
printf("Take Line#%d from %04d to %04d.\n", preLine, preTra, path[i - ]);
preLine = line[path[i - ] * + path[i]];
preTra = path[i - ];
}
}
printf("Take Line#%d from %04d to %04d.\n", preLine, preTra, end1);
}
return ;
}
PAT甲级——A1131 Subway Map【30】的更多相关文章
- 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 ...
- A1131. 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【dfs】【输出方案】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805347523346432 题意: 告诉你一个地铁线路图,站点都是 ...
- PAT甲级1111. Online Map
PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...
- PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)
本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078 1111 Online Map (30 分) ...
- PAT 1131. Subway Map (30)
最短路. 记录一下到某个点,最后是哪辆车乘到的最短距离.换乘次数以及从哪个位置推过来的,可以开$map$记录一下. #include<map> #include<set> #i ...
- PAT甲级——A1111 Online Map【30】
Input our current position and a destination, an online map can recommend several paths. Now your jo ...
- PAT A1131 Subway Map
dfs,选择最优路径并输出~ 这道题难度非常炸裂,要求完完整整自己推一遍,DFS才算过关!思路:一遍dfs,过程中要维护两个变量,minCnt 中途停靠最少的站.minTransfer需要换成的最少次 ...
随机推荐
- Jenkins 搭建 .NET FrameWork 持续集成环境
本文不赘述如何安装 Jenkins,如有需要请看之前文章,这里我们主要搭建 .Net 环境.本文是在 Windows 环境下安装的 Jenkins 进行操作 一.安装所需环境 这里我们 ...
- CRI 与 ShimV2:一种 Kubernetes 集成容器运行时的新思路
摘要: 关于 Kubernetes 接口化设计.CRI.容器运行时.shimv2.RuntimeClass 等关键技术特性的设计与实现. Kubernetes 项目目前的重点发展方向,是为开发 ...
- css属性大全(基础篇)
什么是CSS? CSS全称为Cascading Style Sheets,中文翻译为“层叠样式表”,简称CSS样式表,所以称之为层叠样式表(Cascading Stylesheet)简称CSS.在 ...
- thinkphp模块设计
3.2发布版本自带了一个应用目录结构,并且带了一个默认的应用入口文件,方便部署和测试,默认的应用目录是Application(实际部署过程中可以随意设置). 通常情况下3.2无需使用多应用模式,因为大 ...
- 38 ubuntu/windows双系统安装
0 引言 (1)针对bios 和 uefi引导,安装方式略有不同. (2)针对nvidia显卡,在安装时需要特殊设置. 1 EasyBCD安装方式介绍-适用于bios引导方式 参考百度经验贴安装即可, ...
- pytorch处理模型过拟合
演示代码如下 import torch from torch.autograd import Variable import torch.nn.functional as F import matpl ...
- NX二次开发-创建经典工具栏UF_UI_create_toolbar
NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...
- GO语言range的用法 (2013-08-09 14:08:26)
range是go语言系统定义的一个函数. 函数的含义是在一个数组中遍历每一个值,返回该值的下标值和此处的实际值. 假如说a[0]=10,则遍历到a[0]的时候返回值为0,10两个值. 下面是一个例子: ...
- IDEA web 开发环境搭建
最近由eclipse 换 IDEA ,记录下开发环境的搭建过程. 1 配置idea vim 既可以使用IDEA方便的代码提示和调试功能,又可以方便使用vim编辑文件,安装完成后显示为vim Emula ...
- 剑指offer——26树的子结构
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 题解: 注意,所谓的子结构,是树的形状和值相同,并非判断B是不是A的一部分[如果是这样,那就是 ...