PAT甲级:A1111 Online Map (30分)

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2≤N≤500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

1
V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N−1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

1
Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

1
Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

1
Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:

1
2
Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

1
2
3
4
5
6
7
8
9
10
11
7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:

1
Distance = 3; Time = 4: 3 -> 2 -> 5
  • 题意:一张网络地图,给定起点和终点,找到距离最短和用时最少的路径,如果最短路径不唯一则找用时最短的,如果用时最少的路径不唯一则找顶点最少的。
  • 分析:这题用两次Dijkstra算法就好了,根据题意把路径记录下来存在dispretimepre中,之后递归输出路径结果,因为路径相等的情况下是一行输出,而且是用vector存的,所以直接用==比较是否相等即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <bits/stdc++.h>
using namespace std;
const int N = 510, INF = 0x3f3f3f3f;
vector<int> e[N];
int n, m, st, dest, Dist[N][N], Time[N][N], d[N], t[N], num[N];
vector<int> dispre(N), timepre(N), dispath, timepath;
void dijkstraDistance(int s) {
for (int i = 0; i < N; i++) dispre[i] = i;
fill(d, d + N, INF);
fill(t, t + N, INF);
d[s] = t[s] = 0;
bool vis[N] = {false};
while (true) {
int u = -1, MIN = INF;
for (int i = 0; i < n; i++) {
if (!vis[i] && d[i] < MIN) {
u = i;
MIN = d[i];
}
}
if (u == -1) break;
vis[u] = true;
for (auto v : e[u]) {
if (!vis[v]) {
if (d[u] + Dist[u][v] < d[v]) {
d[v] = d[u] + Dist[u][v];
t[v] = t[u] + Time[u][v];
dispre[v] = u;
} else if (d[u] + Dist[u][v] == d[v] && t[u] + Time[u][v] < t[v]) {
t[v] = t[u] + Time[u][v];
dispre[v] = u;
}
}
}
}
}
void dijkstraTime(int s) {
for (int i = 0; i < N; i++) timepre[i] = i;
fill(num, num + N, INF);
fill(t, t + N, INF);
num[s] = t[s] = 0;
bool vis[N] = {false};
while (true) {
int u = -1, MIN = INF;
for (int i = 0; i < n; i++) {
if (!vis[i] && t[i] < MIN) {
u = i;
MIN = t[i];
}
}
if (u == -1) break;
vis[u] = true;
for (auto v : e[u]) {
if (!vis[v]) {
if (t[u] + Time[u][v] < t[v]) {
t[v] = t[u] + Time[u][v];
num[v] = num[u] + 1;
timepre[v] = u;
} else if (t[u] + Time[u][v] == t[v] && num[u] + 1 < num[v]) {
num[v] = num[u] + 1;
timepre[v] = u;
}
}
}
}
}
void dfsPath(vector<int> &pre, vector<int> &path, int u) {
if (u == st) {
path.push_back(u);
return;
}
dfsPath(pre, path, pre[u]);
path.push_back(u);
}
void printPath(vector<int> &path) {
for (int i = 0; i < path.size(); i++) {
if (i != 0) printf(" -> ");
printf("%d", path[i]);
}
printf("\n");
}
int main() {
scanf("%d%d", &n, &m);
int a, b, oneway, len, time;
for (int i = 0; i < m; i++) {
scanf("%d%d%d%d%d", &a, &b, &oneway, &len, &time);
e[a].push_back(b);
if (oneway == 0) e[b].push_back(a);
Dist[a][b] = Dist[b][a] = len;
Time[a][b] = Time[b][a] = time;
}
scanf("%d%d", &st, &dest);
dijkstraDistance(st);
dijkstraTime(st);
dfsPath(dispre, dispath, dest);
dfsPath(timepre, timepath, dest);
if (dispath == timepath) {
printf("Distance = %d; Time = %d: ", d[dest], t[dest]);
printPath(dispath);
} else {
printf("Distance = %d: ", d[dest]);
printPath(dispath);
printf("Time = %d: ", t[dest]);
printPath(timepath);
}
return 0;
}