题意:求平面上一个点,使其到给定的n个点的距离和最小,即费马点。

模拟退火的思想是随机移动,然后100%接受更优解,以一定概率接受更劣解。移动的过程中温度缓慢降低,接受更劣解的概率降低。

在网上看到的代码都不太靠谱,我这个代码的关键之处在于,每一次随机走点时,不是1次,而是在10次随机中取最优者作为当前这一步的随机结果,这样运行时非常优秀。

T降温时乘0.9/0.99这样的数都行,越接近1越准确,但速度越慢。

这份代码即使用0.9也可以ac。

#include<cstdio>
#include<cmath>
#include<cstdlib>
using namespace std;
const double EPS=0.00000001;
const double PI=acos(-1.0);
struct Point{
double x,y;
Point(const double &x,const double &y){
this->x=x;
this->y=y;
}
Point(){}
void read(){
scanf("%lf%lf",&x,&y);
}
double length(){
return sqrt(x*x+y*y);
}
}a[105],p;
double ans;
int n;
typedef Point Vector;
Vector operator - (const Point &a,const Point &b){
return Vector(a.x-b.x,a.y-b.y);
}
Vector operator + (const Vector &a,const Vector &b){
return Vector(a.x+b.x,a.y+b.y);
}
Vector operator * (const double &K,const Vector &v){
return Vector(K*v.x,K*v.y);
}
double calc(Point p){
double res=0.0;
for(int i=1;i<=n;++i){
res+=(a[i]-p).length();
}
return res;
}
int main(){
srand(233);
// freopen("poj2420.in","r",stdin);
// freopen("poj2420.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;++i){
a[i].read();
p.x+=a[i].x;
p.y+=a[i].y;
}
p.x/=(double)n;
p.y/=(double)n;
ans=calc(p);
double T=100000.0;
while(T>EPS){
double bestnow=10000000.0;
Point besttp;
for(int i=1;i<=10;++i){
double rad=(double)(rand()%10000+1)/10000.0*2.0*PI;
Point tp=p+T*Point(cos(rad),sin(rad));
double now=calc(tp);
if(now<bestnow){
bestnow=now;
besttp=tp;
}
}
if(bestnow<ans || exp((ans-bestnow)/T)*10000.0>(double)(rand()%10000)){
ans=bestnow;
p=besttp;
}
T*=0.99;
}
printf("%.0f\n",ans);
return 0;
}

【模拟退火】poj2420 A Star not a Tree?的更多相关文章

  1. poj2420 A Star not a Tree? 找费马点 模拟退火

    题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...

  2. poj-2420 A Star not a Tree?(模拟退火算法)

    题目链接: A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5219   Accepte ...

  3. 模拟退火算法A Star not a Tree?(poj2420)

    http://write.blog.csdn.net/postedit A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  4. Poj2420 A Star not a Tree? 模拟退火算法

    题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...

  5. POJ-2420 A Star not a Tree? 梯度下降 | 模拟退火

    题目链接:https://cn.vjudge.net/problem/POJ-2420 题意 给出n个点,找一个点,使得这个点到其余所有点距离之和最小. 思路 一开始就在抖机灵考虑梯度下降,猜测是个凸 ...

  6. [POJ2420]A Star not a Tree?(模拟退火)

    题目链接:http://poj.org/problem?id=2420 求费马点,即到所有其他点总和距离最小的点. 一开始想枚举一个坐标,另一个坐标二分的,但是check的时候还是O(n)的,复杂度相 ...

  7. poj2420 A Star not a Tree? 模拟退火

    题目大意: 给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\)) 题解 模拟退火上 #include <cmath> #include <cstdio&g ...

  8. [POJ2420]A Star not a Tree?

    来源: Waterloo Local 2002.01.26 题目大意: 找出$n$个点的费马点. 思路: 模拟退火. 首先任取其中一个点(或随机一个坐标)作为基准点,每次向四周找距离为$t$的点,如果 ...

  9. [模拟退火][UVA10228] A Star not a Tree?

    好的,在h^ovny的安利下做了此题 模拟退火中的大水题,想当年联赛的时候都差点打了退火,正解貌似是三分套三分,我记得上一道三分套三分的题我就是退火水过去的... 貌似B班在讲退火这个大玄学... 这 ...

随机推荐

  1. 【ALB学习笔记】基于多线程方式的串行通信接口数据接收案例

    基于多线程方式的串行通信接口数据接收案例 广东职业技术技术学院  欧浩源 1.案例背景 在本博客的<[CC2530入门教程-06]CC2530的ADC工作原理与应用>中实现了电压数据采集的 ...

  2. 【shell】shell编程(六)-shell函数的应用

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; [return ...

  3. 安全测试===sqlmap(零)转载

    本文转自:https://blog.werner.wiki/sqlmap-study-notes-0/ 感谢作者的整理,如有侵权,立删 零.前言 这篇文章是我学习Sqlmap的用法时做的笔记,记录了S ...

  4. OPENSOLARIS source

    http://blog.csdn.net/nemo2011/article/details/8543220 http://fxr.watson.org/fxr/source/?v=OPENSOLARI ...

  5. openfire在内网的情况下 文件传输代理的设置

    openfire在内网的情况下 文件传输代理的设置 http://blog.csdn.net/v6543210/article/details/22506565

  6. Makefile系列之三 : 变量

    一.变量的基础 变量在声明时需要给予初值,而在使用时,需要给在变量名前加上“$”符号,但最好用小括号“()”或是大括号“{}”把变量给包括起来.如果你要使用真实的“$”字符,那么你需要用“$$”来表示 ...

  7. Dev的js智能提示

    VS中"工具"->"选项"->"文本编辑器"->"JavaScript"->"Inte ...

  8. html5重力感应事件之DeviceMotionEvent

    前言 今天主要介绍一下html5重力感应事件之DeviceMotionEvent,之前我的一篇文章http://www.haorooms.com/post/jquery_jGestures, 介绍了第 ...

  9. Can't load standard profile: GRAY.pf

    报错: java.lang.IllegalArgumentException: Can't load standard profile: GRAY.pf at java.awt.color.ICC_P ...

  10. highcharts 折线,饼状,条状综合图

    完整代码如下: <head> <meta http-equiv="Content-Type" content="text/html; charset=u ...