#P6471. [COCI 2008/2009 #6] DOSTAVA
[COCI 2008/2009 #6] DOSTAVA
Description
In an grid matrix, a courier starts from the company at and needs to deliver pizzas to cells. Before leaving, he has already taken all the pizzas with him, so he will not waste time going back and forth.
At any moment, the courier can only move left or right, but if he is in column or column , he can move up or down. Each time he arrives at a cell, it costs the time assigned to that cell. (If he arrives multiple times, the cost is added multiple times.)
To be more efficient, he wants to know the minimum total time needed to finish deliveries to these delivery points.
Input Format
The first line contains two integers , representing the number of rows and columns of the matrix.
The next lines each contain integers, where each integer is the time cost to step onto that cell.
The next line contains an integer , representing the total number of delivery points that must be visited.
The next lines each contain two integers , meaning the courier must deliver to the cell at row and column .
Although the courier wants to minimize the time, he must deliver in the order given in the input, otherwise customers will be unhappy.
Output Format
Output one integer in one line, representing the minimum delivery time.
3 3
1 8 2
2 3 2
1 0 1
3
1 3
3 3
2 2
17
2 5
0 0 0 0 0
1 4 2 3 2
4
1 5
2 2
2 5
2 1
9
Hint
Explanation for Sample 1
The shortest delivery route is:
$(1,1),(2,1),(3, 1), (3, 2), (3, 3), (2, 3), (1, 3), (2, 3), (3, 3), (2, 3),(2, 2)$.
The total time is: .
Constraints
- For of the testdata, it is guaranteed that .
- For of the testdata, it is guaranteed that , , , , .
Notes
This problem is translated from COCI2008-2009 CONTEST #6 T5 DOSTAVA。
Translated by ChatGPT 5
京公网安备 11011102002149号