PAT 1111. 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: 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: Distance = D: source -> v1 -> … -> destination Then in the next line print the fastest path with total time T: 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: Distance = D; Time = T: source -> u1 -> … -> destination

Sample Input 1:

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:

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

Sample Input 2:

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:

Distance = 3; Time = 4: 3 -> 2 -> 5

用两个Dijkstra就好了,注意如果距离一样,选取时间较短的哪一个;如果时间相等,选取经过的点最少的那个;

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int MAX = 500;

struct City {
int dis, time;
} cities[MAX][MAX];

int dis[MAX], collect[MAX], n, disPath[MAX], disTime[MAX], tt[MAX], timePath[MAX], timeUnit[MAX];
vector<int> final_dis_path, final_time_path;

int getIndexForDistance() {
int index = -1, i = 0;
for (i = 0; i < n; i++) {
if (!collect[i]) {
index = i;
break;
}
}
for (i += 1; i < n; i++) {
if (!collect[i] && dis[i] < dis[index]) {
index = i;
}
}
return index;
}

int getIndexForTime() {
int index = -1, i = 0;
for (i = 0; i < n; i++) {
if (!collect[i]) {
index = i;
break;
}
}
for (i += 1; i < n; i++) {
if (!collect[i] && tt[i] < tt[index]) {
index = i;
}
}
return index;
}

void dijkstraForTime(int s) {
fill(tt, tt + MAX, 65536);
fill(collect, collect + MAX, 0);
tt[s] = 0;
timeUnit[s] = 0;

for (int i = 0; i < n; i++) {
timePath[i] = i;
}
while (true) {
s = getIndexForTime();
if (s == -1) break;
collect[s] = 1;
for (int i = 0; i < n; i++) {
if (!collect[i] && cities[s][i].time != 0) {
if (tt[i] > tt[s] + cities[s][i].time) {
tt[i] = tt[s] + cities[s][i].time;
timePath[i] = s;
timeUnit[i] = timeUnit[s] + 1;
} else if (tt[i] == tt[s] + cities[s][i].time && timeUnit[i] > timeUnit[s] + 1) {
timePath[i] = s;
timeUnit[i] = timeUnit[s] + 1;
}
}
}
}
}

void dijkstraForDistance(int s) {
fill(dis, dis + MAX, 65536);
fill(collect, collect + MAX, 0);
dis[s] = 0;
disTime[s] = 0;

for (int i = 0; i < n; i++) {
disPath[i] = i;
}
while (true) {
s = getIndexForDistance();
if (s == -1) break;
collect[s] = 1;
for (int i = 0; i < n; i++) {
if (!collect[i] && cities[s][i].dis != 0) {
if (dis[i] > dis[s] + cities[s][i].dis) {
dis[i] = dis[s] + cities[s][i].dis;
disPath[i] = s;
disTime[i] = disTime[s] + cities[s][i].time;
} else if (dis[i] == dis[s] + cities[s][i].dis && disTime[i] > disTime[s] + cities[s][i].time) {
disPath[i] = s;
disTime[i] = disTime[s] + cities[s][i].time;
}
}
}
}
}

void getDisPath(int s, int e) {
if (s == e) {
final_dis_path.push_back(s);
return;
}
getDisPath(s, disPath[e]);
final_dis_path.push_back(e);
}

void getTimePath(int s, int e) {
if (s == e) {
final_time_path.push_back(s);
return;
}
getTimePath(s, timePath[e]);
final_time_path.push_back(e);
}

void printTimePath() {
printf("%d", final_time_path[0]);
for (int i = 1; i < final_time_path.size(); i++) {
printf(" -> %d", final_time_path[i]);
}
}

void printDisPath() {
printf("%d", final_dis_path[0]);
for (int i = 1; i < final_dis_path.size(); i++) {
printf(" -> %d", final_dis_path[i]);
}
}

int main() {
int m = 0;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int v1 = 0, v2 = 0, oneWay = 1;
scanf("%d %d %d", &v1, &v2, &oneWay);
scanf("%d %d", &cities[v1][v2].dis, &cities[v1][v2].time);
if (!oneWay) {
cities[v2][v1] = cities[v1][v2];
}
}
int s = 0, e = 0;
scanf("%d %d", &s, &e);
dijkstraForDistance(s);
getDisPath(s, e);
dijkstraForTime(s);
getTimePath(s, e);

if (final_dis_path != final_time_path) {
printf("Distance = %d: ", dis[e]);
printDisPath();
printf("\n");
printf("Time = %d: ", tt[e]);
printTimePath();

} else {
printf("Distance = %d; Time = %d: ", dis[e], tt[e]);
printTimePath();
}
return 0;
}