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

给出 n 个点的坐标,求费马点;

上模拟退火。

代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cstdlib>
  6. #include<ctime>
  7. #include<cmath>
  8. #define eps 1e-17
  9. #define dt 0.99
  10. using namespace std;
  11. typedef double db;
  12. int const xn=;
  13. int n,xx[xn],yy[xn];
  14. db ansx,ansy,ans;
  15. int rd()
  16. {
  17. int ret=,f=; char ch=getchar();
  18. while(ch<''||ch>''){if(ch=='')f=; ch=getchar();}
  19. while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
  20. return f?ret:-ret;
  21. }
  22. db dist(db x,db y,db a,db b){return sqrt((x-a)*(x-a)+(y-b)*(y-b));}
  23. db cal(db x,db y)
  24. {
  25. db ret=;
  26. for(int i=;i<=n;i++)ret+=dist(x,y,xx[i],yy[i]);
  27. return ret;
  28. }
  29. void SA()
  30. {
  31. db T=,x=ansx,y=ansy,lst=ans,tx,ty,tmp;
  32. while(T>eps)
  33. {
  34. tx=x+(rand()*-RAND_MAX)*T;
  35. ty=y+(rand()*-RAND_MAX)*T;
  36. tmp=cal(tx,ty); db delt=tmp-lst;
  37. if(delt<||exp(delt/T)*RAND_MAX<rand())x=tx,y=ty,lst=tmp;
  38. if(tmp<ans)ansx=tx,ansy=ty,ans=tmp;
  39. T*=dt;
  40. }
  41. }
  42. int main()
  43. {
  44. n=rd(); int sx=,sy=;
  45. for(int i=;i<=n;i++)xx[i]=rd(),yy[i]=rd(),sx+=xx[i],sy+=yy[i];
  46. ansx=1.0*sx/n; ansy=1.0*sy/n; ans=cal(ansx,ansy);
  47. SA(); SA(); SA();
  48. printf("%.0lf\n",ans);
  49. return ;
  50. }

poj 2420 A Star not a Tree? —— 模拟退火的更多相关文章

  1. poj 2420 A Star not a Tree?——模拟退火

    题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include ...

  2. POJ 2420 A Star not a Tree?(模拟退火)

    题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...

  3. 三分 POJ 2420 A Star not a Tree?

    题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...

  4. [POJ 2420] A Star not a Tree?

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4058   Accepted: 200 ...

  5. POJ 2420 A Star not a Tree? (计算几何-费马点)

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3435   Accepted: 172 ...

  6. POJ 2420 A Star not a Tree? 爬山算法

    B - A Star not a Tree? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...

  7. POJ 2420 A Star not a Tree?【爬山法】

    题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...

  8. 【POJ】2420 A Star not a Tree?(模拟退火)

    题目 传送门:QWQ 分析 军训完状态不好QwQ,做不动难题,于是就学了下模拟退火. 之前一直以为是个非常nb的东西,主要原因可能是差不多省选前我试着学一下但是根本看不懂? 骗分利器,但据说由于调参困 ...

  9. uva 10228 - Star not a Tree?(模拟退火)

    题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...

随机推荐

  1. update tableView contenSize

    NSIndexPath *messageIndexPath = [NSIndexPath indexPathForRow:afterRowCount-1 inSection:0];    [self. ...

  2. linux与python

    linux一般都装有python,但是版本不一定,需要自己查看,如果版本过低,需要重装.查看python版本方法,直接在命令行输入 python,此时会进入python命令行,同时会显示python版 ...

  3. android相关文件夹的存取方式与函数解析---全

    因为排版问题.转为markdown编辑: http://blog.csdn.net/self_study/article/details/58587412

  4. poj3181 Dollar Dayz

    Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...

  5. python--内置函数补充

    内置函数补充 基础数据类型相关 和数字相关:数据类型: bool 布尔 int 整型 float 浮点 complex 复数 只有在强转中会用进制转换: bin() 二进制转换 比如0b开头表示的是二 ...

  6. 前端自动化工具 gulp

    最近一个项目才接触这些自动化工具 webpack gulp grunt 等等.. webpack 可以引入模块 和 压缩 gulp 和 grunt 可以压缩 这里只说下gulp  因为项目里只用到gu ...

  7. HTML5学习笔记简明版(9):变化的元素和属性

    改变的元素(Element) 下面元素在HTML5里的使用方法稍作改动以便能在web里更好的使用或者起到更大作用: 没有href属性的a元素将显示成一个占位符,并且a元素内部如今支持flow cont ...

  8. Chisel辅助iOS 应用程序调试,MusicApp模仿酷狗4.0 UI框架

    本文转载至 http://www.cocoachina.com/ios/20140825/9446.html Chisel Chisel集合了大量的LLDB 命令来辅助iOS 应用程序调试,并支持添 ...

  9. 初识kbmmw 中的smartbind功能

    关于kbmmw smartbind 的开发原因及思路,大家可以参见官方的博客说明和红鱼儿的翻译. 今天我就实例操作一下,给大家演示一下具体实现. 我们新建一个工程 放几个基本的控件 在单元里面加上引用 ...

  10. 【BZOJ1528】[POI2005]sam-Toy Cars 贪心

    [BZOJ1528][POI2005]sam-Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio ...