Minimum Cost to Visit All Checkpoints
Dynamic ProgrammingAuto-graded · 14 test casesLast updated
Tests bitmask DP over a small set of checkpoints to find the minimum-cost path that visits every checkpoint exactly once, starting and ending at a fixed depot.
Minimum Cost to Visit All Checkpoints
A delivery robot starts and ends at a depot (always node 0). It must visit every checkpoint (nodes 1 through n) exactly once before returning to the depot. You are given the pairwise travel costs between all locations (the depot plus the n checkpoints) as a 2-D list.
Given that cost matrix, find the minimum total travel cost for the robot to complete this round-trip.
Details
- There are
n + 1nodes in total: node0is the depot, nodes1 … nare checkpoints. cost[i][j]is the non-negative integer cost to travel directly from nodeito nodej.- Travel costs are not necessarily symmetric:
cost[i][j]may differ fromcost[j][i]. - The robot must leave the depot, visit every checkpoint exactly once (in any order), and return to the depot.
- Return the minimum total cost of such a tour.
Constraints
1 ≤ n ≤ 15(so there are at most 16 nodes including the depot)costis an(n+1) × (n+1)matrix of non-negative integerscost[i][i] = 0for alli0 ≤ cost[i][j] ≤ 10 000- There is always at least one checkpoint (
n ≥ 1)
Examples
Example 1
Input: min_cost_tour([[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]])
Output: 80
Example 2
Input: min_cost_tour([[0, 1, 100], [100, 0, 1], [1, 100, 0]])
Output: 3
Example 3
Input: min_cost_tour([[0, 7], [7, 0]])
Output: 14
Hints
Hint 1
Let the state be (current_node, set_of_visited_checkpoints). How many distinct states are there when n ≤ 15?
Hint 2
Use a bitmask of n bits where bit k (0-indexed) represents whether checkpoint k+1 has been visited. The full mask (all bits set) means every checkpoint has been visited.
Hint 3
Initialize dp[1 << (c-1)][c] = cost[0][c] for each checkpoint c, then transition by extending the tour to an unvisited checkpoint; finally add the return cost cost[node][0].
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is the classic Traveling Salesman Problem (TSP) on a directed graph with n ≤ 15 checkpoints, which is exactly the setting where bitmask DP is the standard polynomial-time approach.
State Definition
Let dp[mask][v] = minimum cost to travel from the depot, visit exactly the checkpoints encoded in mask (bit k = checkpoint k+1 has been visited), and currently be at node v.
maskranges over all2^nsubsets of checkpoints.vranges over checkpoints1 … n.
Transitions
Base case: For each checkpoint c, dp[1 << (c-1)][c] = cost[0][c] (go directly from depot to c).
Transition: For each (mask, v) with dp[mask][v] < INF and v ∈ mask, try extending to each unvisited checkpoint u:
new_mask = mask | (1 << (u-1))
dp[new_mask][u] = min(dp[new_mask][u], dp[mask][v] + cost[v][u])
Final answer: Once mask = FULL (all bits set), try returning to the depot from each v:
result = min over v of (dp[FULL][v] + cost[v][0])
Correctness
Every valid Hamiltonian cycle through the depot is represented by exactly one (mask, v) sequence that ends at mask = FULL. The DP considers all orderings via the transitions, so the global minimum is captured.
Complexity
- States:
O(2^n * n) - Transitions:
O(n)per state - Total time:
O(2^n * n^2)— forn = 15this is about7.5 millionoperations, well within limits. - Space:
O(2^n * n)
Reference Solution
from typing import List
def min_cost_tour(cost: List[List[int]]) -> int:
n = len(cost) - 1 # number of checkpoints (nodes 1..n)
FULL = (1 << n) - 1 # all n checkpoints visited
INF = float('inf')
dp = [[INF] * (n + 1) for _ in range(1 << n)]
# Base case: travel directly from depot (0) to each checkpoint c
for c in range(1, n + 1):
mask = 1 << (c - 1)
dp[mask][c] = cost[0][c]
# Fill DP
for mask in range(1, 1 << n):
for v in range(1, n + 1):
if dp[mask][v] == INF:
continue
if not (mask >> (v - 1) & 1):
continue # v must be in the visited set
# Try extending to each unvisited checkpoint u
for u in range(1, n + 1):
if mask >> (u - 1) & 1:
continue # already visited
new_mask = mask | (1 << (u - 1))
new_cost = dp[mask][v] + cost[v][u]
if new_cost < dp[new_mask][u]:
dp[new_mask][u] = new_cost
# Find minimum cost to complete the tour (return to depot from any v)
result = INF
for v in range(1, n + 1):
if dp[FULL][v] < INF:
result = min(result, dp[FULL][v] + cost[v][0])
return result