✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1131 Subway Map (pintia.cn)
🔑中文翻译:地铁地图
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪
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. ......
- 1
- 2
- 3
where
X
i’s are the line numbers andS
i’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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
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.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
请你帮助编写一个程序,给定用户的起始位置,找到到达其目的地的最快方法。
第一行包含一个正整数 N N N ,表示地铁线路的数量。
接下来
N
N
N 行,第 i
行以下列格式描述第 i
条线路(地铁线路都是双向的):
M S[1] S[2] ... S[M]
其中
M
M
M 是站点数量,S[i]
是沿线站点的编号(站点编号是从 0000
到 9999
的 4
位数字)。
确保这些站点是照地铁行进顺序给出的,即地铁会从 S[i]
直接开到 S[i+1]
。
注意,可能会存在回路,但不存在自环(即地铁从 S
出发,直接开向 S
,中途不经过任何站点)。
每个车站间隔都只属于一条唯一线路。
一些线路可能会在某些站点(中转站)彼此交叉,但是不能有任何站点作为中转站时,有超过 5 条线路在该站点交汇。
描述完地铁线路信息后,包含一行整数 K K K ,表示询问次数。
接下来 K K K 行,每行描述一个询问,包含两个站点编号分别表示始发站和目的地。
对于每个询问,首先输出最少需要停靠的站点数量,然后以如下格式输出最佳线路:
Take Line#X1 from S1 to S2.
Take Line#X2 from S2 to S3.
......
其中 Xi
是线路编号,Si
是站点编号,除始发站和终点站外,只输出中转站。
如果最快线路不唯一,则输出换乘次数最少的线路,保证唯一。
这道题我们不能用常规的迪杰斯特拉算法来做,这样会超时,并且在存储图的设计上我们需要进行改动,由于给的图是分成了很多的线路,直接用原来的方法去存储不太好存。所以这里我们将每条线路上的每个点都连起来,这样线路上的每个点直接距离多少就可以直接知道,如下图所示:
另外,还需要注意的是如果线路出现回路需要额外进行判断,因为每个点都可以往左和往右走,需要判断是往左走到目的站点近还是往右走到目的站点近。
当存储完图之后,就要开始计算给定两点之间的最短路线,这里我们用到了堆优化版的迪杰斯特拉算法,在原有算法模板的基础上,对需要的答案进行更新操作。需要注意的是,如果出现路径相同的路线,则选取换乘次数最少的那条路线。还有就是站点编号必须是 4
位数字,在将数字转换成字符串的时候,要对于不满 4
位的数字前面进行补 0
操作。
最后从终点往前将路径输入容器中,再将容器从后往前输出即可得到最终答案。
#include
using namespace std;
typedef pair<int, int> PII;
const int N = 10010, M = 1000010;
int n;
int h[N], ne[M], line[M], e[M], w[M], idx;
int dist[N], cnt[N], pre[N];
string info[N];
bool st[N];
int stops[N];
//链式前向星存储邻接表
void add(int a, int b, int c, int t)
{
e[idx] = b, w[idx] = c, line[idx] = t, ne[idx] = h[a], h[a] = idx++;
}
//将站点编号转化为4位的字符串
string get_number(int x)
{
char buf[5];
sprintf(buf, "%04d", x); //不足4位的用0填充
return buf;
}
//堆优化版迪杰斯特拉算法
void dijkstra(int start, int end)
{
//初始化
memset(dist, 0x3f, sizeof dist);
memset(cnt, 0x3f, sizeof cnt);
memset(st, false, sizeof st);
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({ 0,start });
dist[start] = cnt[start] = 0;
//开始寻找最近的点进行更新
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.second;
if (ver == end) break; //如果已经到终点则直接退出
if (st[ver]) continue; //如果已经遍历过则直接跳过
st[ver] = true;
//遍历与该点相连的所有边
for (int i = h[ver]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[ver] + w[i])
{
//路径可以更新的情况
dist[j] = dist[ver] + w[i];
cnt[j] = cnt[ver] + 1;
pre[j] = ver;
info[j] = "Take Line#" + to_string(line[i]) + " from " +
get_number(ver) + " to " + get_number(j) + ".";
heap.push({ dist[j],j });
}
else if (dist[j] == dist[ver] + w[i])
{
//路径长度相等需要根据换乘次数判断的情况
if (cnt[j] > cnt[ver] + 1)
{
cnt[j] = cnt[ver] + 1;
pre[j] = ver;
info[j] = "Take Line#" + to_string(line[i]) + " from " +
get_number(ver) + " to " + get_number(j) + ".";
}
}
}
}
//输出答案
cout << dist[end] << endl;
vector<string> path;
for (int i = end; i != start; i = pre[i])
path.push_back(info[i]);
for (int i = path.size() - 1; i >= 0; i--)
cout << path[i] << endl;
}
int main()
{
cin >> n;
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i++)
{
int m;
cin >> m;
for (int j = 0; j < m; j++) cin >> stops[j];
//给每个点之间都连一条边
for (int j = 0; j < m; j++)
for (int k = 0; k < j; k++)
{
//如果线路是闭环,需要找到最短的那条边
int len;
if (stops[0] != stops[m - 1]) len = j - k;
else len = min(j - k, m - 1 - j + k);
add(stops[j], stops[k], len, i);
add(stops[k], stops[j], len, i);
}
}
int k;
cin >> k;
while (k--)
{
int start, end;
cin >> start >> end;
dijkstra(start, end);
}
return 0;
}