POJ1379:Run Away
我对模拟退火的理解: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的更多相关文章
- Spring Boot Maven Plugin(二):run目标
简介 Spring Boot Maven Plugin插件提供spring boot在maven中的支持.允许你打包可运行的jar包或war包. 插件提供了几个maven目标和Spring Boot ...
- Spring cloud的Maven插件(二):run目标
简介 Spring Boot Maven Plugin插件提供spring boot在maven中的支持.允许你打包可运行的jar包或war包. 插件提供了几个maven目标和Spring Boot ...
- 解决apscheduler报错:Run time of job …… next run at: ……)” was missed by
在Django中使用apscheduler django_apscheduler 实现定时任务, 来完成数据拉取. 一段时间后发现数据量对不上,遂查日志 发现报错如下: Run time of job ...
- 解决linux系统启动之:unexpected inconsistency:RUN fsck
现象: 虚拟机在启动过程中提示: unexpected inconsistency;RUN fsck MANUALLY 原因分析: 1.由于意外关机导致的文件系统问题 解决方法: 方法1: 输入ROO ...
- SVN错误:run 'cleanup' if it was interrupted的解决
原文转自:http://www.lxway.com/812960411.htm 今天碰到了个郁闷的问题,svn执行clean up命令时报错“Previous operation has not fi ...
- Eclipse调试:Run on server 和 Debug on server 区别
Run on server: 以正常模式运行程序.会直接把程序从头到尾执行一遍,运行完就结束,不会进入到源代码里面(即使源代码中设置了断点). Debug on server: 以调试模式运行程序,或 ...
- 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 ...
- 错误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并没有处理空内容的逻辑,所以 ...
- eShopOnContainers 知多少[2]:Run起来
环境准备 Win10(开启Hyper-V) .NET Core SDK Docker for Windows VS2017 or VS Code Git SQL Server Management S ...
随机推荐
- PHP自定义函数: 下载远程文件
function httpcopy($url, $file="", $timeout=60) { $file = empty($file) ? pathinfo($url,PATH ...
- spark在yarn-cluster上面执行报错
在单机模式下执行成功的spark程序,在yarn上面就报错.异常信息如下: // :: INFO DAGScheduler: Completed ResultTask(, ) // :: INFO D ...
- Linux基础系列:常用命令(8)_shell script
一.什么是shell script 将OS命令堆积到可执行的文件里,由上至下的顺序执行文本里的OS命令 就是脚本了. 再加上些智能(条件/流控)控制,就变成了智能化脚本了 二.变量命名规则 以字母或下 ...
- LeetCode:删除排序链表中的重复元素【83】
LeetCode:删除排序链表中的重复元素[83] 题目描述 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示 ...
- 模仿jquery框架源码---网络
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Page& ...
- 3.15课·········out传值(传址)
public void Hs(out int a, out int b) { a = 4; b = 6; a = b++;//a=6,b=b+1=7//b先赋值给a,然后b+1 b = ++a;//a ...
- PAT 天梯赛 L2-003. 月饼 【贪心】
题目链接 https://www.patest.cn/contests/gplt/L2-003 思路 用贪心思路 最后注意一下 总售价有可能是浮点数 AC代码 #include <cstdio& ...
- Caffe学习系列(三)Docker安装及一些问题的记录
前言: Docker安装倒是很简单,按照步骤轻松完成,但是在联网方面还是出现问题,大概是伟大的祖国防火墙将其拦下,但在开发中要遇山开山,见水搭桥.在其中我将解决方法记录下来,每次解决了困难想分享找不到 ...
- Android关闭USB的ADB调试和文件传输功能(禁用USB)【转】
本文转载自:https://blog.csdn.net/jun4331247/article/details/51201825 通过设置系统属性(System Property)[persist.sy ...
- POJ 1183 反正切函数的应用
H - 反正切函数的应用 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...