1. 程式人生 > >Bzoj1857:[Scoi2010]傳送帶:三分

Bzoj1857:[Scoi2010]傳送帶:三分

首先由猜測法證得函式具有下凸性QwQ

然後就可以三分辣

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-5;
struct point{double x,y;}A,B,C,D;
double p,q,r,ax,ay,bx,by,cx,cy,dx,dy;

double powe(double x){return x*x;}

double calc(point a,point b){
	return sqrt(powe(a.x-b.x)+powe(a.y-b.y));
}

double calc2(double x,double y,point a){
	return sqrt(powe(x-a.x)+powe(y-a.y));
}

double getval(point tmp){
	C.x=cx; C.y=cy; D.x=dx; D.y=dy;
	while (abs(C.x-D.x)>eps||abs(C.y-D.y)>eps){
		point tmpl; tmpl.x=C.x+(D.x-C.x)/3; tmpl.y=C.y+(-C.y+D.y)/3;
		point tmpr; tmpr.x=C.x+(D.x-C.x)/3*2; tmpr.y=C.y+(-C.y+D.y)/3*2;
		double ret1=calc2(ax,ay,tmp)/p+calc(tmp,tmpl)/r+calc2(dx,dy,tmpl)/q;
		double ret2=calc2(ax,ay,tmp)/p+calc(tmp,tmpr)/r+calc2(dx,dy,tmpr)/q;
		if (ret1>ret2) C=tmpl; else D=tmpr;
	}
	return calc2(ax,ay,tmp)/p+calc(tmp,C)/r+calc2(dx,dy,C)/q;
}

int main(){
	scanf("%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y);
	scanf("%lf%lf%lf%lf",&C.x,&C.y,&D.x,&D.y);
	ax=A.x; ay=A.y; bx=B.x; by=B.y;
	cx=C.x; cy=C.y; dx=D.x; dy=D.y;
	scanf("%lf%lf%lf",&p,&q,&r);
	while (abs(A.x-B.x)>eps||abs(A.y-B.y)>eps){
		point tmpl; tmpl.x=A.x+(B.x-A.x)/3; tmpl.y=A.y+(B.y-A.y)/3;
		point tmpr; tmpr.x=A.x+(B.x-A.x)/3*2; tmpr.y=A.y+(B.y-A.y)/3*2;
		double ret1=getval(tmpl),ret2=getval(tmpr);
		if(ret1>ret2) A=tmpl; else B=tmpr;
	}
	printf("%.2lf\n",getval(A));
}