poj2420

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#define D 0.01
#define pi 3.1415926535897932
#define MAXN 1000
#define STEP 0.8
using namespace std;

int n;
struct node{
    double x,y;
}point[MAXN];

double dis(double x1,double y1,double x2,double y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

double sum(double x,double y){
    double a=0;
    for(int i=1;i<=n;++i){
        a+=dis(x,y,point[i].x,point[i].y);
    }
    return a;
} 

double suiji(){
    double x=rand()%10000+1;
    return x/10000.0*2.0*pi;
}

int main(){
    double l=9999999,r=-9999999,u=-9999999,d=9999999;
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%lf %lf",&point[i].x,&point[i].y);
    for(int i=1;i<=n;++i){
        if(point[i].x<l) l=point[i].x;
        if(point[i].x>r) r=point[i].x;
        if(point[i].y>u) u=point[i].y;
        if(point[i].y<d) d=point[i].y;
    }
    double xx=(l+r)/2,yy=(u+d)/2,deg,res=sum(xx,yy),t=100000,tmp;
    while(t>0.0001){
        double newx;
        double newy;
        for(int i=1;i<=30;++i){
            for(int j=1;j<=30;++j){
                deg=suiji();
                newx=xx+t*sin(deg);
                newy=yy+t*cos(deg);                      
                tmp=sum(newx,newy);
                if(tmp<res) res=tmp,xx=xx+t*sin(deg),yy=yy+t*cos(deg);
                //随机接受一个错解,随时间推移,概率逐渐减小 
                else if(log10(t/10000*(rand()%100)/10.0+1)>1) res=tmp,xx=xx+t*sin(deg),yy=yy+t*cos(deg);
            }
        }
        t*=STEP;
    }
    printf("%.0f",res);
    return 0;
}

/*
5
547 24
345 234
435 34
675 456
7546 2

5
23 4
5456 23
6556 45
645 23
546 3
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值