Editorial for Baltic OI '02 P1 - Speed Limits
Submitting an official solution before solving the problem yourself is a bannable offence.
Let denote the number of different speed limits occurring. It is clear that . Construct a new graph with vertices, each one corresponding to the state of being at crossing and having speed . There is a directed edge from vertex to vertex if and only if there is a road from crossing to crossing and one of the following holds:
- and
If the weight of the edge is , then the original problem exactly corresponds to finding the shortest path in the new graph from to with arbitrary.
To find the shortest path, Dijkstra's algorithm can be used. The graph does not have to be constructed explicitly. In a typical instance, many of the vertices will not be reached since they have a much longer travel time than the destination. To solve all test cases, a priority queue must be implemented, for example by using a heap or a binary heap.
The test cases distinguish between
- a recursive solution
- a clever recursive solution which saves the best path length for given crossing and speed
- Dijkstra's shortest path with a normal array
- Dijkstra's shortest path with a priority queue
The maximum number of crossings () and velocities () are chosen because even the fourth solution starts to take a long time in that region.
Comments