[JSOI2004]平衡点 / 吊打XXX

题目描述:

雾。

题目分析:

模拟退火!
然而这题目做的我心累啊(雾
只随机出一个答案,被卡的几率太大了,我随机了20个答案出来,然后挑一个最优的。
然而%常数过大,不开氧气优化会T成SB。
卡了20次,终于过了(雾

题目链接:

Luogu 1337
BZOJ过不了(雾

Ac 代码:

#include 
#include 
#include 
#include 
#include 
const double r=0.99;
const double eps=1e-3;
const int maxm=1e5+100;
struct node{
    double x,y;
    double wight;
}a[maxm];
node ans[21];
double distance(double x1,double y1,double x2,double y2){return std::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
double Rand(){return rand()%10000/10000.0;}
int n;
double getf(node poi,double &minn,int x)
{
    double ret=0.0;
    for(int i=1;i<=n;i++) ret+=(distance(poi.x,poi.y,a[i].x,a[i].y)*a[i].wight);
    if(retreturn ret;
}
void SA(double T,int x)
{
    node now=ans[x];
    double minn=1e18+100;
    while(T>eps)
    {
        node newd;
        newd.x=(now.x+T*(Rand()*2-1.0));
        newd.y=(now.y+T*(Rand()*2-1.0));
        double f1=getf(now,minn,x);
        double f2=getf(newd,minn,x);
        double delta=f1-f2;
        if(delta>0||std::exp(delta/T)>Rand()) now=newd;
        T*=r;
    }
    for(int i=1;i<=1000;i++)
    {
        node newd;
        newd.x=(ans[x].x+T*(Rand()*2-1.0));
        newd.y=(ans[x].y+T*(Rand()*2-1.0));
        getf(newd,minn,x);
    }
}
int main()
{
    srand(19260817U);
    node tt;
    tt.x=0,tt.y=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
     scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].wight),tt.x+=a[i].x,tt.y+=a[i].y;
    for(int i=1;i<=20;i++)
    {
         ans[i].x=tt.x/(double)n;
         ans[i].y=tt.y/(double)n;
         SA(5000,i);
    }
    double minn=1e18+100;
    for(int i=1;i<=20;i++) getf(ans[i],minn,0);
    printf("%.3lf %.3lf\n",ans[0].x,ans[0].y);
    return 0;

你可能感兴趣的:(题目分析,膜你退火)