HDU 4720 模拟退火

来源:互联网 发布:四川自考数控编程试题 编辑:程序博客网 时间:2024/06/06 04:08
#include <cstdio>#include <cstring>#include <vector>#include <cmath>using namespace std;struct Point{double x, y;Point(double x = 0, double y = 0): x(x), y(y) {}void read(){scanf("%lf%lf", &x, &y);}};typedef Point Vector;typedef vector<Point> Polygon;Vector operator +(Vector A, Vector B)//{return Vector(A.x + B.x, A.y + B.y);}Vector operator -(Point A, Point B)//{return Vector(A.x - B.x , A.y - B.y);}Vector operator *(Vector A, double p)//{return Vector(A.x * p, A.y * p);}Vector operator /(Vector A, double p)//{return Vector(A.x / p, A.y / p);}bool operator <(const Point &a, const Point &b)//{return a.x < b.x || (a.x == b.x && a.y < b.y);}const double eps = 1e-10;struct Circle{Point c;double r;Circle(Point c, double r): c(c), r(r) {}Point point(double a){return Point(c.x + cos(a) * r, c.y + sin(a) * r);}};double Dot(Vector A, Vector B)//{return A.x * B.x + A.y * B.y;}double Length(Vector A)//{return sqrt(Dot(A, A));}int dcmp(double x)//{if (fabs(x) < eps) return 0;else return x < 0 ? -1 : 1;}double Distance(Point A, Point B){return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));}void circle_center(Point p0, Point p1, Point p2, Point &cp){double a1 = p1.x - p0.x, b1 = p1.y - p0.y, c1 = (a1 * a1 + b1 * b1) / 2;double a2 = p2.x - p0.x, b2 = p2.y - p0.y, c2 = (a2 * a2 + b2 * b2) / 2;double d = a1 * b2 - a2 * b1;cp.x = p0.x + (c1 * b2 - c2 * b1) / d;cp.y = p0.y + (a1 * c2 - a2 * c1) / d;}void circle_center(Point p0, Point p1, Point &cp){cp.x = (p0.x + p1.x) / 2;cp.y = (p0.y + p1.y) / 2;}Point center;double radius;bool point_in(const Point &p){return dcmp(Distance(p, center) - radius) <= 0;}void min_circle_cover(Point a[], int n){radius = 0;center  = a[0];for (int i = 1; i < n; i++) if (!point_in(a[i])){center = a[i]; radius = 0;for (int j = 0; j < i; j++) if (!point_in(a[j])){circle_center(a[i], a[j], center);radius = Distance(a[j], center);for (int k = 0; k < j; k++) if (!point_in(a[k])){circle_center(a[i], a[j], a[k], center);radius = Distance(a[k], center);}}}}int main(int argc, char const *argv[]){int T, kase = 0;scanf("%d", &T);while (T--){Point p[4], p4;p[0].read(), p[1].read(), p[2].read(); p4.read();min_circle_cover(p, 3);// printf("%lf %lf %lf\n", center.x, center.y, radius);if (point_in(p4)) printf("Case #%d: Danger\n", ++kase);else printf("Case #%d: Safe\n", ++kase);}return 0;}


多个点求最小圆覆盖,此题只有三个,模拟退火的模板题。

0 0