题目大意:

给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\))

题解

模拟退火上

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 10010;
const double eps = 1e-3;
const double det = 0.99;
const double pi = acos(-1.0);
struct Point{
double x,y;
Point(const double&a=0,const double &b=0){x=a;y=b;}
};
inline double sqr(const double &x){return x*x;}
inline double dis(const Point &a,const Point &b){
return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));
}
Point p[maxn],nw,ans;
double ans_min = 1e100;
int n,w[maxn];
double f(const Point &x){
double ret = 0;
for(int i=1;i<=n;++i) ret += dis(x,p[i]);
if(ret < ans_min) ans_min = ret,ans = x;
return ret;
}
inline double ran(){
return (double)(rand() % 1000 + 1)/1000.0;
}
int main(){
srand(2333);
read(n);
for(int i=1,x;i<=n;++i){
read(x);nw.x+=x;p[i].x = x;
read(x);nw.y+=x;p[i].y = x;
}nw.x /= 1.0*n;nw.y /= 1.0*n;
double T = 1000.0,x,de;
bool flag = false;
while(T > eps){
flag = true;
while(flag){
flag = false;
for(int i=1;i<=4;++i){
x = pi*2.0*ran();
Point nx(nw.x+T*cos(x),nw.y+T*sin(x));
de = f(nw) - f(nx);
if(de > 0 || exp(de/T) > ran()){
nw = nx;flag = true;
}
T *= det;
}
}
}
T = eps;
for(int i=1;i<=1000;++i){
x = pi*2.0*ran();
f(Point(ans.x+T*cos(x)*ran(),ans.y+T*sin(x)*ran()));
}
printf("%.0lf\n",ans_min);
getchar();getchar();
return 0;
}

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

  1. poj-2420 A Star not a Tree?(模拟退火算法)

    题目链接: A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5219   Accepte ...

  2. Poj2420 A Star not a Tree? 模拟退火算法

    题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...

  3. poj2420 A Star not a Tree? 找费马点 模拟退火

    题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...

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

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

  5. 【模拟退火】poj2420 A Star not a Tree?

    题意:求平面上一个点,使其到给定的n个点的距离和最小,即费马点. 模拟退火的思想是随机移动,然后100%接受更优解,以一定概率接受更劣解.移动的过程中温度缓慢降低,接受更劣解的概率降低. 在网上看到的 ...

  6. POJ-2420 A Star not a Tree? 梯度下降 | 模拟退火

    题目链接:https://cn.vjudge.net/problem/POJ-2420 题意 给出n个点,找一个点,使得这个点到其余所有点距离之和最小. 思路 一开始就在抖机灵考虑梯度下降,猜测是个凸 ...

  7. [POJ2420]A Star not a Tree?(模拟退火)

    题目链接:http://poj.org/problem?id=2420 求费马点,即到所有其他点总和距离最小的点. 一开始想枚举一个坐标,另一个坐标二分的,但是check的时候还是O(n)的,复杂度相 ...

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

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

  9. poj2420A Star not a Tree?(模拟退火)

    链接 求某一点到其它点距离和最小,求这个和,这个点 为费马点. 做法:模拟退火 #include <iostream> #include<cstdio> #include< ...

随机推荐

  1. Obj-C数组以及字符串拼接与分割

    本文转载至 http://mobile.51cto.com/iphone-392148.htm Obj-C只是增加了一点“特殊语料”的C语言,所以可以用printf()代替NSLog().但我们建议使 ...

  2. Wrapper配置详解及高级应用

      将一个简单的程度如HelloWorld 的应用包装秤Wrapper 服务并不复杂,甚至可以认为非常简单.但是实际项目应用过程中我们的程序一般较庞大,运行环境也较复杂. 通过Wrapper 配置文件 ...

  3. 8.Django模型类例子

    这里定义4个模型 作者:一个作者有姓名 作者详情:包括性别,email,出生日期, 出版商:名称,地址,城市,省,国家,网站 书籍:名称,日期 分析: 作者详情和作者一对一的关系 一本书可以有多个作者 ...

  4. 记一次bash脚本报错原因

    准备部署上次写的 爬虫的定时任务,发现sh脚本 写为最简单的cd ,也报错 后来网上一波了解,原因竟是 : 额,格式问题,我为了图方便是在window里用notepad++写的,然后我再linux 系 ...

  5. mysql中子查询更新,得用别名表

    通过查出最大id,来更新记录 update order set status = 'xx' where id in (select v.id from (select max(id) id from ...

  6. matlab 代码加速

    ~ 使用 parfor 代替 for 1. 安装 Parallel Computing Toolbox, 使用 parfor代替for, 使用的是cpu多核,速度能提高三倍左右. ~ 在for循环中的 ...

  7. PHP网页导出Word文档的方法分离

    今天要探讨的是PHP网页导出Word文档的方法,使用其他语言的朋友也可以参考,因为原理是差不多的. 原理 一般,有2种方法可以导出doc文档,一种是使用com,并且作为php的一个扩展库安装到服务器上 ...

  8. Microsoft Office Document Imaging批量ocr 方法

    先将pdf文件->导出->tiff文件,生成pdf每页的tiff文件 使用 G:\SoftWare-new\tiff文件合并拆分工具 将一个导出的单个tiff合并为一个tiff文件 再用 ...

  9. 图片加载Picasso

    https://github.com/square/picasso 基本用法 // 基本用法 // 普通加载图片 Picasso.with(PicassoActivity.this) .load(&q ...

  10. migrate

    数据类型 引用 # :string, :text, :integer, :float,:decimal, :datetime, :timestamp, :time, :date, # :binary, ...