poj1379
poj1379
题意
给出 n 个洞的坐标,要求找到一点使得这一点距离最近洞的距离最远。
分析
通过这道题学习一下模拟退火算法,
这种随机化的算法,在求解距离且精度要求较小时很有用。
简而言之,由随机选取的多个初始点,进行多次的随机变换,并根据是否更优而选择是否保留答案,
那么首先要选择两个值,delta 表示初始最大变换的半径(即对应初始的火温),d 控制半径缩小的快慢(火温减小的快慢)。
这道题,比较直白,随机变换就是改变坐标的位置(通过半径和角度就可以变换坐标,当然角度也是随机的),若变换后距所有点的距离增大了那么则更优。
code
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<ctime>
using namespace std;
const int MAXN = 1005;
const int MAXP = 20; // 随机选取 20 个点
const double PI = acos(-1.0);
double X, Y;
int n;
double dis[100];
struct P
{
double x, y;
P() {}
P(double _x, double _y) : x(_x), y(_y) {}
P operator - (P p)
{
return P(x - p.x, y - p.y);
}
double dis()
{
return sqrt(x * x + y * y);
}
}a[MAXN], ans[MAXN];
double getMinDis(P p)
{
double as = 1e9;
for(int i = 0; i < n; i++)
{
as = min(as, (p - a[i]).dis());
}
return as;
}
P SA()
{
for(int i = 0; i < MAXP; i++)
{
ans[i].x = (rand() % 1000 + 1) / 1000.0 * X;
ans[i].y = (rand() % 1000 + 1) / 1000.0 * Y;
dis[i] = getMinDis(ans[i]);
}
double delta = max(X, Y) / sqrt(n * 1.0), d = 0.9;
while(delta > 0.01) // 边界最小值
{
for(int i = 0; i < MAXP; i++)
{
for(int j = 0; j < 20; j++)
{
P t = ans[i];
double angle = rand() % 1000 / 1000.0 * 2 * PI;
t.x += delta * cos(angle) * (rand() % 1000 / 1000.0);
t.y += delta * sin(angle) * (rand() % 1000 / 1000.0);
if(t.x < 0 || t.x > X || t.y < 0 || t.y > Y) continue;
double tmp = getMinDis(t);
if(tmp > dis[i])
{
dis[i] = tmp;
ans[i] = t;
}
}
}
delta *= d;
}
double dd = 0;
int pp = 0;
for(int i = 0; i < MAXP; i++)
{
if(dis[i] > dd)
{
dd = dis[i];
pp = i;
}
}
return ans[pp];
}
int main()
{
srand(time(0));
int T;
for(scanf("%d", &T); T--;)
{
scanf("%lf%lf%d", &X, &Y, &n);
for(int i = 0; i < n; i++)
{
scanf("%lf%lf", &a[i].x, &a[i].y);
}
P p = SA();
printf("The safest point is (%.1f, %.1f).\n", p.x, p.y);
}
return 0;
}
poj1379的更多相关文章
- 【模拟退火】poj1379 Run Away
题意:平面上找一个点,使得其到给定的n个点的距离的最小值最大. 模拟退火看这篇:http://www.cnblogs.com/autsky-jadek/p/7524208.html 这题稍有不同之处仅 ...
- poj1379 模拟退火
题意:和上题一样...就是把最小值换成了最大值.. ref:http://www.cppblog.com/RyanWang/archive/2010/01/21/106112.html #includ ...
- 模拟退火算法(run away poj1379)
http://poj.org/problem?id=1379 Run Away Time Limit: 3000MS Memory Limit: 65536K Total Submissions: ...
- poj1379 Run Away
传送门:http://poj.org/problem?id=1379 [题解] 题目大意:求(0,0)->(X,Y)内的一个点,使得这个点到给定的n个点的最小距离最大. 模拟退火 一开始可以先把 ...
- poj-1379 Run Away(模拟退火算法)
题目链接: Run Away Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7982 Accepted: 2391 De ...
- POJ1379:Run Away
我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...
- poj-2420 A Star not a Tree?(模拟退火算法)
题目链接: A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5219 Accepte ...
- poj 2420 模拟退火法基础
/* 题意:给n个电脑,求一个点到这n个电脑的距离和最小. 模拟退火法:和poj1379的方法类似 因为坐标范围是0-10000 不妨把它看成是10000*10000的正方形来做 */ #includ ...
随机推荐
- AspNetPager 分页的详细用法(ASP.NET)
1.[添加AspNetPager.dll文件] 2.[使用方法] public static DataTable GetRecord(SystemModel.Pager mt, ref int Tot ...
- C++ 中关于optional 使用过程中遇到的问题
头文件:#include <boost/optional.hpp> using namespace boost; optional很像一个仅能存放一个元素的容器,它实现了"未初始 ...
- js中将yyyy-MM-dd格式的日期转换
1.转换为yyyy年MM月dd日 var str = "2017-02-16"; var reg =/(\d{4})\-(\d{2})\-(\d{2})/; var date = ...
- JAVA压缩图片 服务器端压缩图片 原创
@RequestMapping(value = "/uploadImage", method = RequestMethod.POST) @ResponseBody public ...
- XStream的使用
一:功能 可以将JavaBean转换(序列化)成XMl 二:依赖jar包 xstream.jar xpp3_min.jar(xml pull parser)xml解析器 三:使用步骤 XStream ...
- C++ STL学习之容器set和multiset (补充材料)
一.set和multiset基础 set和multiset会根据特定的排序准则,自动将元素进行排序.不同的是后者允许元素重复而前者不允许. 需要包含头文件: #include <set> ...
- 图片裁剪(cropper)后上传问题
最近工作需要处理头像裁剪以及上传,研究了几天,写点心得,提醒自己记住踩过的坑,能帮助别人当然更好. 功能基本就是这样: 这里需要注意的是:拿到需求后,不要急于直接上手,花费半个小时,甚至更长时间缕清整 ...
- mui开发app之联网应用传输数据
手机的app分为,在线和单机,在线就是类似于C/S模式,能与服务器与他人共享数据的程序,单机就是在没有网络下可以玩转的app. 目前互联网盛行的时代,99%的程序都是联网环境下工作的.那么如何开发本地 ...
- hdu2767强连通加缩点
https://vjudge.net/contest/156688#problem/B 题目说了一大堆,前面的没有用,就是让你判断要加几条边才能强连通,用到缩点的知识 二重循环,判断邻接表下一个点是不 ...
- TP5学习基础一:增删改查小demo
①TP5--增删改查简单的demo 我先吐槽一下:因为工作需要研究tp5,去官网看了一下哎呦,资源挺多挺全啊!然后下载唯一免费的官方教程,我曹pdf打开533页.讲的很细但是开发能等看完才做吗?看到精 ...