Landlord
Time limit2sMemory limit512 MB
Find the minimum travel time to visit a fixed sequence of districts, starting at 0, when cars parked in some districts can be used once each for faster driving.
- Level
Hard8 of 10
- Topics
- Shortest path, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
Kangho is the landlord of a city split into districts. The districts are numbered 0 through , and they are connected by two way roads.
Kangho owns cars, and each car is parked in one district. Several cars may be parked in the same district.
Today is rent collection day. Kangho walks his buildings himself and collects the rent. There are buildings to collect from, and the visiting order is fixed in advance as . He collects rent in district first, then in district , and finally in district . Kangho starts in district 0.
There are two ways to move: walking and driving. Crossing a road of length on foot takes time, and crossing it by car takes time.
When Kangho reaches a district where one of his cars is parked, he can get into that car and drive wherever he wants. Once he gets out of a car, he never gets into that car again. He must also be out of the car when he collects rent.
Given the road information, write a program that computes the minimum time needed to collect all the rent.
Input
The first line contains the number of districts , the number of parked cars , the number of buildings , the walking time per unit of road length, and the driving time per unit of road length. (, )
The second line contains the districts where the cars are parked, separated by spaces.
The third line contains the collection order , separated by spaces. ()
Each of the next lines contains the road information of the city as an adjacency matrix. The -th number on the -th line describes the road between district and district . A 0 means there is no road, and a positive integer is the length of that road. Road lengths are positive integers no greater than 62.
Roads are two way, so the -th number on the -th line always equals the -th number on the -th line, and the -th number on the -th line is always 0. Every district can be reached from every other district.
Output
Print the minimum time needed to collect all the rent on the first line.
Hint
In the first example, Kangho walks from district 0 to district 2 and collects rent. He then walks from district 2 to district 3 and collects rent again. Next he walks to district 1, takes the car to district 0, and collects rent there. The total time is .
In the second example, Kangho walks from district 0 to district 1. He takes the car there and drives to district 2, gets out, and collects rent. Then he walks from district 2 through district 1 to district 0 and collects rent. The total time is .