Ellipsoid(模拟退火算法) 主要是数据范围

版权声明:小白一个,欢迎各位指错。 https://blog.csdn.net/qq_36424540/article/details/82561178

Problem Description

Given a 3-dimension ellipsoid(椭球面)


your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x1,y1,z1) and (x2,y2,z2) is defined as

Input

There are multiple test cases. Please process till EOF.

For each testcase, one line contains 6 real number a,b,c(0 < a,b,c,< 1),d,e,f(0 ≤ d,e,f < 1), as described above. It is guaranteed that the input data forms a ellipsoid. All numbers are fit in double.

Output

For each test contains one line. Describes the minimal distance. Answer will be considered as correct if their absolute error is less than 10-5.

Sample Input

1 0.04 0.01 0 0 0

Sample Output

1.0000000

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)


const double eps=1e-8; 

double a,b,c,d,e,f;

double  r=0.999;

double dir[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};


double get_z(double x,double y){
	double A=c,B=(d*y+e*x),C=a*x*x+b*y*y+f*x*y-1;
	double delta=B*B-4.0*A*C;
	if(delta<0.0)return 1e60;
	delta=sqrt(delta);
	double z1=(-B+delta)/(A*2.0),z2=(-B-delta)/(A*2.0);
	if(z1*z1<z2*z2)return z1;
	return z2;
}

double dis(double x,double y,double z){
	return sqrt(x*x+y*y+z*z); 
} 

void solve(){
	double step=1;
	double x=0,y=0,z=sqrt(1.0/c);double nx,ny,nz;
	while(step>eps){
	    z=get_z(x,y);
		for(int i=0;i<8;i++){
			nx=x+dir[i][0]*step,ny=y+dir[i][1]*step;nz=get_z(nx,ny);
			if(nz>1e30)continue;
			if(dis(nx,ny,nz)<dis(x,y,z)){
				x=nx,y=ny,z=nz;
			}
		} 
		step=step*r;
	} 
	printf("%.7f\n",dis(x,y,z));
}

int main(){
     while(scanf("%lf %lf %lf %lf %lf %lf",&a,&b,&c,&d,&e,&f)==6){
     	solve();
	 } 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/82561178