Created: 2019-12-08 Sun 14:57
vi dp(65536); // we can't solve more than this anyway
int n;
int tsp(vvi &costs, int &mx, int cur, int state) {
if (dp[state]>0) return dp[state];
if (state == mx) return costs[cur][0]; // return home
int minleft = INF;
int bit=2;
for(int i=1; i<n; ++i) {
if ( (state & bit) == 0) { // i not visited
minleft = min(minleft, costs[cur][i] + tsp(costs,mx,i,state | bit));
}
bit <<= 1;
}
return dp[state]=minleft;
}
int main() {
cin >> n;
vvi adj(n);
for(i=0; i<n; ++i)
for(j=0; j<n; ++j) {
cin >> c;
adj[i].push_back(c);
}
mx = (1 << n) - 1;
cout << "Best path has cost " << tsp(adj,mx,0,1) << endl;
}