题意:求平面上一个点,使其到给定的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. Computer(HDU2196+树形dp+树的直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 题目: 题意:有n台电脑,每台电脑连接其他电脑,第i行(包括第一行的n)连接u,长度为w,问你每 ...

  2. Django rest framework 的认证流程(源码分析)

    一.基本流程举例: urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^users/', views.HostView.as_view() ...

  3. 直接在注册DB服务的时候,做beforeQuery事件监听

  4. 海量数据排序——如果有1TB的数据需要排序,但只有32GB的内存如何排序处理?

    转载:https://blog.csdn.net/fx677588/article/details/72471357 1.外排序  传统的排序算法一般指内排序算法,针对的是数据可以一次全部载入内存中的 ...

  5. centos rar 文件打开办法

    http://hi.baidu.com/nmxiaoxin/item/7642a139918a95677d034b6a Centos下解压rar.zip文件的方法 ============zip文件的 ...

  6. 生命周期(vue的钩子函数)

    生命周期图示 创建前,创建后,挂载前,挂载后,更新前,更新后,销毁前,销毁后 beforeCreate:function(){ console.log('1-beforeCreate 组件还未被创建' ...

  7. atom编辑器插件atom-ternjs

    这是官方文档:https://atom.io/packages/atom-ternjs 官方介绍: JavaScript code intelligence for atom with Tern. A ...

  8. yum安装的Apache的各种配置文件的位置

    //配置文件 /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.d/README /etc/httpd/conf.d/proxy_ajp.conf / ...

  9. visualvm监控远程机器上的Java程序

    源文:http://hanwangkun.iteye.com/blog/1195526

  10. 5:django 常用函数

    用django写view函数的时候,我们常常用到django.shortcuts里面的很多常用函数, 这节我们来看看这些函数的具体用法吧 render render(request, template ...