#C3100. Minimum Travel Cost in a Grid

    ID: 46491 Type: Default 1000ms 256MiB

Minimum Travel Cost in a Grid

Minimum Travel Cost in a Grid

You are given a grid with H rows and W columns. Each cell in the grid contains a cost. You start at the top-left cell (0, 0) and want to travel to the bottom-right cell (H-1, W-1). You can move to adjacent cells in four directions (up, down, left, right).

Your task is to compute the minimum total cost to reach the destination. The cost of a path is defined as the sum of the costs of all the cells visited (including both the start and the destination cells). Formally, if you denote the cost matrix by \(cost\), you need to find the minimum cost path from \( (0, 0) \) to \( (H-1, W-1) \).

Note: Movement is allowed in four directions. Use an appropriate algorithm such as Dijkstra's algorithm to solve the problem efficiently.

inputFormat

The input is given via standard input (stdin) in the following format:

  • The first line contains two integers H and W separated by a space, representing the number of rows and columns of the grid.
  • The next H lines each contain W space-separated integers, where the j-th integer in the i-th line represents cost[i][j].

outputFormat

Output a single integer to standard output (stdout) — the minimum total cost to travel from the top-left cell to the bottom-right cell.

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