zoukankan      html  css  js  c++  java
  • poj 2420 A Star not a Tree?——模拟退火

    题目:http://poj.org/problem?id=2420

    精度设成1e-17,做三遍。ans设成double,最后再取整。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<cstdlib>
    #include<ctime>
    #define db double
    using namespace std;
    const int N=105;
    const db dc=0.99,eps=1e-17;
    int n,nx[N],ny[N];
    db px,py,ans;
    db dis(db x0,db y0,db x1,db y1)
    {
      return sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));
    }
    db calc(db x,db y)
    {
      db ret=0;
      for(int i=1;i<=n;i++)ret+=dis(nx[i],ny[i],x,y);
      return ret;
    }
    db gtrd(db T){return (rand()*2-RAND_MAX)*T;}
    void SA(db T)
    {
      db x0=px,y0=py,pr=calc(x0,y0),x,y,cr;
      while(T>eps)
        {
          x=x0+gtrd(T); y=y0+gtrd(T); cr=calc(x,y);
          if(cr<pr||(exp((cr-pr)/T)*RAND_MAX<rand()))
        {
          ans=min(ans,cr);
          x0=x;y0=y;pr=cr;
        }
          T*=dc;
        }
    }
    int main()
    {
      srand(time(0));
      scanf("%d",&n);
      for(int i=1;i<=n;i++)scanf("%d%d",&nx[i],&ny[i]),px+=nx[i],py+=ny[i];
      px/=n; py/=n; ans=calc(px,py);
      SA(10000);SA(10000);SA(10000);
      printf("%.0lf
    ",ans);
      return 0;
    }
  • 相关阅读:
    【leetcode】二叉搜索树的最近公共祖先
    052-12
    052-11
    052-10
    052-9
    052-8
    052-6
    052-5
    052-3
    052-2
  • 原文地址:https://www.cnblogs.com/Narh/p/9876847.html
Copyright © 2011-2022 走看看