Xtreme NP-hard Problem?!
Time limit5sMemory limit1024 MB
Find the minimum-weight simple path from vertex 1 to vertex n that uses exactly k edges, or report -1; with n, m, k up to 10^6 this is explicitly NP-hard.
- Level
Hard9 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Brute force
- Solved
- No attempts yet
Problem
Careful! This problem turned out to be NP-hard. No rule forbade setting an NP-hard problem, so it was left as it is.
You are given an undirected graph with vertices and edges. The vertices are numbered 1 to , the edges are numbered 1 to , and edge has weight .
Given a natural number , find the length of the shortest simple path that starts at vertex 1, ends at vertex , and uses exactly edges.
A simple path is a path that never visits the same vertex twice, and the length of a path is the sum of the weights of the edges on it.
Input
The first line contains three integers , , separated by spaces.
The -th of the next lines contains three integers , , separated by spaces. This means edge joins vertex and vertex and has weight .
The graph contains no loops and no multiple edges.
Output
Print the length of the shortest simple path that starts at vertex 1, ends at vertex , and uses exactly edges. If no such path exists, print -1.