HDU 5017 Ellipsoid(退火模拟)

来源:互联网 发布:冰箱除味剂 知乎 编辑:程序博客网 时间:2024/05/19 12:16

Ellipsoid

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 658    Accepted Submission(s): 196
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 (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
 

Source
2014 ACM/ICPC Asia Regional Xi'an Online
 

参照了网上的程序,自己写了一遍。

利用退货模拟搜寻最优解;
这是一个不断搜寻最优解的过程,知道最后达到要求的精度的时候不再搜索了就;
代码:
#include <iostream>#include <math.h>#include <stdio.h>#include <string.h>#define INF 0x3f3f3f3f#define N 8            using namespace std;double a,b,c,d,e,f;const double r=0.99,eps=1e-9;///r是降温速度,eps是误差int dir[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};///搜索的八个方向(也可以是四个)double dis(double x,double y,double z){    return sqrt(x*x+y*y+z*z);}double getz(double x,double y)///根据x,y获取z的值(二元一次方程){    double A=c;    double B=d*y+e*x;    double C=a*x*x+b*y*y+f*x*y-1;    double detal=B*B-4*A*C;    if(detal<eps)        return INF;    return (sqrt(detal)-B)/(2*A);}double slove(){    int i;    double step=1;    double x=0,y=0,z;    while(step>eps)///步长开始降温    {        z=getz(x,y);        for(i=0; i<N; i++)        {            double nx=x+step*dir[i][0];            double ny=y+step*dir[i][1];            double nz=getz(nx,ny);///获取下一个x,y,z;            if(dis(nx,ny,nz)<dis(x,y,z))///找最优解                x=nx,y=ny,z=nz;        }        step*=r;///降温    }    return dis(x,y,z);}int main(){    double ans;    while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f))    {        ans=slove();        printf("%.8lf\n",ans);    }    return 0;}


0 0