Minimum Cost to Visit All Booths at a Fair
Dynamic ProgrammingAuto-graded · 13 test casesLast updated
Tests bitmask DP on small n by finding the minimum total travel cost to visit every booth at a fair, starting and ending at the entrance.
Minimum Cost to Visit All Booths at a Fair
You are helping plan a visit to a small fair. The fair has an entrance (always at position 0 in the list) and n booths, for a total of n + 1 locations indexed 0 through n.
You are given a 2-D list cost where cost[i][j] is the travel cost to go directly from location i to location j. Travel costs are not necessarily symmetric (cost[i][j] may differ from cost[j][i]).
Your goal is to find the minimum total cost of a route that:
- Starts at the entrance (location
0), - Visits every booth (locations
1throughn) exactly once, and - Returns to the entrance (location
0).
Return this minimum cost as an integer (all costs are non-negative integers).
Constraints
1 ≤ n ≤ 12(number of booths; total locations =n + 1, at most 13)costis a square matrix of size(n + 1) × (n + 1)0 ≤ cost[i][j] ≤ 1000for alli,jcost[i][i] = 0for alli- It is always possible to complete the route (the graph is complete)
- You must visit every booth exactly once before returning; skipping or revisiting a booth is not allowed
Examples
Example 1
Input: min_fair_tour_cost([[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]])
Output: 80
Example 2
Input: min_fair_tour_cost([[0, 1, 2], [1, 0, 3], [2, 3, 0]])
Output: 6
Example 3
Input: min_fair_tour_cost([[0, 5], [5, 0]])
Output: 10
Hints
Hint 1
Let dp[mask][i] represent the minimum cost to reach booth i, having visited exactly the set of booths encoded in mask. Start with dp[1<<i][i] = cost[0][i] for each booth i.
Hint 2
Transition: for each state (mask, i), try extending to an unvisited booth j: dp[mask | (1<<j)][j] = min(..., dp[mask][i] + cost[i+1][j+1]). Use 0-indexed booth bits but be careful about the offset between bit positions and location indices.
Hint 3
The answer is the minimum over all booths i of dp[full_mask][i] + cost[i+1][0], where full_mask has all n booth bits set.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is the classic Travelling Salesman Problem (TSP) on a small complete directed graph, solved with bitmask dynamic programming.
State definition
Let dp[mask][i] be the minimum cost to arrive at booth i+1 (1-indexed location) having visited exactly the subset of booths encoded by mask, where bit k of mask is set iff booth k+1 has been visited.
- There are
2^npossible masks andnpossible last-booth positions, givingn * 2^nstates total. - With
n ≤ 12that is at most12 * 4096 = 49 152states — trivially fast.
Initialisation
For each booth i (0-indexed in our bit representation, 1-indexed as location), set:
dp[1 << i][i] = cost[0][i + 1]
i.e., travel directly from the entrance to booth i+1.
Transition
For each (mask, i) pair where bit i is set in mask and dp[mask][i] is finite, try extending to every unvisited booth j:
dp[mask | (1 << j)][j] = min(dp[mask | (1 << j)][j],
dp[mask][i] + cost[i + 1][j + 1])
Answer
When mask == full_mask (all n booth bits set), add the return leg to the entrance:
answer = min over all i of (dp[full_mask][i] + cost[i + 1][0])
Correctness
Every valid tour corresponds to exactly one sequence of state transitions from an initial single-booth state to the full-mask state. The DP exhausts all such sequences and keeps track of the minimum, so the answer is optimal.
Complexity
- Time: O(2^n · n²) — for each of the
2^nmasks andnlast positions, we trynextensions. - Space: O(2^n · n) for the DP table.
Both are well within limits for n ≤ 12.
from typing import List
def min_fair_tour_cost(cost: List[List[int]]) -> int:
n = len(cost) - 1 # number of booths; locations are 0..n
if n == 0:
return 0
INF = float('inf')
full_mask = (1 << n) - 1
# dp[mask][i] = min cost to reach booth (i+1) having visited the
# booths whose bits are set in mask (bit k represents booth k+1)
dp = [[INF] * n for _ in range(1 << n)]
# Initialise: go directly from entrance (loc 0) to each booth
for i in range(n):
dp[1 << i][i] = cost[0][i + 1]
# Fill DP
for mask in range(1, 1 << n):
for i in range(n):
if not (mask >> i & 1):
continue
if dp[mask][i] == INF:
continue
# Extend to an unvisited booth j
for j in range(n):
if mask >> j & 1:
continue
new_mask = mask | (1 << j)
new_cost = dp[mask][i] + cost[i + 1][j + 1]
if new_cost < dp[new_mask][j]:
dp[new_mask][j] = new_cost
# Return to entrance from every possible last booth
ans = INF
for i in range(n):
if dp[full_mask][i] < INF:
ans = min(ans, dp[full_mask][i] + cost[i + 1][0])
return int(ans)