我对模拟退火的理解: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. python的进程和线程

    关于进程: An executing instance of a program is called a process.程序的执行实例称为进程. Each process provides the ...

  2. Apache转发规则的一点注意

    RewriteRule ^studio/$ book.php?mod=studio 这种目录转发, 正常情况下是没问题的. 但是当根目录下存在一个 studio 目录时, apache就不会转发URL ...

  3. 洛谷 1641 [SCOI2010]生成字符串

    题目戳这里 一句话题意 求\(C_{m+n}^{m}\)-\(C_{m+n}^{m-1}\) Solution 巨说这个题目很水 标签居然还有字符串? 但是我还不很会用逆元真的太菜了,还好此题模数P为 ...

  4. mysql(root用户密码设置)

    root密码重置 修改root用户的密码: /*登录mysql*/ mysql -uroot -p123 /*切换数据库*/ use mysql /*修改root用户的密码*/ update user ...

  5. spring启动quartz定时器

    在很多中经常要用到定时任务,quartz是定时器中比较好用的,在Spring中使用quartz是很容易的事情,首先在spring的applicationContext.xml文件中增加如下配置: &l ...

  6. lazyload.js参数说明

    lazyload.js是jQuery的一个插件,可以用来实现图片异步加载. lazyload插件如何添加参数: $("img").lazyload({ //参数添加到此位置,建议一 ...

  7. 保持linux下保持ssh不断线

    用ssh链接服务端,一段时间不操作或屏幕没输出(比如复制文件)的时候,会自动断开,有两种解决办法: 1.在客户端配置 #vi  /etc/ssh/ssh_config(注意不是/etc/ssh/ssh ...

  8. 基于CocoaPods的iOS项目模块化实践

    什么是CocoaPods? CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has ove ...

  9. [原创] hadoop学习笔记:wordcout程序实践

    看了官网上的示例:但是给的不是很清楚,这里依托官网给出的示例,加上自己的实践,解析worcount程序的操作 1.首先你的确定你的集群正确安装,并且启动你的集群,应为这个是hadoop2.6.0,所以 ...

  10. Python 3 mysql 表操作

    Python 3 mysql 表操作 表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段 id,name,qq,age称为字段,其余的,一行内容称为 ...