题意

给出地铁线路数n,分别给出每条线的站点数m,再依次列出站点id。然后询问k次从启点sv到终点ev的最短路径,如果最短路径相同,要求换乘最少的路径。最后按条件输出。

思路

  • 1、用unordered_map<int, unordered_map<int, int>> rec记录图,目的是在常数时间复杂度范围内知道某条边是第几号线:rec[i][j]表示从i节点到j节点是走第几号线。
unordered_map<int, unordered_map<int, int>> rec;
for(int i = 1; i <= n; i ++){scanf("%d", &m); for(int j = 1; j <= m; j ++){scanf("%d", &ev); if(j != 1) rec[sv][ev] = rec[ev][sv] = i;sv = ev;}
}
  • 2、构造DFS(int s, int cnt)函数,传参当前遍历的节点s,以及当前转了cnt趟车。
  • 3、用tmp数组记录DFS一路上经过的点,到达终点ev后判断是否为第一个or更优解,以复制给ans数组。
  • 4、根据题目要求,输出长度ans.size() - 1,并按要求输出路线。
int i = 0;
printf("%d\n", ans.size() - 1);
while(true){printf("Take Line#%d from %04d to ", rec[ans[i]][ans[i + 1]], ans[i]);while(i + 2 < ans.size() && rec[ans[i]][ans[i + 1]] == rec[ans[i + 1]][ans[i + 2]])i ++;printf("%04d.\n", ans[i + 1]);if(i + 2 == ans.size()) break;else i++;
}

总结

  • 注意不同算法中用pre数组记录前驱、cnt数组记录前置信息的方法。
  • map嵌套代替邻接矩阵存图的方法。可用来存大范围id(eg: id∈[-1E9, 1E9]) or不规则id(eg: id = "ROM") + 边信息(eg: rec[i][j] = {dist, cost})。
  • 应该是目前见过的最麻烦的图论题了。

做题过程记录

  • 一开始想用dijkstra,但是该算法时间复杂度 O ( n 2 ) O(n^2) O(n2) = 1E8显然会TLE放弃。
  • 然后自然想到DFS,但是1E4个结点感觉递归会爆栈。网上查了下对于递归栈5 * 1E3的深度对应1MB(一个深度保存2个int参数),题目给了64MB,所以不用担心。可是自己用PAT测试发现上述结论存疑,递归了5 * 1E6次仍不报段错误SF,再往上报的是超时TLE。
  • 回过头想着dij堆优化,于是补上dijkstra,果然TLE了,见解法3。
  • 堆优化一下,14ms,见解法4,但是不知道为什么测试点3过不去,感觉逻辑没问题。
  • 看到有佬说spfa,但是spfa时间复杂度最大可以是 k ⋅ O ( n v e r n a r c ) = 1 E 7 k\cdot O(n_{ver}n_{arc}) = 1E7 k⋅O(nver​narc​)=1E7会TLE。反思这种笼统思考有问题的。什么时候spfa趋向上述极限呢?稠密图,且权值分布呈梯度情况(找节点1到节点n的最短路径,节点1有n-1条边,到节点2最短,节点2有除了通往节点1外的n-2条边,节点1经过节点2到节点3 - n比直接到达节点3 - n都短,直到节点n)。而题目规定最多5条线经过1个点,也就是该点的度最大为10,卡不住的。写了下,果然AC,见解法2。

遗留问题

  • DFS递归报错SF的边界?
  • 堆优化dij过不了测试点3的原因?

解法1 - DFS(AC)

#include<bits/stdc++.h>
using namespace std;
int n, m, k, sv, ev, mcnt;
bool st[10001];
unordered_map<int, unordered_map<int, int>> rec;
deque<int> tmp, ans;
void DFS(int s, int cnt){if(ans.size() && tmp.size() > ans.size()) return;if(s == ev){if(!ans.size() || tmp.size() < ans.size() || (tmp.size() == ans.size() && mcnt > cnt)){ans = tmp;mcnt = cnt;}return;}for(auto i : rec[s]){if(!st[i.first]){st[i.first] = true;if(tmp.size() != 1 && rec[tmp[tmp.size() - 2]][s] != rec[s][i.first]) cnt++;tmp.push_back(i.first);DFS(i.first, cnt);tmp.pop_back();if(tmp.size() != 1 && rec[tmp[tmp.size() - 2]][s] != rec[s][i.first]) cnt--;st[i.first] = false;}}
}
int main(){scanf("%d", &n); for(int i = 1; i <= n; i ++){scanf("%d", &m); for(int j = 1; j <= m; j ++){scanf("%d", &ev); if(j != 1) rec[sv][ev] = rec[ev][sv] = i;sv = ev;}}scanf("%d", &k); while(k--){scanf("%d %d", &sv, &ev); tmp.clear(); ans.clear();tmp.push_back(sv);memset(st, false, sizeof st);st[sv] = true;DFS(sv, 0);int i = 0;printf("%d\n", ans.size() - 1);while(true){printf("Take Line#%d from %04d to ", rec[ans[i]][ans[i + 1]], ans[i]);while(i + 2 < ans.size() && rec[ans[i]][ans[i + 1]] == rec[ans[i + 1]][ans[i + 2]])i ++;printf("%04d.\n", ans[i + 1]);if(i + 2 == ans.size()) break;else i++;}}return 0;
}

解法2 - SPFA(AC)

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
int n, m, k, sv, ev, mcnt;
bool st[10001], exi[10001];
int d[10001], cnt[10001], pre[10001];
unordered_map<int, unordered_map<int, int>> rec;
deque<int> tmp, ans;
void dij(){memset(st, false, sizeof st), memset(d, 0x3f, sizeof d);memset(cnt, 0, sizeof cnt), memset(pre, -1, sizeof pre);d[sv] = 0, cnt[sv] = 1;queue<int> que;que.push(sv);while(que.size()){int tar = que.front(); que.pop();st[tar] = false;for(auto j : rec[tar]){if(d[j.first] > d[tar] + 1){d[j.first] = d[tar] + 1;pre[j.first] = tar;cnt[j.first] = cnt[tar];if(!st[j.first]){st[j.first] = true;que.push(j.first);}if(tar != sv && j.second != rec[pre[tar]][tar]) cnt[j.first]++;}else if(d[j.first] == d[tar] + 1){int tranold = cnt[j.first], trannew = cnt[tar];if(tar != sv && j.second != rec[pre[tar]][tar]) trannew ++;if(tranold > trannew){pre[j.first] = tar;cnt[j.first] = trannew;}}}}while(ev != -1){ans.push_front(ev);ev = pre[ev];}int i = 0;printf("%d\n", ans.size() - 1);while(true){printf("Take Line#%d from %04d to ", rec[ans[i]][ans[i + 1]], ans[i]);while(i + 2 < ans.size() && rec[ans[i]][ans[i + 1]] == rec[ans[i + 1]][ans[i + 2]])i ++;printf("%04d.\n", ans[i + 1]);if(i + 2 == ans.size()) break;else i++;}
}
int main(){scanf("%d", &n); for(int i = 1; i <= n; i ++){scanf("%d", &m); for(int j = 1; j <= m; j ++){scanf("%d", &ev); exi[ev] = true;if(j != 1) rec[sv][ev] = rec[ev][sv] = i;sv = ev;}}scanf("%d", &k); while(k--){scanf("%d %d", &sv, &ev); ans.clear();dij();}return 0;
}

解法3 - 常规dij - (测试点5 - TLE)

#include<bits/stdc++.h>
using namespace std;
int n, m, k, sv, ev, mcnt;
bool st[10001], exi[10001];
int d[10001], cnt[10001], pre[10001];
unordered_map<int, unordered_map<int, int>> rec;
deque<int> tmp, ans;
void dij(){memset(st, false, sizeof st), memset(d, 0x3f, sizeof d);memset(cnt, 0, sizeof cnt), memset(pre, -1, sizeof pre);d[sv] = 0;for(int i = 0; i < 10001; i ++){int tar = -1;for(int j = 0; j < 10001; j ++)if(exi[j] && !st[j] && (tar == -1 || d[j] < d[tar])) tar = j;if(tar == -1 || tar == ev) break;st[tar] = true;for(auto j : rec[tar]){if(d[j.first] > d[tar] + 1){d[j.first] = d[tar] + 1;pre[j.first] = tar;cnt[j.first] = cnt[tar];if(pre[tar] != -1 && rec[tar][j.first] != rec[pre[tar]][tar]) cnt[j.first]++;}else if(d[j.first] == d[tar] + 1){int tranold = cnt[j.first], trannew = cnt[tar];if(pre[tar] != -1 && rec[tar][j.first] != rec[pre[tar]][tar]) trannew ++;if(tranold > trannew){pre[j.first] = tar;cnt[j.first] = trannew;}}}}while(ev != -1){ans.push_front(ev);ev = pre[ev];}int i = 0;printf("%d\n", ans.size() - 1);while(true){printf("Take Line#%d from %04d to ", rec[ans[i]][ans[i + 1]], ans[i]);while(i + 2 < ans.size() && rec[ans[i]][ans[i + 1]] == rec[ans[i + 1]][ans[i + 2]])i ++;printf("%04d.\n", ans[i + 1]);if(i + 2 == ans.size()) break;else i++;}
}
int main(){scanf("%d", &n); for(int i = 1; i <= n; i ++){scanf("%d", &m); for(int j = 1; j <= m; j ++){scanf("%d", &ev); exi[ev] = true;if(j != 1) rec[sv][ev] = rec[ev][sv] = i;sv = ev;}}scanf("%d", &k); while(k--){scanf("%d %d", &sv, &ev); ans.clear();dij();}return 0;
}

解法4 - 堆优化dijkstra - (测试点3错误)

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
int n, m, k, sv, ev, mcnt;
bool st[10001], exi[10001];
int d[10001], cnt[10001], pre[10001];
unordered_map<int, unordered_map<int, int>> rec;
deque<int> tmp, ans;
void dij(){memset(st, false, sizeof st), memset(d, 0x3f, sizeof d);memset(cnt, 0, sizeof cnt), memset(pre, -1, sizeof pre);d[sv] = 0, cnt[sv] = 1;priority_queue<pii, vector<pii>, greater<pii>> que;que.push({0, sv});while(que.size()){auto it = que.top(); que.pop();int tar = it.second;if(st[tar]) continue;if(tar == ev) break;st[tar] = true;for(auto j : rec[tar]){if(d[j.first] > d[tar] + 1){d[j.first] = d[tar] + 1;pre[j.first] = tar;cnt[j.first] = cnt[tar];que.push({d[j.first], j.first});if(tar != sv && j.second != rec[pre[tar]][tar]) cnt[j.first]++;}else if(d[j.first] == d[tar] + 1){int tranold = cnt[j.first], trannew = cnt[tar];if(tar != sv && j.second != rec[pre[tar]][tar]) trannew ++;if(tranold > trannew){pre[j.first] = tar;cnt[j.first] = trannew;}}}}while(ev != -1){ans.push_front(ev);ev = pre[ev];}int i = 0;printf("%d\n", ans.size() - 1);while(true){printf("Take Line#%d from %04d to ", rec[ans[i]][ans[i + 1]], ans[i]);while(i + 2 < ans.size() && rec[ans[i]][ans[i + 1]] == rec[ans[i + 1]][ans[i + 2]])i ++;printf("%04d.\n", ans[i + 1]);if(i + 2 == ans.size()) break;else i++;}
}
int main(){scanf("%d", &n); for(int i = 1; i <= n; i ++){scanf("%d", &m); for(int j = 1; j <= m; j ++){scanf("%d", &ev); exi[ev] = true;if(j != 1) rec[sv][ev] = rec[ev][sv] = i;sv = ev;}}scanf("%d", &k); while(k--){scanf("%d %d", &sv, &ev); ans.clear();dij();}return 0;
}

题目

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 (i=1,⋯,N) 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 (i=1,⋯,M) 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] (i=1,⋯,M−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.

1131. Subway Map (30)-PAT甲级真题 (DFS or 堆优化dij or SPFA)相关推荐

  1. 1091. Acute Stroke (30)-PAT甲级真题(广度优先搜索)

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...

  2. 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  3. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

  4. PAT甲级1131 Subway Map (30分):[C++题解]堆优化dijkstra、单源最短路、地铁地图、巧妙地建图套dijkstra模板!!

    文章目录 题目分析 题目链接 题目分析 原题: 来源:acwing 分析: 建图:所有能走到的点之间建立一条边,比如下面一条地铁线路有4站,它们是相通的,两两之间建一条边,边权是经过的站点数. 下面考 ...

  5. PAT 1131. Subway Map (30) DFS

    1131. Subway Map (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In the big ...

  6. PAT甲级真题 1018 A+B in Hogwarts--python解法

    PAT甲级真题 1018 A+B in Hogwarts 提交:2638 通过:1559 通过率:59% If you are a fan of Harry Potter, you would kno ...

  7. PAT甲级1003 Emergency Dijkstra算法(堆优化版/朴素版)

    前言   最近花了很多的时间在写JAVA项目上面,疏忽了算法和数据结构的学习.最近突然醒悟基础更为重要,打算从今天开始每天抽出一些时间做下PAT甲级的题目.现有题库的前两题很简单,从第三题开始吧. 题 ...

  8. 【PAT甲级真题整理五】1121~1155

    终于考完了qaq把最后一堆也整理出来了 目录 1121 Damn Single(25)set.map的使用 1122 Hamiltonian Cycle(25)哈密顿回路 1123 Is It a C ...

  9. 【PAT甲级真题整理三】1061~1090

    目录 1061 Dating(20)字符串处理 1062 Talent and Virtue(25)排序 1063 Set Similarity(25)set的使用 1064 Complete Bin ...

最新文章

  1. Linux Socket学习(十三)
  2. 如何替换字符串中出现的所有字符?
  3. 基于 Laravel、Vue.js开发的全新社交系统----ThinkSNS+
  4. mysql分区字段创建索引_MySQL分区字段列有必要再单独建索引吗?
  5. 嵌入式转linux服务器,嵌入式linux下web服务器搭建
  6. 作为算法工程师,在咨询公司工作的另类体验
  7. Codeforces Round #462 (Div. 2)
  8. nginx+tomcat的keepalive验证、bio/nio连接比较
  9. Envi和ArcGIS软件打开和处理.NC4数据
  10. 阿里云96页报告详解《云上转型》(10个案例、10大趋势/完整版PPT)
  11. SMM - 系统管理模式,SMRAM
  12. 模糊综合评价的 matlab,模糊综合评价法代码matlab
  13. mac / Ubuntu 终端下添加tree命令显示文件目录结构
  14. c# 服务器打印word文档,C#中5步完成word文档打印的方法
  15. 嵌入式开发——用memtester软件进行内存压力测试
  16. 罗升阳 android系统源代码情景分析,Android系统源代码情景分析
  17. 基于asyncio编写一个telegram爬虫机器人
  18. 海思hi3516dv300 配置uart3
  19. Linux搭建泰拉瑞亚(原版/模组/插件)服务器之1.4模组服务器
  20. 巧用模板和友联类型为vc++单元测试加一利器

热门文章

  1. 摊牌了,微软始料未及,Excel和WPS用户:我们已经在使用了
  2. 挑战编程:回文字符串
  3. OpenCV 之视频文件的处理
  4. 十一、Actix-web 拦截器中间件
  5. 技术分享 | 如何发现企业云网络中的安全隐患
  6. 微信私域流量池运营:您是否错失了将粉丝转变为客户的机会?电商宝提供一站式客户运营转化服务!...
  7. 如何在Linux中安装应用程序
  8. 淘宝商品类目查询方法
  9. 故障案例----tokudb启动失败
  10. 2021年单招计算机专业学什么,2021单招十大类分别是什么?