poj 2420 A Star not a Tree? —— 模拟退火
题目:http://poj.org/problem?id=2420
给出 n 个点的坐标,求费马点;
上模拟退火。
代码如下:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<cstdlib>
- #include<ctime>
- #include<cmath>
- #define eps 1e-17
- #define dt 0.99
- using namespace std;
- typedef double db;
- int const xn=;
- int n,xx[xn],yy[xn];
- db ansx,ansy,ans;
- int rd()
- {
- int ret=,f=; char ch=getchar();
- while(ch<''||ch>''){if(ch=='')f=; ch=getchar();}
- while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
- return f?ret:-ret;
- }
- db dist(db x,db y,db a,db b){return sqrt((x-a)*(x-a)+(y-b)*(y-b));}
- db cal(db x,db y)
- {
- db ret=;
- for(int i=;i<=n;i++)ret+=dist(x,y,xx[i],yy[i]);
- return ret;
- }
- void SA()
- {
- db T=,x=ansx,y=ansy,lst=ans,tx,ty,tmp;
- while(T>eps)
- {
- tx=x+(rand()*-RAND_MAX)*T;
- ty=y+(rand()*-RAND_MAX)*T;
- tmp=cal(tx,ty); db delt=tmp-lst;
- if(delt<||exp(delt/T)*RAND_MAX<rand())x=tx,y=ty,lst=tmp;
- if(tmp<ans)ansx=tx,ansy=ty,ans=tmp;
- T*=dt;
- }
- }
- int main()
- {
- n=rd(); int sx=,sy=;
- for(int i=;i<=n;i++)xx[i]=rd(),yy[i]=rd(),sx+=xx[i],sy+=yy[i];
- ansx=1.0*sx/n; ansy=1.0*sy/n; ans=cal(ansx,ansy);
- SA(); SA(); SA();
- printf("%.0lf\n",ans);
- return ;
- }
poj 2420 A Star not a Tree? —— 模拟退火的更多相关文章
- poj 2420 A Star not a Tree?——模拟退火
题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include ...
- POJ 2420 A Star not a Tree?(模拟退火)
题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...
- 三分 POJ 2420 A Star not a Tree?
题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...
- [POJ 2420] A Star not a Tree?
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4058 Accepted: 200 ...
- POJ 2420 A Star not a Tree? (计算几何-费马点)
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3435 Accepted: 172 ...
- 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 ...
- POJ 2420 A Star not a Tree?【爬山法】
题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...
- 【POJ】2420 A Star not a Tree?(模拟退火)
题目 传送门:QWQ 分析 军训完状态不好QwQ,做不动难题,于是就学了下模拟退火. 之前一直以为是个非常nb的东西,主要原因可能是差不多省选前我试着学一下但是根本看不懂? 骗分利器,但据说由于调参困 ...
- uva 10228 - Star not a Tree?(模拟退火)
题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...
随机推荐
- update tableView contenSize
NSIndexPath *messageIndexPath = [NSIndexPath indexPathForRow:afterRowCount-1 inSection:0]; [self. ...
- linux与python
linux一般都装有python,但是版本不一定,需要自己查看,如果版本过低,需要重装.查看python版本方法,直接在命令行输入 python,此时会进入python命令行,同时会显示python版 ...
- android相关文件夹的存取方式与函数解析---全
因为排版问题.转为markdown编辑: http://blog.csdn.net/self_study/article/details/58587412
- poj3181 Dollar Dayz
Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...
- python--内置函数补充
内置函数补充 基础数据类型相关 和数字相关:数据类型: bool 布尔 int 整型 float 浮点 complex 复数 只有在强转中会用进制转换: bin() 二进制转换 比如0b开头表示的是二 ...
- 前端自动化工具 gulp
最近一个项目才接触这些自动化工具 webpack gulp grunt 等等.. webpack 可以引入模块 和 压缩 gulp 和 grunt 可以压缩 这里只说下gulp 因为项目里只用到gu ...
- HTML5学习笔记简明版(9):变化的元素和属性
改变的元素(Element) 下面元素在HTML5里的使用方法稍作改动以便能在web里更好的使用或者起到更大作用: 没有href属性的a元素将显示成一个占位符,并且a元素内部如今支持flow cont ...
- Chisel辅助iOS 应用程序调试,MusicApp模仿酷狗4.0 UI框架
本文转载至 http://www.cocoachina.com/ios/20140825/9446.html Chisel Chisel集合了大量的LLDB 命令来辅助iOS 应用程序调试,并支持添 ...
- 初识kbmmw 中的smartbind功能
关于kbmmw smartbind 的开发原因及思路,大家可以参见官方的博客说明和红鱼儿的翻译. 今天我就实例操作一下,给大家演示一下具体实现. 我们新建一个工程 放几个基本的控件 在单元里面加上引用 ...
- 【BZOJ1528】[POI2005]sam-Toy Cars 贪心
[BZOJ1528][POI2005]sam-Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio ...