백준 2098 외판원 순회
#include<iostream>
#include<cstring>
#include <algorithm>
using namespace std;
const int MAX = 16;
const int INF = 987654321;
int N;
int W[MAX][MAX];
int cost[MAX][1 << MAX];
int TSP(int node, int bitmask)
{
if (bitmask == ((1 << N) - 1)){
if (W[node][0] == 0)
return INF;
else
return W[node][0];
}
int& ret = cost[node][bitmask];
if (ret != -1) return ret;
ret = INF;
for (int i = 0; i < N; i++){
if (W[node][i] == 0) continue;
if (bitmask & (1 << i)) continue;
ret = min(ret, W[node][i] + TSP(i, bitmask | (1 << i)));
}
return ret;
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
memset(cost, -1, sizeof(cost));
cin >> N;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
cin >> W[i][j];
cout << TSP(0, 1);
return 0;
}