基础状态压缩 转移方程为
dp[j][i] = min(dp[j][i],dp[k][i^(1<<j)] + dis[k][j]);
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; double dp[16][1<<16]; double x[16],y[16]; double dis[16][16]; int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i = 0;i < n;i++) scanf("%lf%lf",&x[i],&y[i]); for(int i = 0;i < n;i++) for(int j = 0;j < n;j++) dis[i][j] = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j])); for(int i = 0;i < n;i++) { dp[i][0] = 0; for(int j = 1;j < (1<<n);j++) dp[i][j] = 99999999; } for(int i = 0;i < n;i++) dp[i][1<<i] = 0; for(int i = 0;i < (1<<n);i++) { for(int j = 0;j < n;j++) { for(int k = 0;k < n;k++) { if( (1<<j)&i && (1<<k)&i && j != k) dp[j][i] = min(dp[j][i],dp[k][i^(1<<j)] + dis[k][j]); } } } double Min = 99999999.0; for(int i = 0;i < n;i++) { if(dp[i][(1<<n) -1] < Min) Min = dp[i][(1<<n)-1]; } printf("%.2lf ",Min); } return 0; }