题目链接

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. Maven的国内镜像(解决jar下载过慢)

    Maven简介 maven作为一个项目管理工具确实非常好用,结果在使用时候,你会发现下载jar速度不如自己在网上下载.之前oschina的中央仓库可用,现在oschina的maven服务器关了,只能拿 ...

  2. protobuf与json相互转换的方法

    google的protobuf对象转json,不能直接使用FastJson之类的工具进行转换,原因是protobuf生成对象的get方法,返回的类型有byte[],而只有String类型可以作为jso ...

  3. bzoj千题计划233:bzoj 1304: [CQOI2009]叶子的染色

    http://www.lydsy.com/JudgeOnline/problem.php?id=1304 结论1:根节点一定染色 如果根节点没有染色,选择其子节点的一个颜色,那么所有这个颜色的子节点都 ...

  4. HDU 4315 阶梯博弈变形

    n个棋子,其中第k个是红色的,每个棋子只能往上爬,而且不能越过.重叠其他棋子,谁将红色棋子移到顶部谁赢. 由于只能往上爬,所以很像阶梯博弈.这题有2个限制,棋子不能重叠,有红棋存在 首先不考虑红色棋, ...

  5. Linux 并发链接数

    并发数查看   查看 TCP 协议连接数 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' SYN_RECV # ...

  6. 第11月第20天 sqlite3_open xcode mysql connector

    1. sqlite3_open 死锁 * thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP frame ...

  7. Java 学习札记(一)JDK安装配置

    Windows上配置JDK 1.下载windows版JDK 网址:http://www.oracle.com/technetwork/java/javase/archive-139210.html 2 ...

  8. Android Studio 新建drawable-hdpi、drawable-mdpi等

    在不同的模式“Project” / “Android”的文件夹中查看文件夹.如果文件夹丢失,您可以轻松添加它们. 1.在“res”文件夹上右键“New”->”Android Resource D ...

  9. C++单链表反转

    单链表反转笔记: #include<iostream> #include<string.h> using namespace std; struct ListNode { in ...

  10. 使用java如何操作elasticsearch?简单示例。

    在线API:https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/transport-client.html教程:http ...