您的位置:首页 > 其它

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

2016-12-02 21:04 537 查看
经典的模拟退火骗分 唯一不爽的是调了一晚上参发现输出打错了

#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
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: