BZOJ3680 JSOI2004 平衡点 - 随机/近似算法

迭代乱搞了下就过了……

#include  
using namespace std;

double x[10005],y[10005],w[10005];
double xm,ym,wt,k,lambda=0.9;
int n;

const double ox[5]={0,1,0,-1,0},oy[5]={0,0,1,0,-1};

double calc(double xw,double yw){
    double ans=0;
    for(int i=1;i<=n;i++) 
        ans+=sqrt((xw-x[i])*(xw-x[i])+(yw-y[i])*(yw-y[i]))*w[i];
    return ans;
}

int main() {
    cin>>n;
    for(int i=1;i<=n;i++) 
        cin>>x[i]>>y[i]>>w[i],
        xm+=x[i]*w[i],
        ym+=y[i]*w[i],
        k=max(k,max(abs(x[i]),abs(y[i]))),
        wt+=w[i];
    xm/=wt;
    ym/=wt;
    
    k*=0.1;
    
    double z=0,oz=-1e+8;
    
    while(k>=0.0001) {
        //printf("%.3f %.3f  k=%.4f\n",xm,ym,k); 
        oz=z;
        double minopt=1e+18;
        int opt=0;double tmp;
        for(int i=0;i<=4;i++) {
            
            tmp=calc(xm+ox[i]*k,ym+oy[i]*k);
            if(tmp<minopt) 
                minopt=tmp, opt=i;
        }
        xm+=ox[opt]*k, ym+=oy[opt]*k;
        z=tmp;
        k*=lambda;
    }
    
    printf("%.3f %.3f\n",xm,ym);    
}

 

转载于:https://www.cnblogs.com/mollnn/p/8440043.html

你可能感兴趣的:(BZOJ3680 JSOI2004 平衡点 - 随机/近似算法)