//CH0103 2020/10/07 23:21
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
const int INF = 0x3f3f3f3f;
int n, mp[N][N], val[N][1 << 20];
struct Node {
int cur, sit;
};
queue <Node> q;
int main () {
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> mp[i][j];
}
}
memset (val, 0x3f, sizeof (val));
val[0][1] = 0;
q.push ((Node) {0, 1}); // 从第 0 位开始
while (!q.empty ()) {
Node _node = q.front ();
q.pop ();
// cout << "cur = " << _node.cur << ", sit = " << _node.sit << ", val = " << val[_node.cur][_node.sit] << endl;
for (int i = 0; i < n; ++i) {
if (((_node.sit >> i) & 1) == 0) {
int _nextval = val[_node.cur][_node.sit] + mp[_node.cur][i];
int _nextsit = _node.sit + (1 << i);
// cout << "nextval = " << _nextval << ", nextsit = " << _nextsit << endl;
if (val[i][_nextsit] > _nextval) {
if (val[i][_nextsit] == INF) {
q.push ((Node){i, _nextsit});
}
val[i][_nextsit] = _nextval;
}
}
}
}
cout << val[n - 1][(1 << n) - 1] << endl;
}
基础的状压dp,每一层的点不会相互重复所以采取bfs。