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.
Hard8Shortest pathDynamic programmingGraphNo attempts yetTime limit2sMemory limit512 MBKangho is the landlord of a city split into N districts. The districts are numbered 0 through N−1, and they are connected by two way roads.
Kangho owns C 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 M buildings to collect from, and the visiting order is fixed in advance as A0,A1,…,AM−1. He collects rent in district A0 first, then in district A1, and finally in district AM−1. Kangho starts in district 0.
There are two ways to move: walking and driving. Crossing a road of length L on foot takes W×L time, and crossing it by car takes D×L 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.
The first line contains the number of districts N, the number of parked cars C, the number of buildings M, the walking time W per unit of road length, and the driving time D per unit of road length. (1≤N,C,M≤50, 1≤D<W≤100)
The second line contains the C districts where the cars are parked, separated by spaces.
The third line contains the collection order A0,A1,…,AM−1, separated by spaces. (A0=0)
Each of the next N lines contains the road information of the city as an adjacency matrix. The j-th number on the i-th line describes the road between district i and district j. 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 j-th number on the i-th line always equals the i-th number on the j-th line, and the i-th number on the i-th line is always 0. Every district can be reached from every other district.
Print the minimum time needed to collect all the rent on the first line.
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 5×1+5×3+5×2+1×2+1×3+1×1=36.
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 2×37+1×38+2×38+2×37=262.