Water Slides

Time limit1sMemory limit128 MB

Summary
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 EE mini slides connecting VV small pools, conveniently labeled 11 through VV. Every mini slide must be ridden in its proper direction and can never be traversed backwards. The cows start at pool 11 and ride successive mini slides until they reach pool VV, the final pool. Every pool except pool 11 has at least one mini slide entering it, and every pool except pool VV has at least one mini slide leaving it.

Moreover, from any pool it is possible to reach pool VV 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 ii runs from pool PiP_i to pool QiQ_i (Pi≠QiP_i \ne Q_i) and has a fun value FiF_i. 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 KK 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 11.

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: 1≤E≤150,0001 \le E \le 150{,}000, 2≤V≤50,0002 \le V \le 50{,}000, 1≤Pi≤V1 \le P_i \le V, 1≤Qi≤V1 \le Q_i \le V, 0≤Fi≤2,000,000,0000 \le F_i \le 2{,}000{,}000{,}000, 1≤K≤101 \le K \le 10.

For example, consider a small park with 33 pools (pool numbers shown in brackets) and 44 mini slides. Here K=1K = 1, and each slide's fun value is shown outside the brackets:

          [1]
         /   \
   5 -> /     \ <- 9
       /       \
     [2]---3---[3]
        \__5__/

Bessie always starts at pool 11 and finishes at pool 33. If she had her way, she would ride from pool 11 to pool 22 and then take the higher-fun slide (fun value 55) to pool 33, for a total of 5+5=105 + 5 = 10. But if she loses control at pool 11, she might slide straight from pool 11 to pool 33 for a total fun of 99. If she loses control at pool 22, her total fun could drop to 5+3=85 + 3 = 8.

Because Bessie wants to guarantee as much fun as possible, she chooses to ride straight from pool 11 to pool 33 for a total of 99. Even if she loses control at pool 11 and is sent down the 1→21 \to 2 slide, she has no losses of control left, so she will not lose control at pool 22 and will end up with fun 1010. Thus she knows her guaranteed fun is always at least 99.

Input

  • Line 1: three space-separated integers VV, EE, and KK.
  • Lines 22 through E+1E + 1: line i+1i + 1 contains three space-separated integers PiP_i, QiQ_i, and FiF_i.

Output

  • A single line with one integer: the minimum fun Bessie can guarantee.

Examples2

  1. Example 1

    Input
    3 4 1
    2 3 5
    1 2 5
    1 3 9
    2 3 3
    
    Expected output
    9
    
  2. Example 2

    Input
    3 3 0
    1 2 10
    1 3 1
    2 3 10
    
    Expected output
    20