[模拟退火 || Voronoi图] POJ 1379 Run Away

经典的模拟退火骗分 唯一不爽的是调了一晚上参发现输出打错了


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
using namespace std;

const int N=1005;
const double eps=1e-7;
const double PI=acos(-1.0);

inline int dcmp(double a,double b){
  if (fabs(a-b)<eps) return 0;
  if (a>b) return 1; return -1;
}

inline double sqr(double x){ return x*x; }

inline double ran(double x){
  return x*(rand()%1000)/1000;
}

inline double rnd(){
  return 1.0*(rand()%1000)/1000;
}

struct Point{
  double x,y;
  Point(double x=0,double y=0):x(x),y(y) { }
  void read(){ scanf("%lf%lf",&x,&y); }
  double len(){ return sqrt(x*x+y*y); }
  bool operator < (const Point &B) const { return x==B.x?y<B.y:x<B.x; }
  friend Point operator - (Point A,Point B){ return Point(A.x-B.x,A.y-B.y); }
  friend double Dis(Point A,Point B){ return (A-B).len(); }
}P[N];


double X,Y; int n;

typedef pair<double,Point> abcd;
abcd Ret,Ans;

inline double F(Point p){
  double ret=1e130;
  for (int i=1;i<=n;i++)
    ret=min(ret,Dis(p,P[i]));
  return ret;
}

const double Eps=1e-3;
const double beta=.85; 
const int Test=15;

inline void SA(){
  Point sp,np; double theta;
  double ans,ret;
  sp=Point(ran(X),ran(Y)); ans=F(sp);
  for (double T=max(X,Y)/sqrt(1.0*n);T>Eps;T*=beta){
  	Point pre=sp;
  	for (int j=1;j<=30;j++){
	    theta=ran(2*PI);
		np=Point(pre.x+sin(theta)*T,pre.y+cos(theta)*T);
		if (np.x<0||np.y<0||np.x>X||np.y>Y) continue;
	    ret=F(np);
	    if (ret>ans)
	      sp=np,ans=ret;
  	}	
  }
  Ret=abcd(ans,sp);
}

double best; 
Point tp;

int main(){
  freopen("t.in","r",stdin);
  freopen("t.out","w",stdout);
  int Q; srand(time(0));
  scanf("%d",&Q);
  while (Q--){
    scanf("%lf%lf%d",&X,&Y,&n);
    for (int i=1;i<=n;i++) P[i].read();
    Ans.first=0;
    for (int t=1;t<=Test;t++)
      SA(),Ans=max(Ans,Ret);
    printf("The safest point is (%.1lf, %.1lf).\n",Ans.second.x,Ans.second.y);
  }
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值