Source:

PAT A1131 Subway Map (30 分)

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 and Si'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的更多相关文章

  1. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  2. PAT甲级——1131 Subway Map (30 分)

    可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30  ...

  3. A1131. Subway Map (30)

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  4. 1131 Subway Map DFS解法 BFS回溯!

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  5. 1131 Subway Map(30 分)

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  6. PAT 1131 Subway Map

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  7. PAT甲级——A1131 Subway Map【30】

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  8. 1131(★、※)Subway Map

    思路:DFS遍历 #include <iostream> #include <map> #include <vector> #include <cstdio& ...

  9. PAT甲级1131 Subway Map【dfs】【输出方案】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805347523346432 题意: 告诉你一个地铁线路图,站点都是 ...

随机推荐

  1. H5rem

    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1, ...

  2. USACO 5.5 章节

    Picture 题目大意 IOI 1998 求n (<=5000)个矩形 覆盖的图形 的周长(包括洞), 坐标范围[-10000,10000] 题解 一眼离散化+2维线段树,但仔细一想 空间不太 ...

  3. HDU 1205 吃糖果 (鸽巢原理)

    题目链接:HDU 1205 Problem Description HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢 ...

  4. Cocos2d-x打包安卓apk

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 1. 下载安装安卓(android)环境 见http://www.cnblogs.com/geore/p/5793620.html,按照其 ...

  5. SpringBoot连接Oracle

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  6. java中Comparator比较器顺序问题,源码分析

    提示: 分析过程是个人的一些理解,如有不对的地方,还请大家见谅,指出错误,共同学习. 源码分析过程中由于我写的注释比较啰嗦.比较多,导致文中源代码不清晰,还请一遍参照源代码,一遍参照本文进行阅读. 原 ...

  7. 关于she'll脚本中"echo -e"使用sh命令执会显示参数"-e"-的问题

    今天尝试写了个简单的菜单shell脚本文件: clear echo echo -e "\t\t\tSys Admin Menu\n" echo -e "\t1. Disp ...

  8. 修改默认runlevel

    CentOS直接修改文件  /etc/inittab 就好了 # Default runlevel. The runlevels used are: # - halt (Do NOT set init ...

  9. C#读取Xml中出现”&”等特殊符号

    原文:C#读取Xml中出现"&"等特殊符号 C#读取Xml中出现的特殊符号时用ASCII或者转定义名称代替.程序读进来后转成字符串后就自动变成相应的字符了,再度保存时会以正 ...

  10. Java8 新增BASE64加解密API

    什么是Base64编码? Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法 基于64个字符A-Z,a-z,0-9,+,/ ...