题目链接

POJ输出不能用%lf!

mmp从4:30改到6:00,把4:30交的一改输出也过了。

于是就有了两份代码。。

//392K	500MS
//用两点构成的矩形更新,就不需要管边界了
#include <cmath>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
#define Rate (0.9)
#define Rand() (1.0*((rand()<<1)-RAND_MAX)/RAND_MAX)
const int N=1005; int n,X,Y;
double DIS,ans_dis[20];
struct Node{
double x,y;
Node() {}
Node(double x,double y):x(x),y(y) {}
}p[N],ans[30]; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline double Sqr(double x){
return x*x;
}
inline double Dis(double x,double y,int i){
return sqrt(Sqr(x-p[i].x)+Sqr(y-p[i].y));
}
double Calc(double x,double y)
{
double res=DIS;
for(int i=1; i<=n; ++i)
res=std::min(res,Dis(x,y,i));
return res;
}
void Print(){
for(int i=1; i<=15; ++i) printf("%d:%.5lf %.5lf %.5lf %d %d\n",i,ans[i].x,ans[i].y,ans_dis[i],X,Y);
}
void Solve()
{
DIS=sqrt(X*X+Y*Y);
const int tot=15;
for(int i=1; i<=tot; ++i)
ans[i].x=1.0*rand()/RAND_MAX*X,ans[i].y=1.0*rand()/RAND_MAX*Y,ans_dis[i]=Calc(ans[i].x,ans[i].y);
double T=DIS,xx,yy,dis;//this...
while(T>0.01)
{
for(int i=1; i<=tot; ++i)
for(int j=1; j<=30; ++j)
{
xx=ans[i].x+T*Rand(), yy=ans[i].y+T*Rand();
if(xx<0||xx>X||yy<0||yy>Y) continue;//!
dis=Calc(xx,yy);
if(dis>ans_dis[i]) ans[i]=Node(xx,yy),ans_dis[i]=dis;
}
T*=Rate;
}
int res=1;
for(int i=2; i<=tot; ++i)
if(ans_dis[i]>ans_dis[res]) res=i;
printf("The safest point is (%.1f, %.1f).\n",ans[res].x,ans[res].y);
} int main()
{
srand(20180428);
int T=read();
while(T--)
{
X=read(),Y=read(),n=read();
for(int i=1; i<=n; ++i) p[i].x=read(),p[i].y=read();
Solve();
}
return 0;
}

另一种写法:

//392K	1188MS
#include <cmath>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
#define Rate (0.97)
//#define Rand() (1.0*((rand()<<1)-RAND_MAX)/RAND_MAX)
#define Rand() (1.0*rand()/RAND_MAX) const int N=1005; int n,X,Y;
double DIS,ans_dis[30];
struct Node{
double x,y;
Node() {}
Node(double x,double y):x(x),y(y) {}
}p[N],ans[30]; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline double Sqr(double x){
return x*x;
}
inline double Dis(double x,double y,int i){
return sqrt(Sqr(x-p[i].x)+Sqr(y-p[i].y));
}
double Calc(double x,double y)
{
double res=1e9;//DIS;
for(int i=1; i<=n; ++i)
res=std::min(res,Dis(x,y,i));
return res;
}
void Rand_Point(double &x,double &y,Node a,Node b){
x=a.x+(b.x-a.x)*Rand(), y=a.y+(b.y-a.y)*Rand();
}
void Solve()
{
DIS=sqrt(X*X+Y*Y);
const int tot=15;
ans[1]=Node(0,0), ans[2]=Node(X,Y), ans[3]=Node(X,0), ans[4]=Node(0,Y);//
for(int i=5; i<=tot; ++i)
Rand_Point(ans[i].x,ans[i].y,ans[1],ans[2]);
// ans[i].x=Rand()*X,ans[i].y=Rand()*Y;
for(int i=1; i<=tot; ++i) ans_dis[i]=Calc(ans[i].x,ans[i].y);
double T=std::min(X,Y),xx,yy,dis;//this...
while(T>0.01)
{
for(int i=1; i<=tot; ++i)
for(int j=1; j<=20; ++j)
{
Rand_Point(xx,yy,Node(std::max(0.0,ans[i].x-T),std::max(0.0,ans[i].y-T)),Node(std::min(1.0*X,ans[i].x+T),std::min(1.0*Y,ans[i].y+T)));
dis=Calc(xx,yy);
if(dis>ans_dis[i]) ans[i]=Node(xx,yy),ans_dis[i]=dis;
}
T*=Rate;
}
int res=1;
for(int i=2; i<=tot; ++i)
if(ans_dis[i]>ans_dis[res]) res=i;
printf("The safest point is (%.1f, %.1f).\n",ans[res].x,ans[res].y);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
#endif srand(20180428);
int T=read();
while(T--)
{
X=read(),Y=read(),n=read();
for(int i=1; i<=n; ++i) p[i].x=read(),p[i].y=read();
Solve();
}
return 0;
}

POJ.1379.Run Away(模拟退火)的更多相关文章

  1. poj 1379 Run Away 模拟退火 难度:1

    Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6482   Accepted: 1993 Descript ...

  2. POJ 1379 Run Away 【基础模拟退火】

    题意:找出一点,距离所有所有点的最短距离最大 二维平面内模拟退火即可,同样这题用最小圆覆盖也是可以的. Source Code: //#pragma comment(linker, "/ST ...

  3. PKU 1379 Run Away(模拟退火算法)

    题目大意:原题链接 给出指定的区域,以及平面内的点集,求出一个该区域内一个点的坐标到点集中所有点的最小距离最大. 解题思路:一开始想到用随机化算法解决,但是不知道如何实现.最后看了题解才知道原来是要用 ...

  4. POJ 1379 Run Away

    题意:有n个陷阱,在X,Y范围内要求出一个点使得这个点到陷阱的最小距离最大. 思路:模拟退火,随机撒入40个点,然后模拟退火随机化移动. (这题poj坑爹,加了srand(time(NULL))不能交 ...

  5. POJ 1379 模拟退火

    模拟退火算法,很久之前就写过一篇文章了.双倍经验题(POJ 2420) 题意: 在一个矩形区域内,求一个点的距离到所有点的距离最短的那个,最大. 这个题意,很像二分定义,但是毫无思路,也不能暴力枚举, ...

  6. POJ 1379 (随机算法)模拟退火

    题目大意: 给定一堆点,找到一个点的位置使这个点到所有点中的最小距离最大 这里数据范围很小,精度要求也不高,我们这里可以利用模拟退火的方法,随机找到下一个点,如果下一个点比当前点优秀就更新当前点 参考 ...

  7. poj-1379 Run Away(模拟退火算法)

    题目链接: Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7982   Accepted: 2391 De ...

  8. 【BZOJ1844/2210】Pku1379 Run Away 模拟退火

    [BZOJ1844/2210]Pku1379 Run Away 题意:矩形区域中有一堆点,求矩形中一个位置使得它到所有点的距离的最小值最大. 题解:模拟退火的裸题,再调调调调调参就行了~ #inclu ...

  9. poj 2069 Super Star —— 模拟退火

    题目:http://poj.org/problem?id=2069 仍是随机地模拟退火,然而却WA了: 看看网上的题解,都是另一种做法——向距离最远的点靠近: 于是也改成那样,竟然真的A了...感觉这 ...

随机推荐

  1. 鸟哥的Linux私房菜——第十二章:档案的压缩与打包

    视频链接: 土豆:http://www.tudou.com/programs/view/GncwT0FJKsQ B站(推荐):http://www.bilibili.com/video/av98857 ...

  2. service注入到action中

    service注入到action中 之前本人每次要获得service都是在action自己通过WebApplicationContext的getBean获得的,一直在spring中只配置到了servi ...

  3. sssss

    关于征集参加第五届世界互联网大会“世界互联网领先科技成果发布活动”相关成果的通知 2018年07月24日 08:55:00来源: 中国网信网     [打印] [纠错]     各有关单位/个人: 第 ...

  4. SpringJMS解析--使用示例

    Spring配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  5. 一个Zabbix agent配置多个Zabbix Server

    环境说明: 公司和政府合作的项目中,需要对服务器添加监控.政府方面已对服务部署了zabbix agent,实现了系统层面的基础监控:而对于公司而言,需要对服务器上的服务进行监控,故需在原有的zabbi ...

  6. RESTful 个人理解总结【转】

    转自:http://www.cnblogs.com/wang-yaz/p/9237981.html 一.什么是RESTful 面向资源 简单的说:RESTful是一种架构的规范与约束.原则,符合这种规 ...

  7. 从requests源码分析中学习python(一)

    v2ex同步更新:https://www.v2ex.com/t/500081 微信公众号:python学习开发 分析源码,看大神的代码是一种学习的好方法,让我从中学到很多以前不知道的知识,这次打算从大 ...

  8. 『记录』Android参考资料

    欢迎留言推荐好的教程.资料.博客及作者等. 『记录』Android参考资料 1.前期环境 Android Studio使用Git Android Studio快捷键总结 Android Studio及 ...

  9. MySQL乱码问题以及utf8mb4字符集

    MySQL乱码问题以及utf8mb4字符集 1.乱码 推荐大家看 深入MySQL字符集设置 ,区分检查client端.server端的编码:最简单暴力的方式,是在所有的环节都显式明确的指定相同的编码, ...

  10. 读写分离MYSQL类

    2014年4月27日 12:34:08 概述: 1. 根据sql语句判断是连接读库还是写库 2. 链式调用$this->where()->get() 3. 不同的主机对应不同的实例, 不再 ...