poj2420:A Star not a Tree?(模拟退火)

传送门

题解:
根据模拟退火暴力转移点即可。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

inline int rd() {
    char ch=getchar(); int i=0,f=1;
    while(!isdigit(ch)) {if(ch=='-')f=-1; ch=getchar();}
    while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=getchar();}
    return i*f;
}

struct P {
    double x,y;
    P(double x=0,double y=0) :x(x),y(y){}
    friend inline P operator +(const P &a,const P &b) {
        return P(a.x+b.x,a.y+b.y);
    }
    friend inline P operator -(const P &a,const P &b) {
        return P(a.x-b.x,a.y-b.y);
    }
    inline double dis() {
        return sqrt(x*x+y*y);
    }
}p[110],now;

int n;
const double PI=acos(-1.0);

inline double rnd() {
    return (rand()%10000)/10000.0;
}

inline double calc(const P &t) {
    double rs=0;
    for(int i=1;i<=n;i++) rs=rs+(t-p[i]).dis();
    return rs;
}

int main() {
    n=rd();
    for(int i=1;i<=n;i++) p[i].x=rd(),p[i].y=rd();
    now=p[1]; double ans=calc(now),lst=ans;
    for(double st=10000; st>=0.0000001; st=st*0.998) {
        double theta=rand()%360;
        theta=theta/180.0*PI;
        double detx=st*cos(theta);
        double dety=st*sin(theta);
        P tp=now+P(detx,dety);
        double tpans=calc(tp);
        ans=min(ans,tpans);
        if(tpans<lst || exp((lst-tpans)/st)>rnd()) now=tp,lst=tpans;
    }
    printf("%.0f\n",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值