题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5017
Ellipsoid
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1130 Accepted Submission(s): 405
Special Judge
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
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.
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
思路: 原方程可以转化为: cz^2+(dy+ex)z+(fxy+ax^2+by^2-1)=0
即转化为关于z的一元二次方程,然后模拟退火即可(网上学的==!)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
const double eps=1e-8;
const double INF=99999999;
using namespace std;
double a,b,c,d,e,f;
int xx[]={0,0,-1,1,-1,1,-1,1};
int yy[]={-1,1,0,0,-1,-1,1,1};
double distance(double x,double y,double z)
{
return sqrt(x*x+y*y+z*z);
}
double cal(double x,double y)
{
double A=c;
double B=d*y+e*x;
double C=f*x*y+a*x*x+b*y*y-1;
double del=B*B-4*A*C;
if(del<0)return -INF;
double z1=(-B+sqrt(del))/(2*A);
double z2=(-B-sqrt(del))/(2*A);
if(z1*z1<z2*z2)
return z1;
return z2;
}
double solve()
{
double step=1.0,r=0.99;//步长和降火温度
double x=0,y=0,z;
while(step>eps)
{
z=cal(x,y);
for(int i=0;i<8;i++)
{
double dx=x+xx[i]*step;
double dy=y+yy[i]*step;
double dz=cal(dx,dy);
if(dz==-INF)continue;
if(distance(dx,dy,dz)<distance(x,y,z))
{
x=dx;
y=dy;
z=dz;
}
}
step*=r;
}
return distance(x,y,z);
}
int main()
{
while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)!=EOF)
{
printf("%.7lf\n",solve());
}
return 0;
}