All submissions for this problem are available.
Johnny has gone on a short excursion to the island of Bitland, and found himself with plenty of time to spare for sight-seeing. As a matter of fact, he does really have any itinerary at hand. All he would like to do is to walk around a bit, without spending too much money in the process.
The cities of Bitland are connected by a network of one way streets. If there exists a road leading directly from city A to city B, then there also exists a road directly from city B to city A. There maybe any number (possibly zero) of parallel roads between any two cities. However, it is possible to reach any city starting from any other city (possibly indirectly, going through other cities on the way).
Now, the trouble is that all of the roads in Bitland are privately owned, and toll trolls have been hired to guard them and collect the dues. What's worse, the trolls are moody and will in some circumstances change the sum of the toll charged for using a road...
Johnny starts his sight-seeing tour from the capital of Bitland. He always leaves each city by the cheapest outgoing road, i.e. the one which currently has the lowest toll. However, as soon as Johnny has used the road from A to B, the toll troll in charge of it increases the toll for the road, setting it at exactly 1 Bitlandian dinar more than the toll currently being charged on the most expensive of the roads leaving city A.
Johnny has been walking around the towns of Bitland for a while, and he is getting a little bored. Help him answer the following question: what is the toll he is going to pay when traversing the k-th road of his tour?
The first line of input contains two integers: n, the number of towns in Bitland and m, the number of pairs of roads connecting them (1 <= n <= 105, 0<= m <=105). Each of the m following lines contains four integers A B cAB cBA, denoting the identifier of city A, the identifier of city B, the initial toll on the road from A to B, and the initial toll on the road from B to A (1 <= A < B<= n, 1 <= cAB, cBA <= 109). All tolls are expressed in dinars. You may assume that the initial toll values for all of the roads are different, and that the city labeled 1 is the capital of Bitland.
The next line of input contains the number t of queries asked by Johnny, t <= 104. Each of the following t lines contains a single integer k (1 <= k <= 1012).
For each of the values of k given at input, print a line containing the toll paid by Johnny when traversing the k-th road along his route.
Input: 3 2 1 2 5 10 1 3 11 7 4 1 2 3 5 Output: 5 10 11 12
|Time Limit:||0.1 - 0.208696 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, SQL, kotlin, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.