HDU 4720 模拟退火

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值