博客
关于我
1131 Subway Map
阅读量:420 次
发布时间:2019-03-06

本文共 5347 字,大约阅读时间需要 17 分钟。

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:

47 1001 3212 1003 1204 1005 1306 77979 9988 2333 1204 2006 2005 2004 2003 2302 200113 3011 3812 3013 3001 1306 3003 2333 3066 3212 3008 2302 3010 30114 6666 8432 4011 130633011 30136666 20012004 3001
 

Sample Output:

2Take Line#3 from 3011 to 3013.10Take Line#4 from 6666 to 1306.Take Line#3 from 1306 to 2302.Take Line#2 from 2302 to 2001.6Take Line#2 from 2004 to 1204.Take Line#1 from 1204 to 1306.Take Line#3 from 1306 to 3001.

题意:

  用一张无权无向图来表示一个城市的地铁,求出从起始地点到达目标地点的最少换乘的方案。

思路:

  首先需要将两站点之间是属于几号线这个问题解决了,我们用unordered_map<int, int> lines来表示,first = station1*100000 + station2,second = 地铁线路。然后用广搜来找从start到destination的最短路径,这样找到的最短路径可能有多条,我们需要再用深搜来找到换乘最少的最短路径。无权连通图用邻接表来表示。dis[i]表示从起点到达结点i的最短路径。将dis初始化为INT_MAX,然后通过判断dis[i] != INT_MAX来判断结点i是不是已经遍历过了。二维vector数组past[][]用来存储在寻找最短路的过程中当前结点的先驱结点。

  这样所有的最短路径都存在了past数组中了,然后我们需要通过DFS来寻找最短路径中换乘次数最少的那条路线。DFS目标站点dest开始搜索,通过past[dest]来寻找当前节点的先驱结点,将所有的先驱结点都存在path数组中,当找到start之后,根据path数组中的记录找到相邻路径点之间是否换乘。这里用到了三个节点我们用i-1, i, i+1来表示这三个结点,判断是否换乘只需要判断lines[path[i-1]*10000+path[i]] == lines[path[i]*10000+path[i+1]]是否相等。如果换乘的话则将其存入临时的tempTrans数组中记录。因为有多条最短路径,所以在查找每条最短路径的时候,Trans都要找长度最短的tempTrans来进行更新。

 

Code:

 

1 #include 
2 3 using namespace std; 4 5 unordered_map
lines; 6 vector
> trans, tempTrans; 7 vector
grap[10005], past[10005], path, temp, dis(10005); 8 int n, m, k, start, dest; 9 10 void BFS() {11 queue
que;12 // 初始化13 for (int i = 0; i < 10005; ++i) {14 past[i].clear();15 dis[i] = INT_MAX;16 }17 que.push(start);18 dis[start] = 0;19 while (!que.empty()) {20 int q = que.front();21 que.pop();22 if (dis[q] > dis[dest]) continue;23 for (int g : grap[q]) {24 if (dis[g] >= dis[q] + 1) {25 if (dis[g] == INT_MAX) que.push(g);26 dis[g] = dis[q] + 1;27 if (dis[g] > dis[q] + 1) past[g].clear();28 past[g].push_back(q);29 }30 }31 }32 }33 34 void DFS(int d) {35 path.push_back(d);36 if (d == start) {37 tempTrans.clear();38 tempTrans.push_back({start, -1});39 for (int i = path.size() - 2; i > 0; --i) {40 if (lines[path[i + 1] * 10000 + path[i]] !=41 lines[path[i] * 10000 + path[i - 1]]) {42 tempTrans.push_back(43 {path[i], lines[path[i] * 10000 + path[i + 1]]});44 }45 }46 tempTrans.push_back({dest, lines[path[1] * 10000 + dest]});47 if (trans.size() > tempTrans.size() || trans.size() == 0) {48 trans = tempTrans;49 }50 }51 for (int p : past[d]) DFS(p);52 path.pop_back();53 }54 55 int main() {56 cin >> n;57 for (int i = 1; i <= n; ++i) {58 cin >> m;59 temp = vector
(m);60 for (int j = 0; j < m; ++j) cin >> temp[j];61 for (int j = 1; j < m; ++j) {62 lines[temp[j] * 10000 + temp[j - 1]] = i;63 lines[temp[j - 1] * 10000 + temp[j]] = i;64 grap[temp[j]].push_back(temp[j - 1]);65 grap[temp[j - 1]].push_back(temp[j]);66 }67 }68 cin >> k;69 for (int i = 0; i < k; ++i) {70 cin >> start >> dest;71 72 BFS();73 path.clear();74 trans.clear();75 DFS(dest);76 cout << dis[dest] << endl;77 for (int i = 0; i < trans.size() - 1; ++i) {78 printf("Take Line#%d from %04d to %04d.\n", trans[i + 1].second,79 trans[i].first, trans[i + 1].first);80 }81 }82 83 return 0;84 }

 

  看到这道题的第一感觉就是这道题不好做,写的时候发现真的很难,于是,果断放弃,研究大佬的代码去了。(参考:)

 

转载地址:http://yntuz.baihongyu.com/

你可能感兴趣的文章
NIFI大数据进阶_NIFI集群知识点_认识NIFI集群以及集群的组成部分---大数据之Nifi工作笔记0014
查看>>
NIFI大数据进阶_NIFI集群知识点_集群的断开_重连_退役_卸载_总结---大数据之Nifi工作笔记0018
查看>>
NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
查看>>
NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
查看>>
NIFI大数据进阶_实时同步MySql的数据到Hive中去_可增量同步_实时监控MySql数据库变化_操作方法说明_01---大数据之Nifi工作笔记0033
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_01_实际操作---大数据之Nifi工作笔记0029
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
查看>>
NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
查看>>
NIFI数据库同步_多表_特定表同时同步_实际操作_MySqlToMysql_可推广到其他数据库_Postgresql_Hbase_SqlServer等----大数据之Nifi工作笔记0053
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001---大数据之Nifi工作笔记0068
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南002---大数据之Nifi工作笔记0069
查看>>
NIFI集群_内存溢出_CPU占用100%修复_GC overhead limit exceeded_NIFI: out of memory error ---大数据之Nifi工作笔记0017
查看>>
NIFI集群_队列Queue中数据无法清空_清除队列数据报错_无法删除queue_解决_集群中机器交替重启删除---大数据之Nifi工作笔记0061
查看>>
NIH发布包含10600张CT图像数据库 为AI算法测试铺路
查看>>
Nim教程【十二】
查看>>
Nim游戏
查看>>
NIO ByteBuffer实现原理
查看>>
Nio ByteBuffer组件读写指针切换原理与常用方法
查看>>
NIO Selector实现原理
查看>>
nio 中channel和buffer的基本使用
查看>>