最小覆盖圆模版 hdu3932 + 模拟退火

本文探讨了最小覆盖圆算法与模拟退火法的结合使用,通过给定n个点,找到一个点使得n个点到这个点的距离之和最小。详细介绍了算法的具体实现过程,包括如何框定搜索范围、生成候选点以及优化搜索过程。最后通过实例展示了如何求解实际问题,提供了一种有效的解决方法。

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

<pre name="code" class="cpp">/*
转自:http://blog.youkuaiyun.com/zxy_snow/article/details/6682926
*/
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")

using namespace std;

const double eps = 1e-6;
const int MAX = 1010;
struct point{double x,y;};
point p[MAX];
bool dy(double x,double y)	{	return x > y + eps;}	// x > y 
bool xy(double x,double y)	{	return x < y - eps;}	// x < y 
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y 
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y 
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
double disp2p(point a,point b) //  a b 两点之间的距离 
{
	return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
point circumcenter(point a,point b,point c)//求外接圆圆心
{
 	point ret;
 	double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1*a1 + b1*b1)/2; 
  	double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2*a2 + b2*b2)/2; 
  	double d = a1 * b2 - a2 * b1; 
  	ret.x = a.x + (c1*b2 - c2*b1)/d; 
  	ret.y = a.y + (a1*c2 - a2*c1)/d; 
 return ret;
}
void min_cover_circle(point p[],int n,point &c,double &r)//求最小覆盖圆
{
	random_shuffle(p,p+n);// #include <algorithm>
	c = p[0]; r = 0;
	for(int i=1; i<n; i++)
		if( dy(disp2p(p[i],c),r) )
		{
			c = p[i]; r = 0;
			for(int k=0; k<i; k++)
				if( dy(disp2p(p[k],c),r) )
				{
					c.x = (p[i].x + p[k].x)/2;
					c.y = (p[i].y + p[k].y)/2;
					r = disp2p(p[k],c);
					for(int j=0; j<k; j++)
						if( dy(disp2p(p[j],c),r) )
						{							// 求外接圆圆心,三点必不共线 
							c = circumcenter(p[i],p[k],p[j]);
							r = disp2p(p[i],c);
						}
				}
		}
}

int main()
{
	int ncases,n;
	
	double x,y;
	while( ~scanf("%lf%lf%d",&x,&y,&n) )
	{
		for(int i=0; i<n; i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
		double r;
		point c;
		min_cover_circle(p,n,c,r);
		printf("(%.1lf,%.1lf).\n",c.x,c.y);
		printf("%.1lf\n",r);
	}

return 0;
}

/*
参考:http://blog.sina.com.cn/s/blog_64675f540100sehz.html
题意:给定n个点,找到一个点,使得n个点到这个点的和值最小
模拟退火法
模拟退火的过程
1 找到这些点所在的范围,用两个点框定(代码中e1,e2两个点)
2 在这个范围内生成NUM个点(NUM自定)
3 对于每个生成的点i,在其周围生成NUM个点,一旦有点优于i,则替换。
4 缩小范围D,若D<精度,退出,否则执行 3
5 遍历所有NUM个点,找到val的最大值
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include<map>
#include<ctime>
using namespace std;
const int NUM=30;
const int RAD=1000;
struct point
{
	double x,y,val;
	point(){}
	point(double _x,double _y):x(_x),y(_y){}
}p[1001],May[NUM],e1,e2;
int n;
double dis(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double judge(point t)//评价函数,得到点t的评价值val
{
	double len=0;
	for(int i=0;i<n;i++)
	len=max(len,dis(t,p[i]));
	return len;
}
double Rand(){return rand()%(RAD+1)/(1.0*RAD);}//随机产生0-1的浮点数
point Rand_point(point a,point b)//在a,b框定的四边形内随机生成点
{
	point tmp=point(a.x+(b.x-a.x)*Rand(),a.y+(b.y-a.y)*Rand());
	tmp.val=judge(tmp);
	return tmp;
}
void solve(double D)
{
	for(int i=0;i<NUM;i++)
	May[i]=Rand_point(e1,e2);//步骤2
	while(D>0.01)//步骤 3
	{
		for(int i=0;i<NUM;i++)
		for(int j=0;j<NUM;j++)
		{
			point tmp=Rand_point(point(May[i].x-D,May[i].y-D),point(May[i].x+D,May[i].y+D));
			if(tmp.val<May[i].val)
			{
				May[i]=tmp;
			}
		}
		D*=0.5;
	}
	point ans;
	ans.val=1LL<<45;
	for(int i=0;i<NUM;i++)
	if(May[i].val<ans.val)
	ans=May[i];
	printf("(%.1f,%.1f).\n",ans.x,ans.y);
	printf("%0.1f\n",ans.val);
}
int main()
{
	srand(time(0));
	e2=point(0,0);
	while(scanf("%lf%lf%d",&e1.x,&e2.y,&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
		scanf("%lf%lf",&p[i].x,&p[i].y);
		e1.x=min(e1.x,p[i].x);//框定初始范围
		e1.y=min(e1.y,p[i].y);
		e2.x=max(e2.x,p[i].x);
		e2.y=max(e2.y,p[i].y);
		}
		solve(max(e2.y-e1.y,e2.x-e1.x));
	}
}







                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值