Editorial for WC '17 Contest 1 S3 - Crosscountry Canada
Submitting an official solution before solving the problem yourself is a bannable offence.
This problem can be reinterpreted as asking for the shortest path on an implicit directed, weighted graph.
Each node in the graph should represent a particular state which you may be in during your trip. This can be described by a pair of integers , where is the city you're currently in, and is the number of minutes which have gone by since your last Tim Horton's visit (or since the start of your trip). In total, there are nodes.
The edges in this graph should then correspond to valid transitions between such states, of which there are two types. The first type of transition involves stopping at a Tim Horton's. For each node such that , there exists an edge to node with weight . The second type of transition involves driving along a road. For each road and value , there exists an edge from node to node with weight , as long as . There's also a similar edge for taking the road in the opposite direction. In total, there are edges.
With this interpretation, the answer is the length of the shortest path from node to any node . This can be computed using the well-known "Dijkstra's algorithm" in time, where and .
Comments