Walking Plan
Time limit2sMemory limit512 MB
For each query, find the minimum total length of a walk from s to t that uses at least k edges in a directed weighted graph.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
There are intersections in Bytetown, connected by one-way streets. The intersections are labeled . Little Q likes sport walking very much, and he plans to walk for days. On the -th day, Little Q plans to start walking at the -th intersection, move along a street at least times, and finally arrive at the -th intersection. Here is the required number of moves, not streets: the same street may be used more than once.
Little Q's smartphone records his walking route. Little Q cares more about statistics than about staying healthy. So he wants to minimize the total walking length on each day. Write a program to help him find the best route.
Input
The first line contains a single integer (), the number of test cases. For each test case:
The first line contains two integers and (, ), the number of intersections and one-way streets.
Each of the next lines contains three integers , , (, , ), denoting a one-way street from intersection to intersection with length .
The next line contains an integer (), the number of days.
Each of the next lines contains three integers , , (, ), describing the walking plan.
Output
For each walking plan, print one line containing a single integer: the minimum total walking length. If there is no solution, print "-1".