#P7294. [USACO21JAN] Minimum Cost Paths P
[USACO21JAN] Minimum Cost Paths P
Description
Farmer John’s pasture can be viewed as a 2D grid made of an () array of square cells (imagine a huge chessboard). For , the cell in the -th row from top to bottom and the -th column from left to right is denoted by . In addition, for each , column has a cost ().
Bessie starts at cell . If she is currently at cell , she can perform one of the following actions:
- If , Bessie can move to the next column (increase by ) at a cost of .
- If , Bessie can move to the next row (increase by ) at a cost of .
Given () independent queries, each query gives (). Compute the minimum total cost for Bessie to move from to .
Input Format
The first line contains and .
The second line contains space-separated integers .
The third line contains .
The last lines each contain two space-separated integers and .
Output Format
Output lines, one for each query, containing the answer.
Note that the integer size used in this problem may require 64-bit integers (for example, long long in C/C++).
5 4
1 100 100 20
20
1 1
2 1
3 1
4 1
5 1
1 2
2 2
3 2
4 2
5 2
1 3
2 3
3 3
4 3
5 3
1 4
2 4
3 4
4 4
5 4
0
1
2
3
4
1
5
11
19
29
2
9
20
35
54
3
13
29
49
69
Hint
Explanation for Sample 1
The output, shown in grid form, is as follows:
1 2 3 4
*--*--*--*--*
1 | 0| 1| 2| 3|
*--*--*--*--*
2 | 1| 5| 9|13|
*--*--*--*--*
3 | 2|11|20|29|
*--*--*--*--*
4 | 3|19|35|49|
*--*--*--*--*
5 | 4|29|54|69|
*--*--*--*--*
Testdata Properties
- Testdata 1-3 satisfy .
- Testdata 4-8 satisfy .
- Testdata 9-15 satisfy .
- Testdata 16-20 have no additional constraints.
Problem by: Benjamin Qi.
Translated by ChatGPT 5
京公网安备 11011102002149号