1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include<stdio.h> #include<math.h> #include<string.h> int n; double x[20],y[20],id[20],dis[20][20],f[20][35000],ans; double jishuan(double x11,double y11,double x22,double y22){ return sqrt(pow(x11-x22,2)+pow(y11-y22,2)); } double min(double x11,double y11){ if(x11<=y11){ return x11; } else{ return y11; } } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lf%lf",&x[i],&y[i]); } for(int i=0;i<n;i++){ for(int j=i+1;j<=n;j++){ dis[i][j]=jishuan(x[i],y[i],x[j],y[j]); dis[j][i]=dis[i][j]; } } memset(f,127,sizeof(f)); for(int i=1;i<=n;i++){ f[i][1<<(i-1)]=dis[i][0]; } for(int i=1;i<(1<<n);i++){ for(int j=1;j<=n;j++){ if(i&(1<<(j-1))){ for(int z=1;z<=n;z++){ if(i&(1<<(z-1))&&z!=j){ f[j][i]=min(f[j][i],f[z][i-(1<<(j-1))]+dis[z][j]); } } } } } for(int i=1;i<=n;i++){ if(i==1){ ans=f[i][(1<<n)-1]; } else{ if(f[i][(1<<n)-1]<ans){ ans=f[i][(1<<n)-1]; } } } printf("%.2lf",ans); return 0; }
|