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 ...
随机推荐
- 九度OJ 1196:成绩排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4339 解决:1476 题目描述: 用一维数组存储学号和成绩,然后,按成绩排序输出. 输入: 输入第一行包括一个整数N(1<=N< ...
- nginx学习之web服务器(四)
1. 定义一个虚拟服务器 http { server { # Server configuration } } 可以在http {}块里面添加多个server {}块,每一个server {}块代表一 ...
- Data Decisions: DSP vs. DMP
http://www.cmo.com/features/articles/2016/3/9/data-decisions-dsp-vs-dmp.html As marketers assess the ...
- R语言编写乘法表
for(i in 1:9){ for(j in 1:i){ m = j*i cat(i,'*',j,'=',m,' ') } cat('\n') } 1 * 1 = 1 2 * 1 = 2 2 * 2 ...
- 4django模板
在前面的几节中我们都是用简单的django.http.HttpResponse来把内容显示到网页上,本节将讲解如何使用渲染模板的方法来显示内容 1.创建一个 zqxt_tmpl 项目,和一个 名称为 ...
- CSS选择器(三)
九.CSS 伪类 (Pseudo-classes) CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪类 (Pseudo-classes)实例: 1.向文档中的超链接添加不同的颜色. <s ...
- Java InetAddress.getByAddress()的使用
import java.net.*; public class NetDemo { public static void main(String[] args) throws Exception{ S ...
- 【leetcode刷题笔记】Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- ICP 求解相机思路
1.之前仍然是需要创建find_feature_matches,和pixel2cam,一个是用来匹配描述子的,一个是把像素坐标转成归一化平面坐标的.里面的变量都要带上&.2.因为是3d-3d. ...
- Oracle的权限管理
--授予系统权限语法 grant system_privilege | all privileges --可以设置select any dictionary权限之外的索引系统权限 to {user i ...