最小圆覆盖(随机增量法&模拟退火法)
http://acm.hdu.edu.cn/showproblem.php?pid=3007
相关题型连接:
http://acm.hdu.edu.cn/showproblem.php?pid=3932
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=450
Buried memory
The world king Sconbin is not the exception.One day,Sconbin was sleeping,then swakened by one nightmare.It turned out that his love letters to Dufein were made public in his dream.These foolish letters might ruin his throne.Sconbin decided to destroy the letters
by the military exercises's opportunity.The missile is the best weapon.Considered the execution of the missile,Sconbin chose to use one missile with the minimum destruction.
Sconbin had writen N letters to Dufein, she buried these letters on different places.Sconbin got the places by difficult,he wants to know where is the best place launch the missile,and the smallest radius of the burst area. Let's help Sconbin to get the award.
and y coordinate.N=0 is the end of the input file.
output numbers are rounded to the second digit after the decimal point.
3 1.00 1.00 2.00 2.00 3.00 3.00 0
2.00 2.00 1.41
方法一:随机增量法:
分析:用最小的圆覆盖住所有的点:随机增量法复杂度是O(n);
2、算法及原理算法介绍:我们本次算法的设计是基于这样一个简单直观的性质:
在既定的给定点条件下,如果引入一张新的半平面,只要此前的最优解顶点(即唯一确定最小包围圆的几个关键顶点)能够包含于其中,则不必对此最优解进行修改,亦即此亦为新点集的最优解;否则,新的最优解顶点必然位于这个新的半空间的边界上。定理可以通过反证法证明。于是,基于此性质,我们便可得到一个类似于线性规划算法的随机增量式算法。定义Di为相对于pi的最小包围圆。此算法实现的关键在于对于pi∉Di-1时的处理。显然,如果pi∈Di-1,则Di= Di-1;否则,需要对Di另外更新。而且,Di的组成必然包含了pi;因此,此种情况下的最小包围圆是过pi点且覆盖点集{
p1 ,p2 ,p3 ……pi-1}的最小包围圆。则仿照上述处理的思路,Di={ p1 ,pi },逐个判断点集{ p2 ,p3 ……pi-1 },如果存在pj∉ Di,则Di={pj,pi }。同时,再依次对点集{ p1 ,p2 ,p3 ……pj-1 }判断是否满足pk∈Di,若有不满足,则Di={pk ,pj,pi }。由于,三点唯一地确定一个圆,故而,只需在此基础上判断其他的点是否位于此包围圆内,不停地更新pk。当最内层循环完成时,退出循环,转而更新pj;当次内层循环结束时,退出循环,更新pi。当i=n时,表明对所有的顶点均已处理过
,此时的Dn即表示覆盖了给定n个点的最小包围圆。
总结:
假设圆O是前i-1个点得最小覆盖圆,加入第i个点,如果在圆内或边上则什么也不做。否,新得到的最小覆盖圆肯定经过第i个点。然后以第i个点为基础(半径为0),重复以上过程依次加入第j个点,若第j个点在圆外,则最小覆盖圆必经过第j个点。重复以上步骤(因为最多需要三个点来确定这个最小覆盖圆,所以重复三次)。遍历完所有点之后,所得到的圆就是覆盖所有点得最小圆。证明可以考虑这么做:最小圆必定是可以通过不断放大半径,直到所有以任意点为圆心,半径为半径的圆存在交点,此时的半径就是最小圆。所以上述定理可以通过这个思想得到。这个做法复杂度是O(n)的,当加入圆的顺序随机时,因为三点定一圆,所以不在圆内概率是3/i,求出期望可得是O(n)。
方法二:模拟退火法:对于每个枚举的点找到改点到所给点的最远点的距离,然后保证这个距离最小,即为所求圆的半径;
程序:
方法一:随机增量法:
- #include"string.h"
- #include"stdio.h"
- #include"queue"
- #include"stack"
- #include"vector"
- #include"algorithm"
- #include"iostream"
- #include"math.h"
- #include"stdlib.h"
- #define M 522
- #define inf 100000000
- #define eps 1e-8
- #define PI acos(-1.0)
- using namespace std;
- double X,Y;
- struct Point
- {
- double x,y;
- }p[M];
- struct Triangle
- {
- Point v[3];
- };
- struct Circle
- {
- Point center;
- double r;
- };
- double pow(double x)
- {
- return x*x;
- }
- double Len(Point a,Point b)
- {
- return sqrt(pow(a.x-b.x)+pow(a.y-b.y));
- }
- double TriangleArea(Triangle a)//求三角形的面积
- {
- double px1=a.v[1].x-a.v[0].x;
- double py1=a.v[1].y-a.v[0].y;
- double px2=a.v[2].x-a.v[0].x;
- double py2=a.v[2].y-a.v[0].y;
- return fabs(px1*py2-px2*py1)/2;
- }
- Circle CircleOfTriangle(Triangle t)//就三角形外接圆
- {
- Circle tmp;
- double a=Len(t.v[0],t.v[1]);
- double b=Len(t.v[0],t.v[2]);
- double c=Len(t.v[1],t.v[2]);
- tmp.r=a*b*c/4/TriangleArea(t);
- double a1=t.v[1].x-t.v[0].x;
- double b1=t.v[1].y-t.v[0].y;
- double c1=(a1*a1+b1*b1)/2;
- double a2=t.v[2].x-t.v[0].x;
- double b2=t.v[2].y-t.v[0].y;
- double c2=(a2*a2+b2*b2)/2;
- double d=a1*b2-a2*b1;
- tmp.center.x=t.v[0].x+(c1*b2-c2*b1)/d;
- tmp.center.y=t.v[0].y+(a1*c2-a2*c1)/d;
- return tmp;
- }
- void Run(int n)
- {
- random_shuffle(p+1,p+n+1);//随机排序取点
- int i,j,k;
- Circle tep;
- tep.center=p[1];
- tep.r=0;
- for(i=2;i<=n;i++)
- {
- if(Len(p[i],tep.center)>tep.r+eps)
- {
- tep.center=p[i];
- tep.r=0;
- for(j=1;j<i;j++)
- {
- if(Len(p[j],tep.center)>tep.r+eps)
- {
- tep.center.x=(p[i].x+p[j].x)/2;
- tep.center.y=(p[i].y+p[j].y)/2;
- tep.r=Len(p[i],p[j])/2;
- for(k=1;k<j;k++)
- {
- if(Len(p[k],tep.center)>tep.r+eps)
- {
- Triangle t;
- t.v[0]=p[i];
- t.v[1]=p[j];
- t.v[2]=p[k];
- tep=CircleOfTriangle(t);
- }
- }
- }
- }
- }
- }
- printf("%.2lf %.2lf %.2lf\n",tep.center.x,tep.center.y,tep.r);
- }
- int main()
- {
- int n,i;
- while(scanf("%d",&n),n)
- {
- for(i=1;i<=n;i++)
- scanf("%lf%lf",&p[i].x,&p[i].y);
- Run(n);
- }
- }
方法二:模拟退火法:(hdu3932)
- #include"string.h"
- #include"stdio.h"
- #include"queue"
- #include"stack"
- #include"vector"
- #include"algorithm"
- #include"iostream"
- #include"math.h"
- #include"stdlib.h"
- #define M 1009
- #define inf 100000000
- #define eps 1e-8
- #define PI acos(-1.0)
- using namespace std;
- double X,Y;
- int n;
- struct Point
- {
- double x,y,dis;
- Point(){}
- Point(double xx,double yy){x=xx;y=yy;}
- bool check(){
- if(x>0&&y>0&&x<X&&y<Y)return true;
- return false;
- }
- }p[M],q[50];
- double pow(double x)
- {
- return x*x;
- }
- double Len(Point a,Point b)
- {
- return sqrt(pow(a.x-b.x)+pow(a.y-b.y));
- }
- double fun(Point a)
- {
- double maxi=0;
- for(int i=1;i<=n;i++)
- {
- double L=Len(a,p[i]);
- if(maxi<L)
- maxi=L;
- }
- return maxi;
- }
- int main()
- {
- int i;
- while(scanf("%lf%lf%d",&X,&Y,&n)!=EOF)
- {
- for(i=1;i<=n;i++)
- scanf("%lf%lf",&p[i].x,&p[i].y);
- int po=15,est=15;
- for(int i=1;i<=po;i++)
- {
- q[i].x=(rand()%1000+1)/1000.0*X;
- q[i].y=(rand()%1000+1)/1000.0*Y;
- q[i].dis=fun(q[i]);
- }
- double temp=max(X,Y);
- while(temp>0.001)
- {
- for(int i=1;i<=po;i++)
- {
- for(int j=1;j<=est;j++)
- {
- double rad=(rand()%1000+1)/1000.0*PI*2;
- Point cur;
- cur.x=q[i].x+temp*cos(rad);
- cur.y=q[i].y+temp*sin(rad);
- if(!cur.check())continue;
- cur.dis=fun(cur);
- if(cur.dis<q[i].dis)
- q[i]=cur;
- }
- }
- temp*=0.8;
- }
- int id=1;
- for(int i=1;i<=po;i++)
- if(q[id].dis>q[i].dis)
- id=i;
- printf("(%.1lf,%.1lf).\n%.1lf\n",q[id].x,q[id].y,q[id].dis);
- }
- return 0;
- }
最小圆覆盖(随机增量法&模拟退火法)的更多相关文章
- 洛谷 P1742 最小圆覆盖 (随机增量)
题目链接:P1742 最小圆覆盖 题意 给出 N 个点,求最小的包含所有点的圆. 思路 随机增量 最小圆覆盖一般有两种做法:随机增量和模拟退火.随机增量的精确度更高,这里介绍随机增量的做法. 先将所有 ...
- 【bzoj1336/1337/2823】[Balkan2002]Alien最小圆覆盖 随机增量法
题目描述 给出N个点,让你画一个最小的包含所有点的圆. 输入 先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000. ...
- 【BZOJ1336】[Balkan2002]Alien最小圆覆盖 随机增量法
[BZOJ1336][Balkan2002]Alien最小圆覆盖 Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=10000 ...
- BZOJ.2823.[AHOI2012]信号塔(最小圆覆盖 随机增量法)
BZOJ 洛谷 一个经典的随机增量法,具体可以看这里,只记一下大体流程. 一个定理:如果一个点\(p\)不在点集\(S\)的最小覆盖圆内,那么它一定在\(S\bigcup p\)的最小覆盖圆上. 所以 ...
- [BZOJ2823][BZOJ1336][BZOJ1337]最小圆覆盖(随机增量法)
算法介绍网上有很多,不解释了. 给出三点坐标求圆心方法:https://blog.csdn.net/liyuanbhu/article/details/52891868 记得先random_shuff ...
- hdu 3007【最小圆覆盖-随机增量法模板】
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> usin ...
- BZOJ1336 Balkan2002 Alien最小圆覆盖 【随机增量法】*
BZOJ1336 Balkan2002 Alien最小圆覆盖 Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=100000, ...
- 【BZOJ-1336&1337】Alie最小圆覆盖 最小圆覆盖(随机增量法)
1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 1573 ...
- [BZOJ 1336] [Balkan2002] Alien最小圆覆盖 【随机增量法】
题目链接:BZOJ - 1336 题目分析 最小圆覆盖有一个算法叫做随机增量法,看起来复杂度像是 O(n^3) ,但是可以证明其实平均是 O(n) 的,至于为什么我不知道= = 为什么是随机呢?因为算 ...
随机推荐
- 浅谈 JavaScript 编程语言的编码规范
对于熟悉 C/C++ 或 Java 语言的工程师来说,JavaScript 显得灵活,简单易懂,对代码的格式的要求也相对松散.很容易学习,并运用到自己的代码中.也正因为这样,JavaScript 的编 ...
- 德国Aptamil不同系列奶粉间差别
以下内容均来源网络整理.汇总. 德国人做事严谨,而且对于有争议性的成分持保守态度,比如不添加麦芽糊精.所以我比较赞赏购买德国的奶粉,主要是aptamil和hipp喜宝,这两个牌子也基本没有负面新闻.但 ...
- 【转】C# 调用WebService的方法
很少用C#动态的去调用Web Service,一般都是通过添加引用的方式,这样的话是自动成了代理,那么动态代理调用就是我们通过代码去调用这个WSDL,然后自己去生成客户端代理.更多的内容可以看下面的两 ...
- imx6 fec分析
/***************************************************************************** * imx6 fec分析 * 本文主要分析 ...
- Spring RestTemplate 小结
关于RestTemplate 首先,你可以把它理解为一个发起请求并接收响应的工具类(功能类似浏览器). 其次,它其实是一个壳,具体还是通过调用别的接口来实现(如jdk自带的连接,或者HttpClien ...
- C++ 继承、函数重载
题外话1:浪费了两天,可耻! 题外话2:你这个年纪,做得好是理所当然,做不好是罪孽深重!!! --- 深以为然. 题外话3:从开始看C++ Primer 到现在,整整24天了,没想到基础方面耗费这么久 ...
- wampserver下配置虚拟主机 实现多站点支持
wampserver下配置虚拟主机 实现多站点支持 wampserver是由德国人开发,在windows环境下实现apache+php+mysql的一款集成环境软件,wampserver自带SQLit ...
- php header函数下载文件实现代码
在php中header函数的使用很大,header不但可以向客户端发送原始的 HTTP 报头信息,同时还可以直接实现文件下载操作 header函数最常用的不是用于下载而是用于发送http类的 跳转 它 ...
- 一键部署MongoDB集群Windows版
由于周末在家手头没有虚拟机,所以在windows下单机完毕部署要求,并编写bat执行脚本. 1.创建配置文件及相关文件夹 总的启动脚本例如以下(startmc.bat): rem m1 start m ...
- perl 脚本将phred33 转换为phred64
今天用fastx_tookit 时遇到问题, 我的fastq 文件的碱基质量值格式为phred33, 而fastq_tookit 默认碱基质量值的格式为phred64, 所以报错了,提示我的fastq ...