Dynamic Programming21 sections · 916 units
Open in Course

TSP - State Design

Dp[mask][last]

dp[mask][last]dp[\text{mask}][\text{last}] = minimum cost to visit exactly the cities in mask, ending at city last. Base case: dp[1][0]=0dp[1][0] = 0. You start at city 00, having visited only city 00, with zero travel cost.

The mask =1= 1 means only bit 00 is set. Goal: Visit all cities (mask =2n1= 2^n - 1), then return to city 00. The answer is minlast(dp[2n1][last]+cost[last][0])\min_{\text{last}} (dp[2^n - 1][\text{last}] + \text{cost}[\text{last}][0]). You check all possible final cities and add the return flight cost.