Voting Cities
Time limit1sMemory limit1024 MB
For each start city and ticket prices, find the cheapest road path to a voting city, where each of five discount tickets can be used at most once.
- Level
Hard8 of 10
- Topics
- Shortest path, Bit manipulation, Graph
- Solved
- No attempts yet
Problem
The great Emperor, Lord Pooty, has decided to retire and hand the crown to one of his many sons. In the spirit of democracy, he will settle the succession with a vote.
The kingdom has cities numbered from to . Of these, are voting cities, and the th voting city is .
As a responsible citizen, you will travel to one of the designated voting cities to cast your vote. There are roads. Road goes from city to city in one direction and has toll . Local cities have opened a ticket system to reduce travel costs.
There are 5 ticket types, numbered 1 to 5. A ticket of type reduces the toll of a road by , so the toll is multiplied by when that ticket is used.
Ticket rules: you cannot use more than one ticket on a single road. You may buy at most one of each type at the start of your journey. For example, you may buy one type 1 ticket and one type 2 ticket, but not two type 2 tickets. This prevents hoarding. Tickets can only be bought at the start of the journey.
You are busy and do not know in advance which city you start from or the ticket prices. You have a list of situations, each with a starting city and prices through for the five tickets. A ticket may be unavailable, in which case its price is .
For each situation, find the minimum cost to reach a voting city, if one is reachable by road. Not every city can reach every other city, so you may have to walk.
Input
The first line has three integers , , and : the number of cities, roads, and voting cities. The second line has integers, where the th is , the th voting city.
Each of the next lines has three integers , , , describing a one-way road from to with toll .
The next line has , the number of situations. Each of the next lines has six integers , , , , , : the starting city and the prices of tickets 1 through 5.
The program reads from standard input.
Output
Print lines, one integer per line. Each integer is the lowest cost to a voting city for the corresponding situation, in input order. Print if no voting city is reachable from the starting city.
The program writes to standard output.
Constraints
- for all
- for all
- for all
- is a multiple of for all
- and for all
- for all