感觉很简单,就是讨论一下
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; typedef pair<double,int>pii; const int N = 1e5+5; double x[N],y[N]; pii a[N],b[N]; double dis(double x1,double y1,double x2,double y2){ return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } int main(){ double ax,ay,bx,by,tx,ty,sum=0; int n; scanf("%lf%lf%lf%lf%lf%lf",&ax,&ay,&bx,&by,&tx,&ty); scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%lf%lf",&x[i],&y[i]); for(int i=1;i<=n;++i){ a[i].second=b[i].second=i; double k1=dis(ax,ay,x[i],y[i]); double k2=dis(bx,by,x[i],y[i]); double k3=dis(tx,ty,x[i],y[i]); sum+=k3*2; a[i].first=k1-k3; b[i].first=k2-k3; } sort(a+1,a+1+n); sort(b+1,b+1+n); if(a[1].first>=0&&b[1].first>=0){ sum+=min(a[1].first,b[1].first); } else if(a[1].first<0&&b[1].first>=0){ sum+=a[1].first; } else if(a[1].first>=0&&b[1].first<0){ sum+=b[1].first; } else { if(a[1].second!=b[1].second){ sum+=a[1].first+b[1].first; } else { if(n==1)sum+=min(a[1].first,b[1].first); else { double k1=a[1].first+b[2].first; double k2=b[1].first+a[2].first; k1=min(min(k1,k2),min(a[1].first,b[1].first)); sum+=k1; } } } printf("%.8f ",sum); return 0; }