代码
#include <iostream>
#include <cstring>
using namespace std;
const int N = 21, M = 1 << N;
int w[N][N], f[M][N];
int main()
{
int n;
cin>>n;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin>>w[i][j];
memset(f, 0x3f, sizeof f);
f[1][0] = 0;
for(int i = 0; i < 1 << n; i++)
for(int j = 0; j < n; j++)
if(i >> j & 1)
for(int k = 0; k < n; k++)
if((i >> k & 1) && j != k)
f[i][j] = min(f[i][j], f[i - (1 << j)][k] + w[k][j]);
cout<<f[(1 << n) - 1][n - 1];
return 0;
}
核心
状压dp