Editorial for CCC '15 S4 - Convex Hull
Submitting an official solution before solving the problem yourself is a bannable offence.
by Andrew Li
We can see that this is simply a shortest path question with the added constraint that the hull damage must not exceed a certain amount.
We can still use Dijkstra's algorithm, except the distance array needs another dimension. Instead of containing the shortest distance from the source to node , we will instead define to be the minimum distance from the source to node while sustaining exactly cm of damage.
With this in mind, we need to alter Dijkstra's algorithm. Normally, after popping a node from the priority queue, you would loop through all adjacent nodes, updating the shortest path to each adjacent node. For this question, we must consider all adjacent nodes, but also all possible amounts of damage we have currently sustained on the hull. To update an adjacent node, see the following example. Assume you are at node , and there exists an edge to node with length 3 that deals 4 damage to your hull. Then, for all .
Then, we can simply loop through all possible amounts of damage less than , and take the best distance to node given damage .
Note that I use a boolean array called inq here to store whether a node is already in the priority queue. This saves a lot of time as it prevents numerous copies of the same node from being stored in the queue at once.
Thus, the time complexity is the same as for Dijkstra's, except for the factor of from considering all damages at each node. The time complexity is , which will run in time.
This is a multigraph, and according to a wise computer science teacher, it cannot be solved except with 3-D dynamic programming. Okay, I give up! There is no way to solve this! Clearly, this was not designed for the average high school student!