//http://acm.hnu.cn/online/?action=problem&type=show&id=12583&courseid=242
//一个tsp问题,用位状态压缩来解,只不过要先处理两点的距离;
//代码如下:
#include<stdio.h> #include<math.h> #include<string.h> int n; double dis[20][20],x[20],y[20],l,w; double getdis(double x1,double y1,double x2,double y2) { double d2,d1; d1=sqrt(double((x1+x2)*(x1+x2)+(y1-y2)*(y1-y2))); d2=sqrt(double((x1-x2)*(x1-x2)+(y1+y2)*(y1+y2))); if(d2<d1)d1=d2; d2=sqrt(double((x1-2*w+x2)*(x1-2*w+x2)+(y1-y2)*(y1-y2))); if(d2<d1)d1=d2; d2=sqrt(double((x1-x2)*(x1-x2)+(y1-2*l+y2)*(y1-2*l+y2))); if(d2<d1)d1=d2; return d1; } void init() { int i,j; for(i=1;i<=n;i++) dis[0][i]=sqrt(double(x[0]-x[i])*(x[0]-x[i])+(y[0]-y[i])*(y[0]-y[i])); for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) dis[i][j]=dis[j][i]=getdis(x[i],y[i],x[j],y[j]); dis[i][i]=0; } } double Min(double a,double b){return a<b?a:b;} double find(double x1,double y1) { double d1=Min(x1,y1); if(w-x1<d1)d1=w-x1; if(l-y1<d1)d1=l-y1; return d1; } double d[1<<18][19]; double tsp() { double res; int i,j,k; for(i=0;i<(1<<n);i++) for(j=1;j<=n;j++)d[i][j]=1000000; for(i=1;i<=n;i++) d[1<<(i-1)][i]=dis[0][i]; for(i=1;i<(1<<n);i++) for(j=1;j<=n;j++) if(i&(1<<(j-1))) for(k=1;k<=n;k++) if(!(i&(1<<(k-1)))) d[i|(1<<(k-1))][k]=Min(d[i|(1<<(k-1))][k],d[i][j]+dis[j][k]); res=10000000; for(i=1;i<=n;i++) res=Min(res,d[(1<<n)-1][i]+find(x[i],y[i])); return res; } int main() { int i; while(scanf("%lf%lf",&w,&l)!=EOF) { scanf("%d",&n); for(i=1;i<=n;i++) scanf("%lf%lf",&x[i],&y[i]); scanf("%lf%lf",&x[0],&y[0]); init(); printf("%.8lf\n",tsp()); } return 0; }