#C6344. Minimum Cost Path in a Grid

    ID: 50094 Type: Default 1000ms 256MiB

Minimum Cost Path in a Grid

Minimum Cost Path in a Grid

You are given a grid of positive integers with dimensions \(N \times M\). Your task is to find the minimum cost path from the top-left corner (cell \((1,1)\)) to the bottom-right corner (cell \((N,M)\)). You are only allowed to move either right or down at any point in time.

The cost of a path is the sum of the values of the cells visited along the path, including the start and end cells.

Input constraints:

  • The first line contains two integers, \(N\) and \(M\), representing the number of rows and columns of the grid.
  • The following \(N\) lines each contain \(M\) integers separated by spaces representing the grid.

Output: Output a single integer representing the minimum cost to traverse from the top-left cell to the bottom-right cell.

Note: All input values are positive integers.

inputFormat

The input is read from stdin and has the following format:

N M
row1_element1 row1_element2 ... row1_elementM
row2_element1 row2_element2 ... row2_elementM
...
rowN_element1 rowN_element2 ... rowN_elementM

For example:

3 3
1 3 1
1 5 1
4 2 1

outputFormat

The output is written to stdout and should be a single integer representing the minimum cost to get from the top-left corner to the bottom-right corner.

7
## sample
3 3
1 3 1
1 5 1
4 2 1
7