Water Slides
Time limit1sMemory limit128 MB
On a DAG where each node leading to the sink, Bessie maximizes her worst-case path sum when up to K times she is forced down the worst outgoing edge.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Graph, Sorting
- Solved
- No attempts yet
Problem
Inspired by the new water park at Machu Picchu in Peru, Farmer John has decided to build one for his cows. Its biggest attraction is a giant water slide of a peculiar design.
The superslide consists of mini slides connecting small pools, conveniently labeled through . Every mini slide must be ridden in its proper direction and can never be traversed backwards. The cows start at pool and ride successive mini slides until they reach pool , the final pool. Every pool except pool has at least one mini slide entering it, and every pool except pool has at least one mini slide leaving it.
Moreover, from any pool it is possible to reach pool by riding some sequence of mini slides. Finally, because this is a slide, once you leave a pool you can never return to it, no matter which mini slides you ride afterwards.
Each mini slide runs from pool to pool () and has a fun value . Bessie's total fun on any trip down the superslide is the sum of the fun values of all the mini slides she rides.
Naturally Bessie wants to have as much fun as possible, so at each pool she normally chooses carefully which mini slide to take. However, she is a cow: at most times during her descent she loses control and is forced down an arbitrary mini slide leaving a pool — "arbitrary" meaning the worst possible one for her. This can even happen at pool .
If Bessie plays so as to maximize her fun in the worst case, how much fun is she guaranteed to have on the given superslide?
Constraints: , , , , , .
For example, consider a small park with pools (pool numbers shown in brackets) and mini slides. Here , and each slide's fun value is shown outside the brackets:
[1]
/ \
5 -> / \ <- 9
/ \
[2]---3---[3]
\__5__/
Bessie always starts at pool and finishes at pool . If she had her way, she would ride from pool to pool and then take the higher-fun slide (fun value ) to pool , for a total of . But if she loses control at pool , she might slide straight from pool to pool for a total fun of . If she loses control at pool , her total fun could drop to .
Because Bessie wants to guarantee as much fun as possible, she chooses to ride straight from pool to pool for a total of . Even if she loses control at pool and is sent down the slide, she has no losses of control left, so she will not lose control at pool and will end up with fun . Thus she knows her guaranteed fun is always at least .
Input
- Line 1: three space-separated integers , , and .
- Lines through : line contains three space-separated integers , , and .
Output
- A single line with one integer: the minimum fun Bessie can guarantee.