AlgoMaster Logo

Minimum Cost Path with Teleportations

hardFrequencyUpdated September 21, 2026

Understanding the Problem

We need to find the cheapest path from the top-left to the bottom-right of a grid, where we can only move right or down (paying the destination cell's value), but we also have up to k free "teleportation" jumps. A teleportation lets us instantly move to any cell whose value is less than or equal to our current cell's value, at zero cost.

The normal movement part is a standard grid DP problem. The teleportation is what makes this hard. A single teleport can move you across the entire grid at zero cost, but only to a cell with an equal or smaller value. Teleportations are most useful when you are standing on a high-value cell, because more destinations become reachable. The best teleport targets are low-value cells close to the bottom-right corner, since fewer expensive normal moves remain afterward.

Teleportations split the problem into layers. With zero teleportations, it is standard DP. Each teleportation lets you restart from a new position at no cost, as long as the destination value does not exceed the cell you teleport from. So the problem is k+1 phases of normal movement, separated by up to k teleportation jumps.

Key Constraints:

  • 2 <= m, n <= 80 → The grid has at most 6,400 cells, small enough for O(m*n) DP per phase.
  • 0 <= k <= 10 → At most 10 teleportation uses, so adding k as a state dimension costs little.
  • 0 <= grid[i][j] <= 10^4 → A path uses at most m+n-2 = 158 normal moves, so the total cost stays well under 1.6 million. A 32-bit int holds every intermediate sum without overflow.

Approach 1: Dijkstra with State (row, col, teleports_used)

Intuition

Model this as a shortest path problem. Each state is a triple (row, col, teleports_remaining), and Dijkstra's algorithm finds the minimum cost from (0, 0, k) to (m-1, n-1, *). All edge weights are non-negative (normal moves cost a cell value >= 0, teleports cost 0), so Dijkstra applies.

From any state (i, j, t), we have two types of transitions:

  • Normal move: Go to (i+1, j, t) or (i, j+1, t) with cost grid[dest_row][dest_col].
  • Teleport (if t > 0): Go to (x, y, t-1) for any (x, y) where grid[x][y] <= grid[i][j], with cost 0.

The teleportation edges are the expensive part. From each cell, a teleport can reach up to mn other cells, so each pop can push up to O(mn) entries onto the priority queue.

Algorithm

  1. Create a priority queue (min-heap) with initial state (cost=0, row=0, col=0, teleports_remaining=k)
  2. Maintain a distance array dist[i][j][t] initialized to infinity
  3. Set dist[0][0][k] = 0
  4. While the priority queue is not empty:
    • Pop the state with minimum cost
    • If we've reached (m-1, n-1), return the cost
    • If this cost is greater than dist[i][j][t], skip (stale entry)
    • Try normal moves (right, down) and update distances
    • If teleports remaining > 0, try teleporting to all valid cells
  5. Return dist[m-1][n-1][best_t]

Example Walkthrough

1Start at (0,0) cost=0. Explore normal moves: down to (1,0) cost=2, right to (0,1) cost=3
0
1
2
0
0
1
3
3
3
1
2
2
5
4
2
4
3
5
1/5

Code

The bottleneck is teleportation. For every state popped from the heap, the algorithm scans all mn cells for valid targets. The next approach removes that cost by processing all teleportations for a whole phase in one sorted sweep, dropping the time to O(mnlog(mn) + kmn).

Approach 2: Layered DP with Sorting (Optimal)

Intuition

Instead of treating each teleportation as an individual graph edge, process the problem in phases. Phase 0 has zero teleportations and runs standard right-down DP. Phase 1 takes the phase 0 results, applies one teleportation, then runs another right-down DP pass. This repeats for up to k phases.

After computing DP for phase t, we need an efficient way to find where teleportation can take us in phase t+1. To teleport into cell (x, y), we must come from a cell (a, b) where grid[a][b] >= grid[x][y]. The cost of arriving at (x, y) by teleport is the minimum dp[t][a][b] over all such cells. So for each destination cell, we want the smallest DP value among all cells whose grid value is at least the destination's value.

Sorting solves this in one pass. Sort all cells by grid value in descending order and keep a running minimum of DP costs while iterating. When the scan reaches a cell with value v, every cell seen so far has value >= v, so the running minimum is the cheapest teleport source for that cell.

One detail matters. Cells with the same grid value can teleport to each other, because the condition is <=, not <. So all cells of one value must be processed as a group: first update the running minimum with every DP value in the group, then assign that running minimum as the teleport cost to every cell in the group. Doing it in two passes lets equal-valued cells serve as sources for each other.

Algorithm

  1. Create a list of all cells (value, row, col) and sort by value in descending order
  2. Compute dp[0] using standard right-down DP (no teleportation)
  3. For each teleportation phase t from 1 to k:
    • Scan the sorted cells in groups of equal value. For each group, update the running minimum with all DP values in the group, then assign that running minimum as the teleport cost for every cell in the group.
    • Initialize dp[t][i][j] = min(dp[t-1][i][j], teleport_cost[i][j])
    • Run standard right-down DP on dp[t]
  4. Return dp[k][m-1][n-1]

Example Walkthrough

1Input grid. Start at (0,0), goal is (2,2). Cost of starting cell is 0.
0
1
2
0
start
1
3
3
1
2
5
4
2
4
3
goal
5
1/8

Code