Akuna CapitalOA #24
Minimum Cost to Move Within a Grid (Akuna Shang Hai )
Estimated EasyJudge-ready
Topics (AI-labelled)Prefix & RangesIn-place Arrays
In a rectangular grid you can only move up, down, left or right. Crossing between rows i and i+1 in any column costs costRows[i]; crossing between columns j and j+1 in any row costs costCols[j]. Find the minimum total cost from the start to the destination. The statement gives no obstacle positions, so this site interprets the whole grid as passable.
Input
The first line contains rows cols initR initC finalR finalC, with 0-based coordinates; the second line contains rows−1 costs between rows; the third line contains cols−1 costs between columns. Original: 1≤rows,cols≤100000, coordinates are valid, and 0≤(cost of each edge)≤10000.
Output
Output the minimum movement cost.
Example 1
Input:
3 3 0 0 1 2
5 2
6 1
Output:
12
Example 2
Input:
4 4 1 2 3 3
1 2 3
7 8 9
Output:
14
Example 3
Input:
1 1 0 0 0 0
Output:
0
Editorial
Loading solution…