#P4768. Minimize Walking Distance in Magic City
Minimize Walking Distance in Magic City
Minimize Walking Distance in Magic City
In the magical city where rain is frequent, the urban area is modeled as an undirected connected graph with nodes and edges. Each edge is described by its length and altitude . Due to heavy seasonal rains, roads accumulate water. The drainage system makes sure that the flooded roads are exactly those with the lowest altitudes: when the water level is set to , every road whose altitude is flooded. Cars cannot travel through flooded roads, but pedestrians can go along any road.\n\nYazid, a local resident and competitor, has his home at node . Over days, on each day he provides a starting node and a water level . Starting at , he is provided with a car which can drive only on non-flooded (safe) edges (i.e. edges with altitude ). At any node he may choose to abandon the car and complete his journey on foot (which can use any road). However, once he leaves the car, he must continue walking, and the car he left behind will not be available later (cars are reset on each day).\n\nHelp Yazid minimize the total walking distance—the sum of the lengths of the edges he must walk on—on his journey to his home. Equivalently, let be the shortest walking distance (using any roads) from node to node . On a given day with starting node and water level , let be the set of nodes reachable from using roads with altitude (i.e. via car). The answer is ( \min_{u \in S} d(u) ).\n\nNote that the graph is connected (by all roads) and the car is available fresh each day.
inputFormat
The first line contains two integers and , representing the number of nodes and edges.\n\nThe next lines each contain four integers , , , and , describing an edge between nodes and with length and altitude .\n\nThe following line contains an integer , the number of queries.\n\nEach of the next lines contains two integers and , which are the starting node and the water level for that day.
outputFormat
For each query, output a single integer representing the minimum total walking distance from the starting node (given the water level ) to home at node .
sample
4 4
1 2 3 5
2 3 2 3
3 4 4 6
2 4 1 2
2
3 4
4 2
4
0
</p>