hdu 5017 Ellipsoid 模拟退火

来源:互联网 发布:java微信商城开源 编辑:程序博客网 时间:2024/05/16 05:30

Ellipsoid

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

题意:

给定几个点  a,b,c,d,e,f的值,求在椭圆上的点x,y,z,使得式子 sqrt(x*x+y*y+z*z)的值最小,并输出该值。


思路:
第一题模拟退火的题目;
关于模拟退火,有很多讲的很好的博客,例如http://blog.sina.com.cn/s/blog_a3a7ea8201014btj.html

理解了思路,写出代码还是相对容易的,重要的是确定步长和步长变化的数值大小;


代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;double a,b,c,d,e,f;const double eps= 1e-8;int dx[8] = {0,0,1,-1,1,-1,1,-1};int dy[8] = {1,-1,0,0,1,1,-1,-1};double dis (double x,double y,double z){    return sqrt(x*x+y*y+z*z);}double calz (double x, double y ){    double A= c;    double B= d*y+e*x;    double C= a*x*x+b*y*y+f*x*y-1;    double delta = B*B-4*A*C;    if(delta<0){        return -1;    }    double z1=(-1.0*B+sqrt(delta))/(2*A);    double z2=(-1.0*B-sqrt(delta))/(2*A);    if(dis(x,y,z1)<dis(x,y,z2))        return z1;    else        return z2;}double  Simulated_Annealing(){    double step=1.0;    double rate=0.99;    double ans = 0x3f3f3f3f;    double fx=0,fy=0;    double fz=sqrt(1.0/c);    while(step>eps){        for(int k=0;k<8;k++)        {            double kx = fx + step*dx[k];            double ky = fy + step*dy[k];            double kz = calz(kx,ky);            if(kz ==-1) continue;            if(dis(kx,ky,kz) < dis(fx,fy,fz))            {                fx = kx,fy = ky,fz = kz;            }        }        step*=rate;    }    return dis(fx,fy,fz);}int main(){    while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)!=EOF){            printf("%.7lf\n",Simulated_Annealing());    }    return 0;}


0 0