Ray Needs Help

View as PDF

Submit solution


Points: 20 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem types

Ray is planning something and needs your help. Ray needs your help to answer Q queries on a directed, weighted graph of N nodes and M edges.

For the ith query he wants to compute the minimum weight of a walk from ui to vi that takes exactly ki edges, and if no such walk exists, output -1 instead. A walk is similar to a path, but edges (and nodes) can be traversed multiple times. The weight of a walk is equal to the sum of weights of the edges it traverses, and if an edge is traversed multiple times, its weight will count multiple times towards the sum.

Constraints

1N100

1MN2

1Q2000

There may be duplicate edges and/or self-loops.

1ui,vi,ai,biN

1wi,ki109

Subtask 1 [25%]

Q=1

ki100

Subtask 2 [35%]

Q=1

Subtask 3 [40%]

No additional constraints.

Input Specification

The first line contains the integers N, M, and Q.

The next M lines each contain the integers ai,bi,wi, meaning that there is a directed edge from ai to bi with a weight of wi. Note that it may be possible for a single pair of nodes to have multiple edges between them.

The next Q lines each contain a query in the form ui,vi,ki.

Output Specification

For each query ui,vi,ki, output the shortest walk from ui to vi that uses ki edges. If no such walk exists, output -1 instead. The output of each query should be on a separate line.

Sample Input

Copy
5 7 7
2 1 1
1 2 3
1 4 10
4 5 1
3 4 1
2 3 4
3 5 5
2 4 2
2 4 6
2 4 7
2 5 3
3 5 1
3 5 2
3 5 3

Sample Output

Copy
5
13
-1
6
5
2
-1

Sample Explanation

Here are the answers to each query:

  • 2 4 2: 234
  • 2 4 6: 2121234
  • 2 4 7: It can be shown that no such walk exists.
  • 2 5 3: 2345
  • 3 5 1: 35
  • 3 5 2: 345
  • 3 5 3: It can be shown that no such walk exists.

Comments

There are no comments at the moment.