标签:size int loading cpp sqrt src lin while sts
class Solution {
public:
int n;
double eps=1E-6;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
double getMinDistSum(vector<vector<int>>& positions) {
n=positions.size();
double xavg=0.0;
double yavg=0.0;
for(auto& p:positions){
xavg+=p[0];
yavg+=p[1];
}
xavg/=n;
yavg/=n;
double mindis=dist(xavg,yavg,positions);
double step=100.0;
int done=0;
while(step>eps){
done=0;
for(int i=0;i<4;i++){
double nx=xavg+step*dx[i];
double ny=yavg+step*dy[i];
double t=dist(nx,ny,positions);
if(t<mindis){
mindis=t;
xavg=nx;
yavg=ny;
done=1;
break;
}
}
if(!done){
step/=2;
}
}
return mindis;
}
double dist(double x, double y,vector<vector<int>>& positions){
double ret=0.0;
for(int i=0;i<n;i++){
double dx=positions[i][0]-x;
double dy=positions[i][1]-y;
ret+=sqrt(dx*dx+dy*dy);
}
return ret;
}
};
LC 1515. Best Position for a Service Centre (Simulated Annealing)
标签:size int loading cpp sqrt src lin while sts
原文地址:https://www.cnblogs.com/FEIIEF/p/13289110.html