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. Makefile之自动化变量

    makefile自动化变量在大型项目的Makefile使用的太普遍了,如果你看不懂自动化变量,开源项目的makefile你是看不下去的. 以往总是看到一些项目的makefile,总是要翻gnu的Mak ...

  2. PAT 2019-3 7-4 Structure of a Binary Tree

    Description: Suppose that all the keys in a binary tree are distinct positive integers. Given the po ...

  3. RQNOJ PID331 家族

    题目描述 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系. 规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚.如果x,y是 ...

  4. upc组队赛14 Communication【并查集+floyd /Tarjan】

    Communication 题目描述 The Ministry of Communication has an extremely wonderful message system, designed ...

  5. 爬取拉钩网上所有的python职位

    # 2.爬取拉钩网上的所有python职位. from urllib import request,parse import json,random def user_agent(page): #浏览 ...

  6. 为什么学python

    一.什么是Python Python [1](英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum发明. ...

  7. Volatile 只保证可见性,并不保证原子性

    [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article/details/52525724   在说明Java多线程内存可见性之前,先来简单了解一下J ...

  8. How to compile Linux kernel in fedora 6

    前提:已裝好Fedora 6 core 2.6.18 ,在 Fedora 6 中compile linux kernel.1.下載 Fedora 6 core 2.6.18 http://www.ke ...

  9. Docker镜像仓库的搭建--> Harbor篇

    简介 Harbor是VMware公司开源的一个企业级Docker Registry项目,项目地址:https://github.com/goharbor/harbor Harbor作为一个企业级私有R ...

  10. Codeforces Paths and Trees

    Paths and Trees time limit per test3 seconds memory limit per test256 megabytes Little girl Susie ac ...