【HDU3007】模拟退火求解最小圆覆盖

本文探讨了使用模拟退火(SA)算法解决经典最小圆覆盖问题的方法。通过详细的代码示例,展示了如何找到能够覆盖所有给定点的最小圆心坐标及半径,适用于几何优化问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.题目链接. 题目大意:给出n个点,求出最小的圆来覆盖这些点

2.十分经典的最小圆覆盖问题。直接SA就好了。代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N = 55;
#pragma warning(disable:4996);
const double eps = 1e-8;
const double INF = 1e19;
struct Point
{
	double x, y;
}p[N];
int n;
double x, y, r;
double dis(Point a, Point b)
{
	return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y,2));
}
void SA()
{
	double T = 10;
	double delt = 0.98;
	Point a = p[0];
	 r = INF;
	while (T > eps)
	{
		int  k = 0;
		double d = 0;
		for (int i = 0; i < n; i++)
		{
			double f = dis(a, p[i]);
			if (f>d)
			{
				d = f;
				k = i;
			}
		}
		a.x += (p[k].x - a.x) / d * T;
		a.y += (p[k].y - a.y) / d * T;
		r = min(r, d);
		T *= delt;
	}
	x = a.x, y = a.y;
}
int main()
{
	while (scanf("%d", &n) && n)
	{
		for (int i = 0; i < n; i++)
			scanf("%lf%lf", &p[i].x, &p[i].y);
		SA();
		printf("%.2lf %.2lf %.2lf\n", x, y, r);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值