我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html

我对爬山的理解:https://www.cnblogs.com/AKMer/p/9555215.html

题目传送门:http://poj.org/problem?id=1379

题目意思是要求规定大小平面内某一点,该点到所有给定点的距离中最小距离最大。

类似于费马点做法……不过把统计距离和变成了求距离最小值最大。

费马点不会的可以先看看这篇博客。

BZOJ3680:吊打XXXhttps://www.cnblogs.com/AKMer/p/9588724.html

时间复杂度:\(O(能A)\)

空间复杂度:\(O(能A)\)

爬山算法代码如下:

#include <cmath>
#include <ctime>
#include <cstdio>
#include <algorithm>
using namespace std; #define sqr(a) ((a)*(a)) int n,lenx,leny;
double ansx,ansy,ans; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} struct point {
double x,y;
}p[1001]; double len() {
double x=rand()%200000-100000;
return x/100000;
} double dis(double x1,double y1,double x2,double y2) {
return sqrt(sqr(x1-x2)+sqr(y1-y2));
} double calc(double x,double y) {
double tmp=1e18;
for(int i=1;i<=n;i++)
tmp=min(tmp,dis(x,y,p[i].x,p[i].y));//求最小距离最大
if(tmp>ans) {ans=tmp;ansx=x,ansy=y;}
return tmp;
} void climbhill() {
double now_x=ansx,now_y=ansy;
for(double T=1e7;T>=1e-7;T*=0.998) {
double nxt_x=now_x+len()*T;
double nxt_y=now_y+len()*T;
if(nxt_x<0||nxt_x>lenx||nxt_y<0||nxt_y>leny)continue;
if(calc(now_x,now_y)<calc(nxt_x,nxt_y))
now_x=nxt_x,now_y=nxt_y;
}
} int main() {
srand(time(0));
int T=read();
while(T--) {
lenx=read(),leny=read(),n=read();
ans=-1e18;ansx=lenx/2;ansy=leny/2;//起始点设在平面中央
for(int i=1;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
climbhill();
printf("The safest point is (%.1lf, %.1lf).\n",ansx,ansy);
}
return 0;
}

模拟退火代码如下:

#include <cmath>
#include <ctime>
#include <cstdio>
#include <algorithm>
using namespace std; #define sqr(x) ((x)*(x)) const double T_0=1e-7,del_T=0.998; int lenx,leny,n;
double ansx,ansy,ans; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} struct point {
double x,y;
}p[1001]; double len() {
double x=rand()%200000-100000;
return x/100000;
} double dis(double x1,double y1,double x2,double y2) {
return sqrt(sqr(x1-x2)+sqr(y1-y2));
} double calc(double x,double y) {
double tmp=1e18;
for(int i=1;i<=n;i++)
tmp=min(tmp,dis(x,y,p[i].x,p[i].y));
if(tmp>ans) {ans=tmp;ansx=x;ansy=y;}
return tmp;
} void Anneal() {
double T=1e7,now_x=ansx,now_y=ansy;
while(T>=T_0) {
double nxt_x=now_x+len()*T;
double nxt_y=now_y+len()*T;
if(nxt_x<0||nxt_x>lenx||nxt_y<0||nxt_y>leny) {
T*=del_T;continue;//不这么写直接卡死循环了……
}
double tmp1=calc(now_x,now_y);
double tmp2=calc(nxt_x,nxt_y);
if(tmp2>tmp1||exp((tmp2-tmp1)/T)*RAND_MAX>rand())
now_x=nxt_x,now_y=nxt_y;
T*=0.998;
}
} int main() {
srand(time(0));
int T=read();
while(T--) {
lenx=read(),leny=read(),n=read();
ans=-1e18;ansx=lenx/2;ansy=leny/2;
for(int i=1;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
Anneal();
printf("The safest point is (%.1lf, %.1lf).\n",ansx,ansy);
}
return 0;
}

程序仅供参考

POJ1379:Run Away的更多相关文章

  1. Spring Boot Maven Plugin(二):run目标

    简介 Spring Boot Maven Plugin插件提供spring boot在maven中的支持.允许你打包可运行的jar包或war包. 插件提供了几个maven目标和Spring Boot ...

  2. Spring cloud的Maven插件(二):run目标

    简介 Spring Boot Maven Plugin插件提供spring boot在maven中的支持.允许你打包可运行的jar包或war包. 插件提供了几个maven目标和Spring Boot ...

  3. 解决apscheduler报错:Run time of job …… next run at: ……)” was missed by

    在Django中使用apscheduler django_apscheduler 实现定时任务, 来完成数据拉取. 一段时间后发现数据量对不上,遂查日志 发现报错如下: Run time of job ...

  4. 解决linux系统启动之:unexpected inconsistency:RUN fsck

    现象: 虚拟机在启动过程中提示: unexpected inconsistency;RUN fsck MANUALLY 原因分析: 1.由于意外关机导致的文件系统问题 解决方法: 方法1: 输入ROO ...

  5. SVN错误:run 'cleanup' if it was interrupted的解决

    原文转自:http://www.lxway.com/812960411.htm 今天碰到了个郁闷的问题,svn执行clean up命令时报错“Previous operation has not fi ...

  6. Eclipse调试:Run on server 和 Debug on server 区别

    Run on server: 以正常模式运行程序.会直接把程序从头到尾执行一遍,运行完就结束,不会进入到源代码里面(即使源代码中设置了断点). Debug on server: 以调试模式运行程序,或 ...

  7. centos7运行yum报如下提示:Run "yum repolist all" to see the repos you have

    centos7运行yum报如下提示: There are no enabled repos. Run "yum repolist all" to see the repos you ...

  8. 错误ERROR datanode.DataNode (DataXceiver.java:run(278)) - hadoop07:50010DataXceiver error processing unknown operation src:127.0.0.136479 dst:127.0.0.150010

    原因: Ambari 每分钟会向datanode发送"ping"连接一下去确保datanode是正常工作的.否则它会触发alert.但是datanode并没有处理空内容的逻辑,所以 ...

  9. eShopOnContainers 知多少[2]:Run起来

    环境准备 Win10(开启Hyper-V) .NET Core SDK Docker for Windows VS2017 or VS Code Git SQL Server Management S ...

随机推荐

  1. 记录-项目java项目框架搭建的一些问题(maven+spring+springmvc+mybatis)

    伴随着项目框架的落成后,本以为启动就能成功的,but.... 项目启动开始报错误1:java.lang.ClassNotFoundException: org.springframework.web. ...

  2. Xshell调节字体大小和样式

    有时候没有看着字体太小的,好难受, 调节字体大小: ALT+P快捷键打开

  3. ls --color=xxx

      默认的ls是由"ls --color=auto"组成的,假如某个目录中的文件特别多,我不希望显示颜色(可以加快显示),那就需要指定单独的参数. [root@localhost ...

  4. 京东android面试题(2018 顶级互联网公司面试题系列)

    以下来自于北京的一个兄弟的面试题 1.静态内部类和非静态内部类有什么区别  2.谈谈你对java多态的理解  3.如何开启线程,run和runnable有什么区别  4.线程池的好处  5.说一下你知 ...

  5. CSS 布局实例系列(四)如何实现容器中每一行的子容器数量随着浏览器宽度的变化而变化?

    Hello,小朋友们,还记得我是谁吗?对了,我就是~超威~好啦,言归正传,今天的布局实例是: 实现一个浮动布局,红色容器中每一行的蓝色容器数量随着浏览器宽度的变化而变化,就如下图: 肯定有人心里犯嘀咕 ...

  6. Card Collector(期望+min-max容斥)

    Card Collector(期望+min-max容斥) Card Collector woc居然在毫不知情的情况下写出一个min-max容斥 题意 买一包方便面有几率附赠一张卡,有\(n\)种卡,每 ...

  7. 聊聊数据库~5.SQL运维上篇

    1.6.SQL运维篇 运维这块逆天只能说够用,并不能说擅长,所以这篇就当抛砖之用,欢迎补充和纠错 PS:再说明下CentOS优化策略这部分的内容来源:首先这块逆天不是很擅长,所以主要是参考网上的DBA ...

  8. ABAP下载服务器文件到本机

    转自http://blog.sina.com.cn/s/blog_701594f40100l8ml.html ABAP:下载服务器文件到本机 对服务器的文件进行读写操作,SAP提供了OPEN DATA ...

  9. Linux expect介绍和用法

    expect时用与提供自动交互的工具.比如如果想要用ssh登陆服务器,每次都输入密码你觉得麻烦,那你就可以使用expect来做自动交互,这样的话就不用每次都输入密码了. 先看例子: #!/usr/bi ...

  10. MYSQL:基础——3N范式的表结构设计

    基于3N范式的数据表设计 范式 设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不同的规范要求被称为不同的范式,各种范式呈递次规范,越高的范式数据库冗余越小. 关系数据库现有六种范 ...