Find the cheapest path cost from multiple sources to one destination in a weighted grid

Quick Overview

Given a grid of non-negative cell costs, a list of source cells and one destination, return the cheapest four-direction path cost from each source to the destination, counting both end cells. It tests Dijkstra's algorithm on a grid and how to answer many source queries without repeating the search.

Find the cheapest path cost from multiple sources to one destination in a weighted grid

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an `m x n` grid where `grid[i][j]` is a non-negative integer: the cost of passing through cell `(i, j)`. You are also given a list of `sources` (for example, the positions of several drivers) and a single `destination` cell (for example, a rider). From any cell you may move up, down, left or right to an adjacent cell inside the grid. For each source, find the minimum total cost of a path from that source to the destination. Return these costs as a list in the same order as `sources`. ### Function Signature ```python def shortest_path_costs(grid: list[list[int]], sources: list[list[int]], destination: list[int]) -> list[int]: ``` ### Rules - Cells are given as `[row, col]` pairs, 0-indexed. - The cost of a path is the sum of the `grid` values of every cell on it, including the source cell and the destination cell. - When a source is the destination itself, its cost is the value of that one cell. - The output has one entry per element of `sources`, in input order. A source that appears twice gets two entries. - Return `-1` for a source that cannot reach the destination. Every cell can be entered, so this does not happen under the constraints below. ### Constraints - `1 <= m, n <= 100` - `0 <= grid[i][j] <= 10^4` - `1 <= len(sources) <= m * n` - Every source and the destination are valid cells of the grid. ### Examples ```text Input: grid = [[1,2,3],[4,5,1],[1,1,1]], sources = [[0,0],[2,0],[0,2]], destination = [2,2] Output: [8,3,5] ``` From (0,0), going along the top row and down the right column costs 1 + 2 + 3 + 1 + 1 = 8, and going down the left column and along the bottom row also costs 1 + 4 + 1 + 1 + 1 = 8. Any path through the 5 in the center costs at least 10. From (2,0) the bottom row costs 1 + 1 + 1 = 3, and from (0,2) the right column costs 3 + 1 + 1 = 5. ```text Input: grid = [[0,1],[1,0]], sources = [[0,0],[1,1]], destination = [1,1] Output: [1,0] ``` From (0,0) both routes cost 0 + 1 + 0 = 1. The second source is the destination, so its cost is `grid[1][1] = 0`. ```text Input: grid = [[5]], sources = [[0,0]], destination = [0,0] Output: [5] ``` The only source sits on the destination. The path is that single cell, and it costs its value, 5.

Overview: Given a grid of non-negative cell costs, a list of source cells and one destination, return the cheapest four-direction path cost from each source to the destination, counting both end cells. It tests Dijkstra's algorithm on a grid and how to answer many source queries without repeating the search.

Uber logo
Uber
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an m x n grid where grid[i][j] is a non-negative integer: the cost of passing through cell (i, j). You are also given a list of sources (for example, the positions of several drivers) and a single destination cell (for example, a rider). From any cell you may move up, down, left or right to an adjacent cell inside the grid.

For each source, find the minimum total cost of a path from that source to the destination. Return these costs as a list in the same order as sources.

Function Signature

def shortest_path_costs(grid: list[list[int]], sources: list[list[int]], destination: list[int]) -> list[int]:

Rules

  • Cells are given as [row, col] pairs, 0-indexed.
  • The cost of a path is the sum of the grid values of every cell on it, including the source cell and the destination cell.
  • When a source is the destination itself, its cost is the value of that one cell.
  • The output has one entry per element of sources , in input order. A source that appears twice gets two entries.
  • Return -1 for a source that cannot reach the destination. Every cell can be entered, so this does not happen under the constraints below.

Constraints

  • 1 <= m, n <= 100
  • 0 <= grid[i][j] <= 10^4
  • 1 <= len(sources) <= m * n
  • Every source and the destination are valid cells of the grid.

Examples

Input: grid = [[1,2,3],[4,5,1],[1,1,1]], sources = [[0,0],[2,0],[0,2]], destination = [2,2]
Output: [8,3,5]

From (0,0), going along the top row and down the right column costs 1 + 2 + 3 + 1 + 1 = 8, and going down the left column and along the bottom row also costs 1 + 4 + 1 + 1 + 1 = 8. Any path through the 5 in the center costs at least 10. From (2,0) the bottom row costs 1 + 1 + 1 = 3, and from (0,2) the right column costs 3 + 1 + 1 = 5.

Input: grid = [[0,1],[1,0]], sources = [[0,0],[1,1]], destination = [1,1]
Output: [1,0]

From (0,0) both routes cost 0 + 1 + 0 = 1. The second source is the destination, so its cost is grid[1][1] = 0.

Input: grid = [[5]], sources = [[0,0]], destination = [0,0]
Output: [5]

The only source sits on the destination. The path is that single cell, and it costs its value, 5.