【模拟退火】poj2420 A Star not a Tree?
题意:求平面上一个点,使其到给定的n个点的距离和最小,即费马点。
模拟退火的思想是随机移动,然后100%接受更优解,以一定概率接受更劣解。移动的过程中温度缓慢降低,接受更劣解的概率降低。
在网上看到的代码都不太靠谱,我这个代码的关键之处在于,每一次随机走点时,不是1次,而是在10次随机中取最优者作为当前这一步的随机结果,这样运行时非常优秀。
T降温时乘0.9/0.99这样的数都行,越接近1越准确,但速度越慢。
这份代码即使用0.9也可以ac。
#include<cstdio>
#include<cmath>
#include<cstdlib>
using namespace std;
const double EPS=0.00000001;
const double PI=acos(-1.0);
struct Point{
double x,y;
Point(const double &x,const double &y){
this->x=x;
this->y=y;
}
Point(){}
void read(){
scanf("%lf%lf",&x,&y);
}
double length(){
return sqrt(x*x+y*y);
}
}a[105],p;
double ans;
int n;
typedef Point Vector;
Vector operator - (const Point &a,const Point &b){
return Vector(a.x-b.x,a.y-b.y);
}
Vector operator + (const Vector &a,const Vector &b){
return Vector(a.x+b.x,a.y+b.y);
}
Vector operator * (const double &K,const Vector &v){
return Vector(K*v.x,K*v.y);
}
double calc(Point p){
double res=0.0;
for(int i=1;i<=n;++i){
res+=(a[i]-p).length();
}
return res;
}
int main(){
srand(233);
// freopen("poj2420.in","r",stdin);
// freopen("poj2420.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;++i){
a[i].read();
p.x+=a[i].x;
p.y+=a[i].y;
}
p.x/=(double)n;
p.y/=(double)n;
ans=calc(p);
double T=100000.0;
while(T>EPS){
double bestnow=10000000.0;
Point besttp;
for(int i=1;i<=10;++i){
double rad=(double)(rand()%10000+1)/10000.0*2.0*PI;
Point tp=p+T*Point(cos(rad),sin(rad));
double now=calc(tp);
if(now<bestnow){
bestnow=now;
besttp=tp;
}
}
if(bestnow<ans || exp((ans-bestnow)/T)*10000.0>(double)(rand()%10000)){
ans=bestnow;
p=besttp;
}
T*=0.99;
}
printf("%.0f\n",ans);
return 0;
}
【模拟退火】poj2420 A Star not a Tree?的更多相关文章
- poj2420 A Star not a Tree? 找费马点 模拟退火
题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...
- poj-2420 A Star not a Tree?(模拟退火算法)
题目链接: A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5219 Accepte ...
- 模拟退火算法A Star not a Tree?(poj2420)
http://write.blog.csdn.net/postedit A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Tot ...
- Poj2420 A Star not a Tree? 模拟退火算法
题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...
- POJ-2420 A Star not a Tree? 梯度下降 | 模拟退火
题目链接:https://cn.vjudge.net/problem/POJ-2420 题意 给出n个点,找一个点,使得这个点到其余所有点距离之和最小. 思路 一开始就在抖机灵考虑梯度下降,猜测是个凸 ...
- [POJ2420]A Star not a Tree?(模拟退火)
题目链接:http://poj.org/problem?id=2420 求费马点,即到所有其他点总和距离最小的点. 一开始想枚举一个坐标,另一个坐标二分的,但是check的时候还是O(n)的,复杂度相 ...
- poj2420 A Star not a Tree? 模拟退火
题目大意: 给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\)) 题解 模拟退火上 #include <cmath> #include <cstdio&g ...
- [POJ2420]A Star not a Tree?
来源: Waterloo Local 2002.01.26 题目大意: 找出$n$个点的费马点. 思路: 模拟退火. 首先任取其中一个点(或随机一个坐标)作为基准点,每次向四周找距离为$t$的点,如果 ...
- [模拟退火][UVA10228] A Star not a Tree?
好的,在h^ovny的安利下做了此题 模拟退火中的大水题,想当年联赛的时候都差点打了退火,正解貌似是三分套三分,我记得上一道三分套三分的题我就是退火水过去的... 貌似B班在讲退火这个大玄学... 这 ...
随机推荐
- Winform Socket通信
Socket相关概念[端口] 在Internet上有很多这样的主机,这些主机一般运行了多个服务软件,同时提供几种服务.每种服务都打开一个Socket,并绑定到一个端口上,不同的端口对应于不同的服务(应 ...
- Arduino 舵机sg90电位器实现转动方向控制
/* Sweep*/ #include <Servo.h> int potpin = 0;//电位器接到A0 int val; //存储电位器读取的数值 Servo myservo//定义 ...
- linux下暴力破解工具hydra【转】
一.简介 Number one of the biggest security holes are passwords, as every password security study shows. ...
- LeetCode 19 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 2015多校第6场 HDU 5361 并查集,最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...
- SAE如何使用Git
了解Git及远程git仓库 请先看博文<Git入门及上传项目到github中>,弄懂了之后我相信我下面说的就相当于废话了. SAE的git远程仓库就相当于github. 向SAE的远程仓库 ...
- 2.C 基础
C 基础 原文地址:http://rypress.com/tutorials/objective-c/c-basics OC 可以说是C语言的一个超集,这样你可以无缝的和C语言结合编程也就是你可以这两 ...
- python中的map、reduce、filter、sorted函数
map.reduce.filter.sorted函数,这些函数都支持函数作为参数. map函数 map() 函数语法:map(function, iterable, ...) function -- ...
- HDU 3669 Cross the Wall(斜率DP+预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3669 题目大意:有n(n<=50000)个矩形,每个矩形都有高和宽,你可以在墙上最多挖k个洞使得 ...
- Dubbo 用户手册学习笔记 —— Dubbo架构
Dubbo的架构 节点角色说明 节点 角色说明 Provider 服务提供方 Consumer 服务消费方 Registry 服务注册与发现的注册中心 Monitor 统计服务的调用次数和调用时间的监 ...