Increasing Shortest Path
Time limit15sMemory limit256 MB
Find the cheapest A to B path using at most C edges whose weights strictly increase.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Shortest path, Graph, Sorting
- Solved
- No attempts yet
Problem
"Life is too short to make a story", said Ahmed Aly, so this problem gets straight to the point.
You are given a weighted directed graph with nodes numbered from to . Every edge weight is a positive integer, and all edge weights are distinct.
A query is three integers , , . Consider the paths that start at node , end at node , use at most edges, and whose edge weights grow along the path: every edge must weigh more than the edge taken just before it. The first edge of a path carries no such restriction.
For each query, find the smallest possible sum of edge weights over those paths.
Input
The first line contains one integer , the number of test cases ().
Each test case begins with a line holding three integers separated by a single space, , and (, , ): the number of nodes, edges and queries.
The next lines each contain three integers separated by a single space, , and (, , ), an edge going from node to node with weight . There might be multiple edges between the same pair of nodes.
The next lines each contain three integers separated by a single space, , and (, , ), one query as described above.
Output
For each query, print one line with the minimum sum of edge weights of a path that satisfies the constraints, or if no such path exists. Do not print blank lines between test cases.