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 题意: 告诉你一个地铁线路图,站点都是 ...
随机推荐
- 重新认识Maven
PS:第一次接触maven大约是两年前吧,隐约记得之前都是人工寻找并下载很多jar,放在项目的lib中(表示太年轻,没有接触过Ant或者其他类似的工具,就不找别人写的比较了).懒人永远有着自己的小聪明 ...
- upc组队赛14 Floating-Point Hazard【求导】
Floating-Point Hazard 题目描述 Given the value of low, high you will have to find the value of the follo ...
- Python中的内置函数和匿名函数
1. 内置函数 print用法 def print(self, *args, sep=' ', end='\n', file=None): # known special case of print ...
- 继承Process类,run函数的简单使用
#定义一个类 继承Process类 from multiprocessing import Process import os import time class Download(Process): ...
- SpringCloud 使用Feign访问服务
Feign简介: 声明式的Rest WEB 服务的客户端, https://github.com/OpenFeign/feign.Spring Cloud 提供了Spring-cloud-start ...
- git 上传本地代码
新增本地代码到远程库 http://blog.csdn.net/hanhailong726188/article/details/46738929 github配置教程 http://www.runo ...
- springcloud费话之Eureka基础
目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...
- SQL数据库—<1>SQL语言
关系数据库.SQL语言简单.学习软件介绍 SQL:Structured Query Language 结构化查询语言 数据库分为:层次型,网状型,关系型. 关系型数据库:是一个二维表的集合,可以用来存 ...
- 【CSS3】transform-origin以原点进行旋转 (转)
话不多说, 以左上角为原点 -moz-transform-origin: 0 0; -webkit-transform-origin:0 0; -o-transform-origin:0 0; 以右上 ...
- 安装JDK,并检测JDK是否安装成功
方法/步骤 首先,我们需要先安装好我们的JDK软件,安装好之后,我们需要对我们的电脑进行环境变量配置的设置,这样我们安装的JDK才能真正起到作用. 第一步:我们需要将光标放在“计算机”上面,然 ...