--- leetcode_url: https://leetcode.com/problems/minimum-cost-path-with-teleportations date: 2026-02-06 tags: - DynamicProgramming --- # Thought ## State Transfer Overview State transfer for `m_cost_arr[layer][r][c]`: ```python # normal move m_cost_arr[layer][r - 1][c]; # from above m_cost_arr[layer][r][c - 1]; # from left # teleportation get_min_cost( max_teleport = layer - 1, min_node_weight = weight[r][c], ) ``` ----- In above, `m_cost_arr[max_teleport][row_idx][column_idx]` is a 3D array storing the dynamic programming solutions for sub-questions: ![[260206_m_cost_arr.png]] The green highlighted area is the **sub-question area** which's solution stored in the array. That is: **value in `m_cost_arr[layer][i][j]` is the solution for sub-question when only considering highlighted nodes from $(0, 0)$ to $(i,j)$ with maximum allowed transportation count `layer`.** > [!note] > > The **sub-question area is not a strict triangular**, this definition is required based on how we deal with transportation when performing state transfer. Keep reading for more info. > [!note] > > `layer` in this article usually refers to max allowed transportation count. ----- We actually split up the state transfer into two different cases: The last operation to the destination point $(r,c)$: 1. Is a Normal Move operation. 2. Is a Teleportation operation. It's obvious that the first case could be done with $O(1)$ complexity. The point is to figure out how to perform state transfer for the second case in an efficient way, that is, the transportation as the last operation to point $(r,c)$. ## Teleportation As Last Operation We know that: - Teleportation itself **do not impose cost** - Teleportation will use teleportation quota So we have: ```python cost_from_origin_to(x, y, teleport_limit = n) = cost_from_origin_to(i, j, teleport_limit = n - 1) + 0 # cost nothing ``` So the optimal solution when last operation is Teleportation should be: $$ \text{min}(\text{CostFromOriginTo}(x, y, \text{layer} - 1)), \quad \text{where: } \begin{cases} (x,y) \in \text{SubQuestionSpace} \\ \text{weight}(x,y) \ge \text{weight}(i,j) \end{cases} $$ ![[260206_optimal_for_teleportation.png]] For example, still consider sub-question `[layer][i][j]`, as the image show, let's say all the green nodes have nodes weight $> min_solution; ``` ## Min Cost