2026-06-16 space route optimization

space route optimization

the galactic trade federation needs to transport essential supplies across a network of star systems. there are N space stations, numbered 1 to N, connected by M bidirectional hyperlanes. each hyperlane connects two stations and takes a certain amount of fuel to traverse.

however, space pirates are active! each station i has a danger level Di. the federation wants to send a cargo ship from station 1 to station N. to minimize risk, the captain refuses to visit any station where the danger level is strictly greater than a threshold X.

your task is to find the minimum fuel cost to travel from station 1 to station N, such that the maximum danger level of any station visited on the path is at most X. if no such path exists for a given X, output -1.

because the Federation is testing multiple shipping routes under different pirate threat levels, you must answer Q independent queries, each with a different value of X.

input format



output format

for each of the Q series, print a single integer on a new line: the minimum fuel cost required, or -1 if station N is unreachable under the danger constraint.

constraints



back