This page is still under construction.

Parts of this page are still being built. What you see may change.

Cow Jogging

Interview

Time limit1sMemory limit128 MB

Summary
Given a DAG with edges pointing from higher to lower numbered nodes, report the K shortest path lengths from node N to node 1, with duplicates allowed.
Level

Medium7 of 10

Topics
Graph, Dynamic programming, Shortest path, Heap
Solved
No attempts yet

Problem

Bessie has taken heed of the evils of sloth and decided to get fit by jogging from the barn to the pond several times a week. To avoid working too hard, she only jogs downhill to the pond and then ambles back to the barn at her leisure.

The pastures are numbered 11 through NN (1≤N≤1,0001 \le N \le 1{,}000). Whenever X>YX > Y, the cow path from pasture XX to pasture YY runs downhill. Pasture NN is the barn at the top of the hill, and pasture 11 is the pond at the bottom.

Tired of always taking the same route, Bessie wants variety: she would like to know the lengths of the KK shortest routes from the barn to the pond (1≤K≤1001 \le K \le 100). Two routes are considered different if they consist of a different sequence of cow paths.

You are given MM downhill cow paths (1≤M≤10,0001 \le M \le 10{,}000). Cow path ii goes from pasture XiX_i to pasture YiY_i (1≤Yi<Xi≤N1 \le Y_i < X_i \le N) and has length DiD_i (1≤Di≤1,000,0001 \le D_i \le 1{,}000{,}000).

Input

  • Line 1: three space-separated integers NN, MM, and KK.
  • Lines 2 through M+1M+1: each line describes one downhill cow path with three space-separated integers XiX_i, YiY_i, and DiD_i.

Output

  • Print KK lines. Line ii contains the length of the ii-th shortest route, or −1-1 if no such route exists. If a shortest-route length occurs multiple times, print it that many times.

Hint

For the graph with N=5N = 5, the routes from the barn (pasture 55) to the pond (pasture 11) are 5→15 \to 1, 5→3→15 \to 3 \to 1, 5→2→15 \to 2 \to 1, 5→3→2→15 \to 3 \to 2 \to 1, 5→4→3→15 \to 4 \to 3 \to 1, and 5→4→3→2→15 \to 4 \to 3 \to 2 \to 1, with lengths 1,2,2,3,6,71, 2, 2, 3, 6, 7 respectively.

Examples2

  1. Example 1

    Input
    5 8 7
    5 4 1
    5 3 1
    5 2 1
    5 1 1
    4 3 4
    3 1 1
    3 2 1
    2 1 1
    
    Expected output
    1
    2
    2
    3
    6
    7
    -1
    
  2. Example 2

    Input
    2 1 1
    2 1 5
    
    Expected output
    5