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:
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 | 10 15 |
Sample Output 1:
1 | Distance = 6: 3 -> 4 -> 8 -> 5 |
Sample Input 2:
1 | 7 9 |
Sample Output 2:
1 | Distance = 3; Time = 4: 3 -> 2 -> 5 |
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
题目大意
给出一张图,结点之间既有长度也有时间。求起点至终点最短路径、时间最短路径。其中最短路径有多条时,输出时间最短的。时间最短路径有多条时,输出路上结点数最少的。
分析
使用两次dijkstra。因为要输出路径,用pre数组记录前驱。tatn(timeatnode)记录走至第i个结点的时间,natn(numberatnode)记录走至第i个结点时路径上结点数。
代码
1 |
|
其他
测试点4是时间相同情况,测试点2是路径长度相同情况。