Groundhog Build Home - HDU - 3932(模拟退火)
题意
给定一个矩形内的\(n\)个点,在矩形中找一个点,离其他点的最大距离最小。
题解
模拟退火。
这个题需要\(x\)和\(y\)坐标随机动的时候多随机几次。否则就WA了。另外由于随机多次,如果温度变化率太小,就会TLE。
代码
//#include <bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#define FOPI freopen("in.txt", "r", stdin)
#define FOPO freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
const int maxn = 1000 + 5;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const double start_T = 20000;
struct Point
{
double x, y, z;
Point() {}
Point(double _x, double _y, double _z = 0):x(_x), y(_y), z(_z) {}
}a[maxn];
int dx[] = {1, 1, 1, -1, -1, -1, 0, 0},
dy[] = {0, 1, -1, 0, 1, -1, -1, 1};
int n;
int X, Y;
double dist(Point a, Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + 1e-10);
}
double getMax(Point p)
{
double res = 0;
for (int i = 1; i <= n; i++)
res = max(res, dist(a[i], p));
return res;
}
double SA()
{
Point p(rand()%X, rand()%Y), to;
double T = start_T, rate = 0.94;
double fx, fy, ans = getMax(p);
while(T > eps)
{
double tmp = inf;
for (int times = 1; times <= 20; times++)
for (int i = 0; i < 8; i++)
{
fx = p.x + dx[i] / start_T * T * (rand()%X);
fy = p.y + dy[i] / start_T * T * (rand()%Y);
if (fx < 0) fx = 0;
if (fy < 0) fy = 0;
if (fx > X) fx = X;
if (fy > Y) fy = Y;
double d = getMax(Point(fx, fy));
if (d < tmp)
{
tmp = d;
to = Point(fx, fy);
}
}
T *= rate;
if (tmp < eps) continue;
if (tmp < ans || (rand()%1000)/1000.0 < exp(-fabs(ans-tmp)/T*start_T))
{
ans = tmp;
p = to;
}
}
printf("(%.1f,%.1f).\n", p.x, p.y);
return ans;
}
int t;
int main()
{
// FOPI;
srand(time(NULL));
while(~scanf("%d%d%d", &X, &Y, &n))
{
for (int i = 1; i <= n; i++)
scanf("%lf%lf", &a[i].x, &a[i].y);
printf("%.1f\n", SA());
}
}
Groundhog Build Home - HDU - 3932(模拟退火)的更多相关文章
- HDU 3932 Groundhog Build Home 【基础模拟退火】
和刚才那道是一模一样 不过求的是最小的,只要稍微修改一下就可以了~ //#pragma comment(linker, "/STACK:16777216") //for c++ C ...
- HDU 3932 模拟退火
HDU3932 题目大意:给定一堆点,找到一个点的位置使这个点到所有点中的最大距离最小 简单的模拟退火即可 #include <iostream> #include <cstdio& ...
- hdu 3932 Groundhog Build Home
Groundhog Build Home Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- hdu 2215 & hdu 3932(最小覆盖圆)
Maple trees Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 3932 Groundhog Build Home —— 模拟退火
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3932 找一个位置使距离最远的点的距离最小: 上模拟退火: 每次向距离最远的点移动,注意判断一下距离最远的点 ...
- hdu 3932 Groundhog Build Home——模拟退火
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3932 注意平均值与最远的点距离为0的情况.所以初值设成-1,这样 id 就不会乱.不过设成0也可以.注意判 ...
- HDU 3932
http://acm.hdu.edu.cn/showproblem.php?pid=3932 一定范围的平面上给一些点,求到这些点的最大距离最小,和上一题的题意正好相反,稍微改一下就可以 这个问题又叫 ...
- hdu 5017 模拟退火/三分求椭圆上离圆心最近的点的距离
http://acm.hdu.edu.cn/showproblem.php?pid=5017 求椭圆上离圆心最近的点的距离. 模拟退火和三分套三分都能解决 #include <cstdio> ...
- hdu 5017 模拟退火算法
hdu 5017 http://blog.csdn.net/mypsq/article/details/39340601 #include <cstdio> #include <cs ...
随机推荐
- form中onsubmit的使用
form 中的onsubmit在点submit按钮时被触发,如果return false;则结果不会被提交到action中去(也就是提交动作不会发生),如果不返回或者返回true,则执行提交动作.(& ...
- 用mvc模式,整理前两次的代码并增加登陆注册
简单的servlet连接mysql数据库 使用mvc的登录注册 commons-dbutils-1.6 mysql-connector-java-5.1.40-bin c3p0-0.9.5.2 mch ...
- 移动开发,Webapp 淘宝手机 rem 布局
(function (doc, win) { var docEl = doc.documentElement, resizeEvt ="orientationchange"in w ...
- 你还在为UiPath课程考试发愁吗?
刚开始学UiPath的时候,课程的考试难倒了很多人,有语言的原因也有对课程理解的原因,记忆中好像有一课考了5次估计,由于题库也就那么多,只要你努力考,总会过的. 学会了RPA的自动化工具,能否自动化答 ...
- AngularJS(十):依赖注入
本文也同步发表在我的公众号“我的天空” 依赖注入 依赖注入不是AngularJS独有的概念,而是现代软件开发与架构的范畴,但是在AngularJS中“依赖注入”是其核心思想之一,所以我们专门来学习一下 ...
- Lua学习---函数定义
1.函数定义的格式: Lua使用function定义函数,语法如下: function function_name (arc) --arc表示参数列表,函数的参数列表可以为空 --body end 上 ...
- java Vamei快速教程06 组合
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经尝试去定义类.定义类,就是新建了一种类型(type).有了类,我们接着构造 ...
- js 获取时间戳 登陆验证码生成要加时间戳
JavaScript 获取当前时间戳,登陆验证码生成要加时间戳,防止存在session不重新请求第一种方法: var timestamp = Date.parse(new Date()); 结果:12 ...
- 站点安全预警,建议大家多重禁止load_file函数!
比如在你的linux机器上运行 select load_file(0x2F6574632F706173737764); 看看结果是什么?这应该不是我们希望看到的. 所以我们禁用这个函数吧. 这个主要通 ...
- python_49_三种编程方式及面向过程与面向函数区别.py
''' 三种编程方式:1.面向对象 (类:class)2.面向过程 (过程:def)3.函数式编程(函数:def) 编程语言中函数的定义:函数是逻辑结构化和过程化的一种编程方法 过程与函数的区别,过程 ...