Full Tank?

Time limit1sMemory limit128 MB

Summary
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 nn and the number of roads mm (1≤n≤10001 \le n \le 1000, 0≤m≤100000 \le m \le 10000).

The next line contains nn integers pip_i (1≤pi≤1001 \le p_i \le 100), where pip_i is the fuel price in city ii. Cities are numbered from 00 to n−1n-1.

Then follow mm lines, each with three integers uu, vv, and dd (0≤u,v<n0 \le u, v < n, 1≤d≤1001 \le d \le 100), telling that there is a road between cities uu and vv with length dd.

Then comes a line with the number of queries qq (1≤q≤1001 \le q \le 100), followed by qq lines each with three integers cc, ss, and ee (1≤c≤1001 \le c \le 100), where cc is the fuel-tank capacity of the vehicle, ss is the starting city, and ee is the goal city.

Output

For each query, output the price of the cheapest trip from city ss to city ee using a car with the given capacity, or impossible if there is no way of getting from ss to ee with that car.

Examples3

  1. Example 1

    Input
    5 5
    10 10 20 12 13
    0 1 9
    0 2 8
    1 2 1
    1 3 11
    2 3 7
    2
    10 0 3
    20 1 4
    
    Expected output
    170
    impossible
    
  2. Example 2

    Input
    2 1
    5 100
    0 1 3
    3
    5 0 1
    2 0 1
    5 1 0
    
    Expected output
    15
    impossible
    300
    
  3. Example 3

    Input
    3 0
    1 2 3
    2
    5 0 0
    5 0 1
    
    Expected output
    0
    impossible