Optimal Closed Tour Cost Across Directed Locations
Dynamic ProgrammingAuto-graded · 20 test casesLast updated
Find the minimum cost to complete a full circuit starting and ending at node 0, visiting all other nodes exactly once with asymmetric leg costs.
You are managing a delivery vehicle that must complete a full route servicing $n$ distinct locations, indexed from $0$ to $n - 1$.
You are given a 2D integer array dist of size $n \times n$, where dist[i][j] represents the cost of traveling directly from location $i$ to location $j$. Travel costs can be directional (i.e., dist[i][j] may not be equal to dist[j][i]).
The route must:
- Begin at location $0$.
- Visit every other location ($1$ through $n - 1$) exactly once.
- Return to location $0$.
Return the minimum total cost required to complete such a full circuit. If $n = 1$, the route starts and ends at location $0$ without moving, so the cost is $0$.
Constraints
- $1 \le n \le 16$
len(dist) == nandlen(dist[i]) == nfor all $0 \le i < n$dist[i][i] == 0for all $0 \le i < n$- $0 \le \text{dist}[i][j] \le 10^6$ for all $0 \le i, j < n$
Examples
Example 1
Input: min_tour_cost([[0, 3, 7, 4], [3, 0, 2, 6], [7, 2, 0, 5], [4, 6, 5, 0]])
Output: 14
Example 2
Input: min_tour_cost([[0, 1, 10], [10, 0, 1], [1, 10, 0]])
Output: 3
Example 3
Input: min_tour_cost([[0]])
Output: 0
Hints
Hint 1
The constraints on $n$ ($n \le 16$) suggest that an $O(n!)$ search is too slow, but an exponential dynamic programming approach using bitmasks will work.
Hint 2
Define state as `(mask, last_node)`, where `mask` is a bitfield representing the set of visited locations (excluding location 0), and `last_node` is the most recently visited location.
Hint 3
Iterate through masks in increasing order to ensure subproblems are solved before they are used to compute larger states, then find the minimum cost over all valid last nodes plus the cost to return to location 0.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a classic variation of the Traveling Salesperson Problem (TSP). Given $n \le 16$, an exhaustive search exploring all $(n-1)!$ permutations of locations would be inefficient. Instead, we use dynamic programming with bitmasks (Held-Karp algorithm).
Since every valid tour starts at location $0$, location $0$ is fixed as the origin and destination. We track the remaining $m = n - 1$ locations using a bitmask of size $m$, where the $k$-th bit ($0 \le k < m$) indicates whether location $k + 1$ has been visited.
We define the DP state:
dp[mask][j] = minimum cost to start at location 0, visit all locations represented in mask, and land at location $j + 1$ last (where bit $j$ is set in mask).
-
Base Cases: For each single location $j + 1$,
dp[1 << j][j] = dist[0][j + 1]. -
Transitions: For a given
maskand current location $j + 1$: For any unvisited location $k + 1$ (where bit $k$ is 0 inmask), transition to statenew_mask = mask | (1 << k)with last node $k$: $$\text{dp}[\text{new_mask}][k] = \min(\text{dp}[\text{new_mask}][k], , \text{dp}[\text{mask}][j] + \text{dist}[j + 1][k + 1])$$ -
Final Result: Once all locations $1 \dots n-1$ are visited (
mask == (1 << m) - 1), complete the circuit by returning to location $0$: $$\min_{0 \le j < m} (\text{dp}[\text{full} - 1][j] + \text{dist}[j + 1][0])$$
Complexity
- Time Complexity: $O(2^m \cdot m^2)$ where $m = n - 1$. For $n = 16$, $m = 15$, giving $2^{15} \cdot 15^2 \approx 7.37 \times 10^6$ operations, which comfortably runs well within standard limits.
- Space Complexity: $O(2^m \cdot m)$ to store the DP table.
Python Reference Solution
def min_tour_cost(dist: list[list[int]]) -> int:
n = len(dist)
if n == 1:
return 0
m = n - 1
INF = float("inf")
full = 1 << m
dp = [[INF] * m for _ in range(full)]
# Base case: initial step from city 0 to city (j + 1)
for j in range(m):
dp[1 << j][j] = dist[0][j + 1]
# Transitions: process states by bitmask size
for mask in range(1, full):
row = dp[mask]
for j in range(m):
cur = row[j]
if cur == INF or not (mask >> j) & 1:
continue
dj = dist[j + 1]
for k in range(m):
if mask & (1 << k):
continue
nm = mask | (1 << k)
v = cur + dj[k + 1]
if v < dp[nm][k]:
dp[nm][k] = v
last = dp[full - 1]
return min(last[j] + dist[j + 1][0] for j in range(m))