【HDOJ】5017 Ellipsoid

简单地模拟退火。

 /* 5017 */
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath> #define INF 1e30 const double eps = 1e-;
const double next = 0.99;
int dir[][] = {
{-, }, {, }, {, -}, {, },
{-, }, {-, -}, {, -}, {, }
};
double a, b, c, d, e, f; double Length(double x, double y, double z) {
return sqrt(x*x + y*y + z*z);
} double getZ(double x, double y) {
double A, B, C, delta, tmp;
double z1, z2; A = c;
B = d*y + e*x;
C = a*x*x + b*y*y + f*x*y - 1.0;
tmp = B*B - *A*C;
if (tmp < )
return INF;
delta = sqrt(tmp);
z1 = (-B+delta)/(2.0*A);
z2 = (-B-delta)/(2.0*A); return fabs(z1) < fabs(z2) ? z1:z2;
} int main() {
double x, y, z;
double xx, yy, zz;
double step, tmp, ans;
int i; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f) != EOF) {
x = ;
y = ;
z = getZ(x, y);
ans = Length(x, y, z);
step = .;
while (step > eps) {
for (i=; i<; ++i) {
xx = x + dir[i][]*step;
yy = y + dir[i][]*step;
zz = getZ(xx, yy);
if (zz >= INF)
continue;
tmp = Length(xx, yy, zz);
if (tmp < ans) {
x = xx;
y = yy;
z = zz;
ans = tmp;
}
}
step *= next;
}
printf("%.7lf\n", ans);
} return ;
}
上一篇:WordPress研究心得


下一篇:MySQL to Redis同步