博客
关于我
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/

你可能感兴趣的文章
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx、HAProxy、LVS
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx中使用expires指令实现配置浏览器缓存
查看>>
Nginx之二:nginx.conf简单配置(参数详解)
查看>>
Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
查看>>
Nginx代理初探
查看>>
nginx代理地图服务--离线部署地图服务(地图数据篇.4)
查看>>
Nginx代理外网映射
查看>>
Nginx代理模式下 log-format 获取客户端真实IP
查看>>
Nginx代理解决跨域问题(导致图片只能预览不能下载)
查看>>
Nginx代理访问提示ERR_CONTENT_LENGTH_MISMATCH
查看>>
Nginx代理配置详解
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
Nginx入门教程-简介、安装、反向代理、负载均衡、动静分离使用实例
查看>>
nginx反向代理
查看>>