PAT_A1131#Subway Map
Source:
Description:
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
Xi's are the line numbers andSi'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.
Keys:
- 深度优先搜索
Attention:
- 本来今天没啥精神敲代码,寻思把这道遗留好久的题拿出来随便做做,没想到做出来了。。。
Code:
/*
Date: 2019-08-12 14:26:22
Problem:PAT_A1131#Subway Map
AC:28:03 题目大意:
地铁线路数N<=100
站数M<=100,s1,s2,...,sm(4位)
注:各线路仅在中转站交汇(任一线路不超过5个交汇点),
且各段两个站点之间的路段仅属于某一条线路(不存在两条线路经过同一个路段)
查询数K<=10
始发站,终点站 输出:
给出经过站数
给出最短线路,若不唯一,给出中转次数最少的线路 基本思路:
仿照Dijskra+DFS的解法
深度优先搜索,记录各个路径,并统计中转个数,比较最优路径
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e4;
struct node
{
int line,next;
};
int n,m,v1,v2,length,trans,vis[M];
vector<node> subway[M],path,optPath; void DFS(int v, int line, int transfer)
{
if(vis[v]==)
return;
if(v == v2)
{
path.push_back(node{line,v2});
if(path.size() < length)
{
length = path.size();
trans = transfer;
optPath = path;
}
else if(path.size()==length && transfer<trans)
{
trans = transfer;
optPath = path;
}
path.pop_back();
return;
}
vis[v]=;
path.push_back(node{line,v});
for(int i=; i<subway[v].size(); i++)
{
if(line != subway[v][i].line)
{
if(v == v1)
{
path.pop_back();
path.push_back(node{subway[v][i].line,v});
}
DFS(subway[v][i].next,subway[v][i].line,transfer+);
}
else
DFS(subway[v][i].next,line,transfer);
}
path.pop_back();
vis[v]=;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
for(int i=; i<=n; i++)
{
scanf("%d%d", &m,&v1);
for(int j=; j<m; j++)
{
scanf("%d", &v2);
subway[v1].push_back(node{i,v2});
subway[v2].push_back(node{i,v1});
v1=v2;
}
}
scanf("%d", &m);
while(m--)
{
length=M;
fill(vis,vis+M,);
scanf("%d%d", &v1,&v2);
DFS(v1,,);
printf("%d\n", length-);
for(int i=; i<length; i++)
{
if(i!= && optPath[i-].line!=optPath[i].line)
printf("%04d.\n", optPath[i-].next);
if(i== || optPath[i-].line!=optPath[i].line)
printf("Take Line#%d from %04d to ", optPath[i].line,optPath[i==?i:i-].next);
}
printf("%04d.\n", optPath[length-].next);
} return ;
}
PAT_A1131#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 ...
- A1131. Subway Map (30)
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- 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甲级——A1131 Subway Map【30】
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- 1131(★、※)Subway Map
思路:DFS遍历 #include <iostream> #include <map> #include <vector> #include <cstdio& ...
- PAT甲级1131 Subway Map【dfs】【输出方案】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805347523346432 题意: 告诉你一个地铁线路图,站点都是 ...
随机推荐
- Windows编写driver
1. 编译 Pspeek.cpp #include <ntddk.h> #define DANIEL_LIST_PROCESS 0x8001 PDRIVER_OBJECT daniel_D ...
- websocket 服务搭建
链接过程 前端 1.CREATED WEBSOCKE 2.ONOPEN 3.ONMESSAGE 服务端 1.收到request 2.给客户端发送消息,生成id //msg { type: " ...
- UVA 11355 Cool Points( 极角计算 )
We have a circle of radius R and several line segments situated within the circumference of this cir ...
- 修改el-input
无论是input框,还是select, 都是对 el-input 进行修改: .houseRegisterCompany .el-input{ width: 130px; }
- css中的文本字间距离、行距、overflow
css字间距.div css字符间距样式实例1.text-indent设置抬头距离css缩进 div设置css样式text-indent : 20px; 缩进了20px 2.letter-spacin ...
- db2 连接数据库与断开数据库
连接数据库: connect to db_name user db_user using db_pass 断开连接: connect resetdisconnect current quit是退出交 ...
- Java工作流引擎 Activiti springmvc 后台框架源码 SSM 流程审批
工作流模块----------------------------------------------------------------------------------------------- ...
- vue侦听属性和计算属性
监听movies,实现点击添加显示到li标签里面.页面效果如下: <template> <div> <div class="moive"> &l ...
- canvas---从基础到实战
canvas是H5新增的一个元素,可以用来描绘各种你想描绘的东西. canvas本身没有绘制能力,你可以把它当做一个容器,需要我们用脚本,也就是js来给他灌满水. 兼容性 1. IE9版本以上.Fir ...
- 如何从零搭建一个webpack+react+redux+react-redux的开发环境一入门
阅读本文章的时候,你要有一定的基础知识储备,简单的es6知识,模块化思想知识,js基础知识,node基础知识,react等 首先执行npm init,此时我的文件叫case; 下面安装一些需要的npm ...

