您的位置:首页 > 其它

bzoj3680 吊打XXX 爬山算法

2016-03-09 22:00 357 查看
本来是打算写一个模拟退火的。但是发现这道题目一开始步长定得太长就可能会调到一个比较偏的点然后就回不来了。。。。索性直接写爬山。

AC代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#define N 10005
using namespace std;

int n; double ans,ansx,ansy,x
,y
,z
;
double dist(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double calc(double u,double v){
double tmp=0; int i;
for (i=1; i<=n; i++) tmp+=dist(x[i],y[i],u,v)*z[i];
if (tmp<ans){ ans=tmp; ansx=u; ansy=v; } return tmp;
}
double getr(){ return (double)(rand()%20000)/20000; }
int main(){
srand(20160309); scanf("%d",&n); int i;
for (i=1; i<=n; i++){
scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
ansx+=x[i]; ansy+=y[i];
}
ansx/=n; ansy/=n; ans=calc(ansx,ansy);
double sx=ansx,sy=ansy,u,v,t=1e5;
for (; t>1e-3; t*=0.98){
u=sx+t*(getr()*2-1); v=sy+t*(getr()*2-1);
if (calc(u,v)<calc(sx,sy)){ sx=u; sy=v; }
}
printf("%.3f %.3f\n",ansx,ansy);
return 0;
}


by lych
2016.3.9
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: