Ellipsoid
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 (x 1,y 1,z 1) and (x 2,y 2,z 2) 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
Source
Recommend
hujie
题目大意:
求一个椭球面上的一个点到原点的最短距离。
解题思路:
模拟退火,不多解释了。
解题代码:
#include#include #include using namespace std;const double eps=1e-8;const double INF=1e100;const int offx[]={1,0,-1,0,1,-1,-1,1};const int offy[]={0,1,0,-1,1,1,-1,-1};double a,b,c,d,e,f;double getAns(double x,double y){ double A=c,B=d*y+e*x,C=a*x*x+b*y*y+f*x*y-1.0; double delta=B*B-4*A*C; if(delta<0) return INF+10; delta=sqrt(delta); double z1=(-B+delta)/(2*A),z2=(-B-delta)/(2*A); return min( sqrt(x*x+y*y+z1*z1) , sqrt(x*x+y*y+z2*z2) );}double tosolve(double sx,double sy){ double x=sx,y=sy,ans=getAns(sx,sy),step=1e6; while(step>eps){ double sx=x,sy=y; bool flag=false; for(int i=0;i<8;i++){ double dx=x+offx[i]*step,dy=y+offy[i]*step; double tmp=getAns(dx,dy); if(tmp>=INF) continue; if(tmp
版权声明:欢迎关注我的博客,本文为博主toyking原创文章,未经博主允许不得转载。