POJ.1379.Run Away(模拟退火)
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(模拟退火)的更多相关文章
- poj 1379 Run Away 模拟退火 难度:1
Run Away Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6482 Accepted: 1993 Descript ...
- POJ 1379 Run Away 【基础模拟退火】
题意:找出一点,距离所有所有点的最短距离最大 二维平面内模拟退火即可,同样这题用最小圆覆盖也是可以的. Source Code: //#pragma comment(linker, "/ST ...
- PKU 1379 Run Away(模拟退火算法)
题目大意:原题链接 给出指定的区域,以及平面内的点集,求出一个该区域内一个点的坐标到点集中所有点的最小距离最大. 解题思路:一开始想到用随机化算法解决,但是不知道如何实现.最后看了题解才知道原来是要用 ...
- POJ 1379 Run Away
题意:有n个陷阱,在X,Y范围内要求出一个点使得这个点到陷阱的最小距离最大. 思路:模拟退火,随机撒入40个点,然后模拟退火随机化移动. (这题poj坑爹,加了srand(time(NULL))不能交 ...
- POJ 1379 模拟退火
模拟退火算法,很久之前就写过一篇文章了.双倍经验题(POJ 2420) 题意: 在一个矩形区域内,求一个点的距离到所有点的距离最短的那个,最大. 这个题意,很像二分定义,但是毫无思路,也不能暴力枚举, ...
- POJ 1379 (随机算法)模拟退火
题目大意: 给定一堆点,找到一个点的位置使这个点到所有点中的最小距离最大 这里数据范围很小,精度要求也不高,我们这里可以利用模拟退火的方法,随机找到下一个点,如果下一个点比当前点优秀就更新当前点 参考 ...
- poj-1379 Run Away(模拟退火算法)
题目链接: Run Away Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7982 Accepted: 2391 De ...
- 【BZOJ1844/2210】Pku1379 Run Away 模拟退火
[BZOJ1844/2210]Pku1379 Run Away 题意:矩形区域中有一堆点,求矩形中一个位置使得它到所有点的距离的最小值最大. 题解:模拟退火的裸题,再调调调调调参就行了~ #inclu ...
- poj 2069 Super Star —— 模拟退火
题目:http://poj.org/problem?id=2069 仍是随机地模拟退火,然而却WA了: 看看网上的题解,都是另一种做法——向距离最远的点靠近: 于是也改成那样,竟然真的A了...感觉这 ...
随机推荐
- Spark记录-源码编译spark2.2.0(结合Hive on Spark/Hive on MR2/Spark on Yarn)
#spark2.2.0源码编译 #组件:mvn-3.3.9 jdk-1.8 #wget http://mirror.bit.edu.cn/apache/spark/spark-2.2.0/spark- ...
- centos6.x下安装maven
转自:http://www.centoscn.com/image-text/install/2014/0507/2923.html 1.下载maven包首先从官网上 http://maven.apac ...
- python学习笔记5--加密模块hashlib
import hashlib # md5 ybm_pwd='yuanbapqingsdfs234FF234HF@F' # m = hashlib.md5() # bytes_ybq = ybm_pwd ...
- Firefox滚动残影(转)
Firefox滚动残影 Firefox滚动残影这文章放在草稿箱有一阵子了,之前的3系列都有这BUG,当正想发表这文章的时候,和我沟通刚刚升级的FF4已修复此BUG,所以搁置一阵在考虑到这文章还有没 ...
- [转] Android 性能分析案例
Android 系统的一个工程师(Romain Guy)针对Falcon Pro 应用,撰写了一个Android性能分析的文章.该文章介绍了如何分析一个应用哪里出现了性能瓶颈,导致该应用使用起来不流 ...
- mysql5.7 主从复制的正常切换【转】
目前环境如下: master server IP:172.17.61.131 slave server IP:172.17.61.132 mysql version: mysql-5.7.21-lin ...
- 拓展中国剩余定理(exCRT)摘要
清除一个误区 虽然中国剩余定理和拓展中国剩余定理只差两个字,但他俩的解法相差十万八千里,所以会不会CRT无所谓 用途 求类似$$\begin{cases}x \equiv b_{1}\pmod{a_{ ...
- mysql ON DUPLICATE KEY UPDATE重复插入时更新
mysql当插入重复时更新的方法: 第一种方法: 示例一:插入多条记录 假设有一个主键为 client_id 的 clients 表,可以使用下面的语句: INSERT INTO clients (c ...
- MySQL 获得当前日期时间\时间戳 函数
MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间(date + time)函数:now() mysql> select now(); +———————+ | now() | +—— ...
- Extjs Window用法详解 3 打印具体应用,是否关掉打印预览的界面
Extjs Window用法详解 3 打印具体应用,是否关掉打印预览的界面 Extjs 中的按钮元素 {xtype: 'buttongroup',title: '打印',items: [me.ts ...