//f[i][j]表示从0走到j,走过的所有点是i(一个二进制数)的所有路径 #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 20, M = 1 << N; int n; int w[N][N];//两点之间的距离 int f[M][N];//状态 int main() { 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;//从0走到0,1表示0走过,距离位0 for (int i = 0; i < 1 << n; i ++ ) for (int j = 0; j < n; j ++ ) //如果从0走到j,那么i里面一定要包含j if (i >> j & 1) for (int k = 0; k < n; k ++ )//枚举从哪个点转移过来 if ((i-(1<<j))>>k&1)//如果说,想从k点转移过来 //那么i除去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; }