#P4046. [JSOI2010] 快递服务

    ID: 2986 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划,dp2010各省省选江苏枚举,暴力

[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 mm points (1,2,,m1, 2, \dots, m). Therefore, “Feiben” purchased three trucks and hired three drivers, who set out every morning from pickup locations 1,2,31, 2, 3 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 11 starts at pickup location 11, he cannot first pick up the mail for the fourth registered company (location 11) and then go to the first, second, or third registered pickup locations (locations 4,2,44, 2, 4). However, if the first three registered pickups are handled by driver 22 or 33, then driver 11 can pick up the fourth registered mail at location 11 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 m×mm \times m matrix DD. We define the cost of a sequence a(a0,a1,a2,,an)a(a_0, a_1, a_2, \dots, a_n) as i=1nDai1,ai\sum\limits_{i=1}^{n} D_{a_{i-1}, a_i}.

Now you are given a sequence ss with length 1000\leq 1000. Split it into three subsequences a,b,ca, b, c, with a0=1,b0=2,c0=3a_0 = 1, b_0 = 2, c_0 = 3. Among all such partitions, minimize the sum of their costs.

In particular, the matrix DD 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 mm, the number of pickup locations, identified by integers from 11 to mm.

The next mm lines (lines 22 to m+1m+1) each contain mm integers, representing a matrix DD. On the ii-th line, the jj-th integer D(i,j)D(i, j) is the fuel required to drive from pickup point ii to pickup point jj. 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 10001000 pickup requests.

Any two adjacent integers in the input are separated by one space.

Note: The fuel matrix DD 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 11 only needs to move from starting point 11 to location 33, driver 22 can stay at location 22, and driver 33 moves from starting point 33 to location 44.

Constraints: 3m200,1sim3 \leq m \leq 200, 1 \leq s_i \leq m.

Translated by ChatGPT 5