Editorial for IOI '11 P4 - Crocodile's Underground City
Submitting an official solution before solving the problem yourself is a bannable offence.
Subtask 1: Trees
In this subtask, we can infer that the underlying graph is a tree and the exit chambers are precisely the leaves. This is simpler than the general case but will be instructive for the next subtasks. To begin, let denote the time to travel along the corridor connecting node to node . For simplicity, we will root the tree at the starting node (chamber ).
The crucial observation then is that in any successful escape plan, the instruction at each node, if reachable from the root, will always tell Somying to move further away from the root (otherwise, she can be forced to cycle around the underground city forever). This leads to a simple dynamic programming (DP) solution: Let denote the best time to reach an exit chamber. First, if is a leaf. Otherwise, if has children , ordered such that , then . The problem statement guarantees that .
It is easy to show inductively that is indeed the best time starting at . Specifically, we prove that first, Benjamas can reach an exit chamber from in time regardless of what the gatekeeper does, and second, the gatekeeper can force Benjamas to spend time in the underground city. Clearly, if is a leaf node and hence an exit chamber, is . Assuming inductively that is as claimed for each child of , we have that in time , Benjamas can reach an exit from , through , if the corridor is not blocked. Since the evil crocodile can only block one corridor at a time, he can force Benjamas to spend , by blocking —blocking any other corridor only helps Benjamas reach an exit faster, in time.
To compute this DP, we traverse the tree in postorder (i.e., the leaves are visited first and each parent is visited after all its children); the final answer is stored in . The computation at each node involves finding the second smallest value, which can be done in time. Here, denotes the (out-)degree of . Therefore, the total running time is , for some positive constant , because the degrees of the tree nodes sum to .
Subtasks 2 and 3: General Graphs
The challenge in generalizing our current algorithm to the general setting is the lack of a clear sense of direction; in our current algorithm, we know Benjamas must always move further from the root as necessitated by the tree structure.
A moment's thought reveals a striking similarity between Dijkstra's single-source shortest path algorithm and our algorithm for trees. Indeed, the algorithm iteratively grows the frontier set, where at any point in time, a node is in the set if has been determined. From this view, our algorithm for trees can be seen as running Dijkstra's algorithm starting from the exit chambers. The algorithm is standard except for how the cost at a node is defined.
Consider the following algorithm: For all nodes , set to except when is an exit chamber, set . Initially, the frontier set contains exactly the exit chambers. During the execution of the algorithm, we maintain that for , the cost of can be conceptually computed by producing the list1 , sorting this list, and returning the second value (i.e., the second smallest value in this list). When a node enters the frontier (it has the lowest cost among non-frontier nodes), is set to the cost of at that moment.
Claim 1. For each node , Benjamas can reach an exit from in time regardless of what the gatekeeper does. Furthermore, the crocodile gatekeeper can force Benjamas to spend time in the underground city.
This claim can be shown by a similar inductive argument as in the tree case and by observing that as in Dijkstra's algorithm, once a node enters the frontier its cost cannot decrease because the edges have positive cost.
Implementation Details. For each node, we can maintain its cost by keeping two numbers—the smallest value and the second smallest value—which can be updated in when the neighboring values change. Using this, Dijkstra's algorithm takes using a heap or without using one.
1 denotes the set of neighbors of .
Comments