QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 100

#12985. Connections

統計

Byteotian Ministry of Infrastructure has decided to create a computer program that helps to find quickly the lengths of routes between arbitrary towns. It would be small wonder if the inhabitants of Byteotia always wanted to find the shortest route. However, it happens that they want to know the $k$-th shortest route. Moreover, cycles in routes are possible, i.e. routes that have recurring towns.

If there are 4 routes between two towns and their lengths are 2, 4, 4 and 5, then the length of the shortest connection is 2, the second shortest is 4, the third is 4, and the fourth is 5.

Task

Write a program which:

  • reads from the standard input a description of Byteotian road network and queries concerning lengths of journey routes,
  • computes and writes to the standard output answers to the queries read.

Input

In the first line of the standard input there are two positive integers n and m, separated by a single space, $1 \le n \le 100$, $0 \le m \le n^2-n$. They are the number of towns in Byteotia and the number of roads connecting the towns, respectively. The towns are numbered from $1$ to $n$.

In each of $m$ successive lines there are three integers separated by single spaces: $a$, $b$ and $l$, $a \ne b$, $1 \le l \le 500$. Each triple describes one one-way road of length $l$ enabling to move from the town $a$ to $b$. For each two towns there exist at most one road that enables to move in the given direction.

In the following line there is one integer $q$, $1 \le q \le 10\,000$, denoting the number of queries. In the successive $q$ lines there are queries written, one per line. Each query has a form of three integers separated by single spaces: $c$, $d$ and $k$, $1 \le k \le 100$. Such a query refers to the length of the $k$-th shortest route from the town $c$ to the town $d$.

Output

Your program should write to the standard output the answers to the queries read, one answer per line. In the $i$-th line the answer to the $i$-th query should be written: one integer equal to the length of the route being sought or -1, when such a route does not exist.

Example

Input

5 5
1 2 3
2 3 2
3 2 1
1 3 10
1 4 1
8
1 3 1
1 3 2
1 3 3
1 4 2
2 5 1
2 2 1
2 2 2
1 1 2

Output

5
8
10
-1
-1
3
6
-1
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.