HDU 5017 Ellipsoid (计算几何,模拟退火)

时间:2014-10-06 18:37:10   收藏:0   阅读:195

Ellipsoid

Problem Description
Given a 3-dimension ellipsoid(椭球面)
bubuko.com,布布扣

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 bubuko.com,布布扣
 

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 <iostream>
#include <cstdio>
#include <cmath>
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<ans){
                ans=tmp;
                flag=true;
                sx=dx,sy=dy;
            }
        }
        x=sx,y=sy;
        if(!flag) step/=2;
    }
    return ans;
}

void solve(){
    //cout<<tosolve(0,0)<<" "<<tosolve(sqrt(1.0/a),0)<<" "<<tosolve(0,sqrt(1.0/b))<<endl;
    double ans=tosolve(0,0),tmp;
    tmp=tosolve(sqrt(1.0/a),0);
    if(tmp<ans) ans=tmp;
    tmp=tosolve(-sqrt(1.0/a),0);
    if(tmp<ans) ans=tmp;
    tmp=tosolve(0,sqrt(1.0/b));
    if(tmp<ans) ans=tmp;
    tmp=tosolve(0,-sqrt(1.0/b));
    if(tmp<ans) ans=tmp;
    printf("%.7lf\n",ans);
}

int main(){
    while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)!=EOF){
        solve();
    }
    return 0;
}



原文:http://blog.csdn.net/a1061747415/article/details/39830299

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!