状压DP
DP[S][i]在S状态下到i最后到i点的最小距离
#include <queue>
#include <cstdio>
#include <cstring>
#define il inline
#define idx(x) 1<<(x-1)
using namespace std;
int dis[21][21],dp[(1<<20)+10][21];
il int qread()
{
int x=0,w=1;
char ch=0;
while(ch<'0'||ch>'9')
{
if(ch=='-') w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return x*w;
}
int main()
{
int n;
n=qread();
int ans=1e7;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
dis[i][j]=qread();
}
memset(dp,127/3,sizeof(dp));
dp[idx(1)][1]=0;
for(int s=0;s<=(1<<n)-1;s++)
{
for(int i=1;i<=n;i++)
if(idx(i)&s)
for(int j=1;j<=n;j++)
if(!(idx(j)&s))
dp[s|(idx(j))][j]=min(dp[s|(idx(j))][j],dp[s][i]+dis[i][j]);
}
for(int i=2;i<=n;i++)
ans=min(dp[(1<<n)-1][i]+dis[i][1],ans);
return printf("%d\n",ans);
/*printf("%d",sizeof(dp));*/
}