Shortest Path Queries
Time limit5sMemory limit512 MB
Given a W by H grid of cell costs, the task answers Q queries with the minimum-cost path sum between two cells, moving only to edge-adjacent cells.
- Level
Medium7 of 10
- Topics
- Divide and conquer, Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
You are given a grid. Each cell contains an integer. The top-left cell is called , and the bottom-right cell is called .
A path from a cell to a cell is a sequence of cells such that the first cell in the sequence is , the last cell is , and any two consecutive cells share an edge. The cost of a path is the sum of the costs of all cells in the path.
You are given the integers written in the grid and pairs of cells and . For each pair, compute the minimum cost of a path from cell to cell .
Input
The first line contains three integers , , and (, , ).
The next lines describe the grid. The -th number on the -th of these lines, , is the integer written in cell ().
The next lines each contain a pair of cells and (, , ).
Output
Print lines. On the -th line, print the minimum cost for the pair and .