Problems > Exactly K Flights

Exactly K Flights

Difficulty: 2200 6000 ms 128 MB

graphs matrix-exponentiation

Print the cheapest walk from node 1 to node N using EXACTLY K directed edges (edges may repeat), or −1 if impossible.

Input: the first line has N, M and K; each of the next M lines has u v w.

Output: the minimum cost over walks of exactly K edges, or −1.

Constraints:

  • 2 ≤ N ≤ 100
  • 1 ≤ K ≤ 109
  • 1 ≤ w ≤ 106

Sample Tests

Input 1
3 3 2
1 2 1
2 3 1
1 3 5
Output 1
2
Input 2
2 1 2
1 2 3
Output 2
-1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.