Editorial for WC '16 Contest 4 S2 - Pawpsicles


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Consider an implicit graph with 5N nodes, with each node representing a possible state which Nick can be in. Each node can be described by two values – his current location i (1 \le i \le N), and the next location type t which he needs to visit (1 \le t \le 5, with t = 5 representing that he's already completed all 4 steps of his Pawpsicle operation). His initial state is \{i = 1, t = 1\}, and he wants to reach a state \{i, t = 5\} (for any i) as quickly as possible. By travelling along roads and visiting the correct types of locations, he can move between these states in certain amounts of time. These transitions between states correspond to weighted, directed edges in the implicit graph.

At this point, we've reduced the problem to finding the shortest path on a graph, a well-known task which can be accomplished using Dijkstra's algorithm in \mathcal O(E+V \log V) time, where V and E are the number of nodes and edges in the implicit graph, respectively. In this case, V = 5N, and E is in \mathcal O(N+M). What remains is implementing Dijkstra's, and handling the implicit graph's edges properly. For example, the j-th road can be used to go from state \{i = A_j, t\} to \{B_j, t\} for any value of t, and for each node j such that T_j > 0, there exists a weight-0 edge from state \{i = j, t = T_j\} to \{i = j, t = T_j+1\}. We want to stop as soon as we reach any node with t = 5.


Comments

There are no comments at the moment.