Full Tank?
Time limit1sMemory limit128 MB
For each query (tank capacity c, start s, goal e), find the minimum fuel cost to drive from s to e, buying gas only at cities at given prices; output impossible if unreachable.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Heap
- Solved
- No attempts yet
Problem
After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you visited. Maybe you could have saved some money if you were a bit more clever about where you filled your fuel?
To help other tourists (and save money yourself next time), you want to write a program for finding the cheapest way to travel between cities, filling your tank on the way. We assume that all cars use one unit of fuel per unit of distance, and start with an empty gas tank.
Input
The first line contains the number of cities and the number of roads (, ).
The next line contains integers (), where is the fuel price in city . Cities are numbered from to .
Then follow lines, each with three integers , , and (, ), telling that there is a road between cities and with length .
Then comes a line with the number of queries (), followed by lines each with three integers , , and (), where is the fuel-tank capacity of the vehicle, is the starting city, and is the goal city.
Output
For each query, output the price of the cheapest trip from city to city using a car with the given capacity, or impossible if there is no way of getting from to with that car.