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.
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.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:
(i+1, j, t) or (i, j+1, t) with cost grid[dest_row][dest_col].(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.
(cost=0, row=0, col=0, teleports_remaining=k)dist[i][j][t] initialized to infinitydist[0][0][k] = 0(m-1, n-1), return the costdist[i][j][t], skip (stale entry)dist[m-1][n-1][best_t]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).
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.
Splitting into phases is valid because the optimal path uses some number of teleports t between 0 and k, and a path with t teleports decomposes into t+1 stretches of normal moves joined by t teleports. Phase t of the DP captures exactly the best cost reachable using up to t teleports, so dp[k][m-1][n-1] covers every legal path.
Within a single phase the teleport step needs no ordering because each phase performs at most one teleport per cell: it reads dp[t-1] to build teleportCost, then merges and runs DP to produce dp[t]. The source values are frozen before any teleport cost is assigned, so a cheaper teleport source cannot be missed regardless of scan order.
(value, row, col) and sort by value in descending orderdp[0] using standard right-down DP (no teleportation)dp[t][i][j] = min(dp[t-1][i][j], teleport_cost[i][j])dp[t]dp[k][m-1][n-1]