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.
Alternate Solution
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!
Comments
From the Sample Input 1,
particulary, line 3 and 4:
Can we make a conclusion that this is a directional map, instead of an undirectional map?
Thank you ...
You may find it useful to reread the statement. In particular:
"... the
route runs directly between two different islands
and
; (
), takes
minutes to travel along in either direction, and has rocks that wear down the ship's hull by
centimetres. There may be multiple routes running between a pair of islands."
Why do you set inq[spot] = False every time you pop from the queue?
When I submitted the solution with the boolean array to prevent duplicates in the queue, it got 12/15 (WA). Then I submitted removing the boolean array, and got 14/15...not sure why.
Also, why do you sort the priority queue from greatest to least? Shouldn't it be the other way around?
Usually when you use a priority queue for dijkstras you put in negative values for distance so that it sorts from greatest to least. He didn't put in negative values so he instead sorted the priority queue from greatest to least.
If anyone is interested! Also, thanks to kobortor for being awesome.
Editorial
Sadly BFS does pass, and all the additional structs aren't necessary
why sadly, since its so fast isn't it the intended solution?
Edit: NVM I'm blind, I just like to use structs to organize things in my code.