【HDU - 5017】Ellipsoid(爬山算法,模拟退火,三分)
題干:
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?
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 0Sample Output
1.0000000題目大意:
求橢圓上離圓心最近的點的距離。
解題報告:
? 先解方程,然后模擬退火。但是發現這個題無論如何也調不出樣例,無奈改爬山算法。(正解是三分)
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; const double eps = 1e-10; const double GG = 12398734577LL; const int MAX = 2e5 + 5; int nx[8] = {1,1,1,0,0,-1,-1,-1}; int ny[8] = {1,0,-1,1,-1,1,0,-1}; double a,b,c,d,e,f; //double Rand(){return rand()%(RAD+1)/(1.0*RAD);}//隨機產生0-1的浮點數 double calz(double x,double y) {double A=c,B=(d*y+e*x),C=f*x*y+a*x*x+b*y*y-1;double det = B*B-4*A*C;if(det < 0) return GG;double z1 = (-B+sqrt(det))/(2*A);double z2 = (-B-sqrt(det))/(2*A);if(fabs(z1)<fabs(z2)) return z1;else return z2; // return min(z1,z2); } double solve(double curx,double cury) {curx=0,cury=0;double T = 1,ansdis = GG;while(T>eps) {for(int i = 0; i<8; i++) {double tx = curx + nx[i]*T;double ty = cury + ny[i]*T;double tz = calz(tx,ty);if(fabs(tz-GG) < eps) continue;double tmpdis = sqrt(tx*tx+ty*ty+tz*tz);if(tmpdis < ansdis) {ansdis = tmpdis;curx = tx;cury = ty;}}T*=0.99;}return ansdis; } int main() {while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)) {double ans = 12312312313LL;ans = min(ans,solve(1/sqrt(a),0));ans = min(ans,solve(0,1/sqrt(b)));printf("%.6f\n",ans);}return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 5017】Ellipsoid(爬山算法,模拟退火,三分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 申请信用卡全部被拒是什么原因 申请信用卡
- 下一篇: 广发信用卡分期优惠 广发信用卡自助分期手