#BF0000004. Đường đi ngắn nhất tối đa K cạnh (Shortest Path with K Edges)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đườ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 vertices and weighted edges, possibly including negative weights. Find a minimum-cost route from vertex to vertex using at most 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 counts edges, not intermediate vertices.
Input
First line: . Next lines: .
Output
Print the minimum cost or impossible if no valid route exists.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input
3 3 1
1 2 1
2 3 1
1 3 3
Output
3
Explanation
With , only one edge is permitted. The two-edge route costing 2 is invalid; the direct flight costs 3.