PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000004. Đường đi ngắn nhất tối đa K cạnh (Shortest Path with K Edges)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đường đi ngắn nhất tối đa K cạnh (Shortest Path with K Edges)

Shortest Path with K Edges

Source: AcWing

Version: Phuoc Hung OJ Extended

Problem Statement

Find minimum path cost from 1 to n using at most k edges, even with negative cycles.

A directed graph has nn vertices and mm weighted edges, possibly including negative weights. Find a minimum-cost route from vertex 11 to vertex nn using at most kk edges.

Each edge traversal counts separately, including repeated traversal of an edge. The finite edge budget prevents any route from circling a negative cycle indefinitely. Consider only routes satisfying the edge budget, and print impossible if no such route exists. Here kk counts edges, not intermediate vertices.

Input

First line: n,m,kn,m,k. Next mm lines: u,v,wu,v,w.

Output

Print the minimum cost or impossible if no valid route exists.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 1≤m≤501\le m\le50; 1≤k≤201\le k\le20; −10000≤w≤10000-10000\le w\le10000.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le150; 1≤m≤5001\le m\le500; 1≤k≤1501\le k\le150; −10000≤w≤10000-10000\le w\le10000.
  • Subtask 3 (50 points): 1≤n≤5001\le n\le500; 1≤m≤100001\le m\le10000; 1≤k≤5001\le k\le500; −10000≤w≤10000-10000\le w\le10000.

Examples

Input

3 3 1
1 2 1
2 3 1
1 3 3

Output

3

Explanation

With k=1k=1, only one edge is permitted. The two-edge route costing 2 is invalid; the direct flight costs 3.