码迷,mamicode.com
首页 > 其他好文 > 详细

poj2420(模拟退火大法好)

时间:2016-02-13 23:07:02      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

//
//  main.cpp
//  poj2420
//
//  Created by 陈加寿 on 16/2/13.
//  Copyright © 2016年 chenhuan001. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct Point
{
    int x,y;
}g[110];
#define eps 1e-6

double myrand()
{
    long long a=rand();
    long long b=rand();
    a = a*b%200000-100000;
    return ((double)a/10);
}

double fuc(double x,double y,int n)
{
    double sum=0;
    for(int i=0;i<n;i++)
        sum += sqrt( (x-g[i].x)*(x-g[i].x)+(y-g[i].y)*(y-g[i].y) );//不是哈密顿距离
    return sum;
}

double cold(int n)
{
    double tx=0,ty=0;
    double K=1;
    double min=fuc(tx,ty,n);
    while(K>eps)
    {
        double xx,yy;
        xx=myrand()*K+tx;
        yy=myrand()*K+ty;
        double tmp=fuc(xx,yy,n);
        if( tmp < min )
        {
            min=tmp;
            tx=xx;
            ty=yy;
        }
        K*=0.98;
    }
    return min;
}

int main(int argc, const char * argv[]) {
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&g[i].x,&g[i].y);
    }
    double ans = 1e9;
    ans = min(ans,cold(n));
    printf("%.0lf\n",ans);
    return 0;
}

  

poj2420(模拟退火大法好)

标签:

原文地址:http://www.cnblogs.com/chenhuan001/p/5188378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!