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
X
i's are the line numbers andS
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.
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 题意: 告诉你一个地铁线路图,站点都是 ...
随机推荐
- JS中基本数据类型和引用类型最根本的区别
栈内存和堆内存:https://segmentfault.com/a/1190000015118062 https://segmentfault.com/a/1190000016389376 变量:内 ...
- 关于Ext4 extraParams 不能传递动态参数的问题解决办法
可以监听请求发送之前的事件:beforeload ,然后再添加请求的参数 me.store = Ext.create('Ext.data.JsonStore', { remoteSort: true, ...
- Qt Creator 中的插件Plugin, 区分说明。。。
Qt Creator 中可以创建 三中类型的插件Plugin: 1.用的最多的,派生自QGenericPlugin类: 在新建Library, Plugin类型工程中,新建. 调用使用QPlugi ...
- GMTC全球大前端技术大会-未来已来
GMTC-2019北京 GMTC这次的大会的热词肯定是监控.性能,当然跨平台依然是热点,write once,run anywhere!,以下是自己参加的总结心得. 6.20上午 前端的演化 核心理念 ...
- redis 入门之集合
sadd 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略.假如 key 不存在,则创建一个只包含 member 元素作成员的集合.当 key 不 ...
- SpringBoot-技术专区-异步编程
最近在实现一个聚合搜索的需求时,由于需要从五个索引中查询数据,然后再将搜索结果组合返回给前端app展现,显然这个地方不能再用同步的方式来操作了,如果有一个索引查询出现耗时较长,那么其余的请求都会排同步 ...
- DB2实例
实例是逻辑数据库管理环境,可以在此环境中对数据库进行编目和设置配置参数.根据需要, 可以在一台服务器上创建多个实例,该服务器为每个实例提供唯一的数据库服务器环境. 默认实例:DB2 显示实例: ...
- docker 安装nexus
1.查找镜像 docker search nexus 2.拉取镜像 docker pull sonatype/nexus3 3.启动容器 docker run -d -p 8081:8081 -p 8 ...
- 如何加快Vue项目的开发速度
如何加快Vue项目的开发速度 本文摘自奇舞周刊,侵权删. 现如今的开发,比如内部使用的管理平台这种项目大都时间比较仓促.实际上来说,在使用了webpack + vue 这一套来开发的话已经大大了提高了 ...
- sql server教程
简单认识 SQL Server sql server教程 SQL Server 是 Microsoft 开发的一个关系数据库管理系统(RDBMS),现在是世界上最为常用的数据库: SQL Server ...