HDU 5017 ellipsoid (Simulated Annealing)

Source: Internet
Author: User

Ellipsoid Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 1140 accepted submission (s): 412
Special Judge


Problem descriptiongiven a 3-dimension ellipsoid (elliptical)

Your task is to find the minimal distance between the original point (0, 0) and points on the ellipsoid. the distance between two points (x1, Y1, Z1) and (X2, Y2, Z2) is defined
Inputthere 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 abve. It is guaranteed that the input data forms a ellipsoid.All numbers are fit in double.
 
Outputfor 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
 
Source2014 ACM/ICPC Asia Regional Xi 'an online
Question: I also looked at others' questions and learned simulated annealing. It is not difficult and simple. The overall idea is to search in eight directions. The annealing means gradually narrowing down the search scope so that the final solution is accurate enough.
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>using namespace std;const double sp=0.99,eps=1e-8;double a,b,c,d,e,f,M=1e9;double dirx[]={-1,-1,-1,0,0,1,1,1};double diry[]={-1,0,1,-1,1,-1,0,1};double dis(double x,double y,double z){    return sqrt(x*x+y*y+z*z);}double getz(double x,double y){    double A=0,B=0,C=0;    A=c;    B=d*y+e*x;    C=a*x*x+b*y*y+f*x*y-1;    double delta=B*B-4*A*C;    if(delta<0) return M;    double z1=(sqrt(delta)-B)/(2.0*A),z2=(-sqrt(delta)-B)/(2.0*A);    if(z1*z1<z2*z2) return z1;    return z2;}double solve(){    double x=0,y=0,z=0,tx=0,ty=0,tz=0,step=1;    z=getz(x,y);    while(step>eps)    {        for(int i=0;i<8;i++)        {            tx=x+dirx[i]*step;            ty=y+diry[i]*step;            tz=getz(tx,ty);            if(tz>=M) continue;            if(dis(tx,ty,tz)<dis(x,y,z))            {                x=tx,y=ty,z=tz;            }        }        step*=sp;    }    return dis(x,y,z);}int main(){    while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)!=EOF)    {        printf("%.8lf\n",solve());    }    return 0;}






HDU 5017 ellipsoid (Simulated Annealing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.