#P4046. [JSOI2010] 快递服务
[JSOI2010] 快递服务
Description
After the “Feiben” (pinyin) courier company was founded, it signed mail pickup-and-delivery service contracts with many small and medium-sized companies in the city. Since some companies are in the same building, the pickup locations (pickup points) that “Feiben” needs to visit are at most points (). Therefore, “Feiben” purchased three trucks and hired three drivers, who set out every morning from pickup locations respectively. The service contract with clients clearly states that “Feiben” must send someone to the client’s company (location) to pick up the mail on the day after the client submits the mailing request.
To serve clients more efficiently and save pickup time, the company set up a pickup service registration website. If a client needs to send mail, they must register online one day in advance. To save fuel, “Feiben” plans the next day’s pickup routes for the three drivers at night. The order in which each driver visits their assigned locations must respect the online registration order, and all pickups must be completed using the least fuel. Thus, a driver may need to visit the same location multiple times at different times, and different drivers may also go to the same location at different times.
As shown in Example 2 below (the pickup locations in order are: 4 2 4 1 5 4 3 2 1), although driver starts at pickup location , he cannot first pick up the mail for the fourth registered company (location ) and then go to the first, second, or third registered pickup locations (locations ). However, if the first three registered pickups are handled by driver or , then driver can pick up the fourth registered mail at location and then proceed to later registered locations. In some cases, not every truck needs to pick up mail; the optimal plan may use only one or two trucks. Please write a program to compute the minimum total fuel consumption to visit all pickup locations in the registered order.
Brief statement: Given an matrix . We define the cost of a sequence as .
Now you are given a sequence with length . Split it into three subsequences , with . Among all such partitions, minimize the sum of their costs.
In particular, the matrix satisfies the triangle inequality; see the detailed explanation in the Input Format.
(By El_destructor.)
Input Format
The first line of the input file contains an integer , the number of pickup locations, identified by integers from to .
The next lines (lines to ) each contain integers, representing a matrix . On the -th line, the -th integer is the fuel required to drive from pickup point to pickup point . The last line is a sequence of location IDs in the order of the requests registered online on the previous day; there are at most pickup requests.
Any two adjacent integers in the input are separated by one space.
Note: The fuel matrix satisfies the triangle inequality, that is, $\forall 1 \leq i, j, k \leq m, D(i, j) \leq D(i, k) + D(k, j)$. Therefore, when a truck goes to the next assigned pickup location, it always goes directly without detouring through other locations.
Output Format
Output a single integer, the minimum total fuel required for all pickups.
4
0 5 0 6
6 0 5 6
1 6 0 6
1 1 1 0
1 1 1 1 4 4 2 2 2 3
6
Hint
Sample explanation:
The drivers assigned to each request, in order, are 1 1 1 1 3 3 2 2 2 1. Therefore, driver only needs to move from starting point to location , driver can stay at location , and driver moves from starting point to location .
Constraints: .
Translated by ChatGPT 5
京公网安备 11011102002149号