bzoj3680: 吊打XXX

%你退火。卡精度卡得我好难过。。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
double myrand(){return double(rand()%10000)/10000.0;}

int n;
struct node{double x,y,w;}a[11000];
double ansx,ansy,mmin;
double calc(double x,double y)
{
    double sum=0;
    for(int i=1;i<=n;i++)
        sum+=a[i].w* sqrt((x-a[i].x)*(x-a[i].x)+(y-a[i].y)*(y-a[i].y)) ;
    if(sum<mmin)ansx=x,ansy=y,mmin=sum;
    return sum;
}

void annealing(double x,double y)
{
    double T=1000000.0;
    while(T>0.001)
    {
        double tx=x+T*(myrand()*2-1.0);
        double ty=y+T*(myrand()*2-1.0);
        double delta=calc(x,y)-calc(tx,ty);
        if(delta>0||exp(delta/T)>myrand())x=tx,y=ty;
        T*=0.97;
    }
    for(int i=1;i<=1000;i++)
    {
        double tx=ansx+T*(myrand()*2-1.0);
        double ty=ansy+T*(myrand()*2-1.0);
        calc(tx,ty);
    }
}

int main()
{
    srand(34572345);
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    double avx=0,avy=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].w);
        avx+=a[i].x;
        avy+=a[i].y;
    }
    avx/=double(n);
    avy/=double(n);
    
    mmin=calc(avx,avy);
    annealing(avx,avy);
    printf("%.3lf %.3lf\n",ansx,ansy);
    return 0;
}

 

posted @ 2018-11-23 13:59  AKCqhzdy  阅读(172)  评论(0编辑  收藏  举报