题目大意:

给定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. Java 学习 day09

    01-面向对象(内部类访问规则) package myFirstCode; /* 内部类的访问规则: 1. 内部类可以直接访问外部类的成员,包括私有private. 之所以可以直接访问外部类中的成员, ...

  2. 【基础版限时免费】致敬WebForms,ASP.NET Core也能这么玩!

    ASP.NET WebForms ASP.NET WebForms 随着微软 2000 年的 .Net Framework 一起发布,至今也将近 20 年的时间.相信很多人和我一样,对 WebForm ...

  3. 【BZOJ2006】[NOI2010]超级钢琴 ST表+堆

    [BZOJ2006][NOI2010]超级钢琴 Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以 ...

  4. 大数据学习系列(5)-- 局域网yum仓库搭建

    https://www.cnblogs.com/nulige/p/6081192.html

  5. IIS架构介绍

    IIS7及以上版本提供的请求-处理架构包括以下内容: Windows Process Activation Service(WAS)可以让站点支持更多协议,不仅仅是HTTP和HTTPS 可以通过增加或 ...

  6. ural 1303 Minimal Coverage【贪心】

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...

  7. Python菜鸟之路:Python基础-类(1)——概念

    什么是类? 在python中,把具有相同属性和方法的对象归为一个类(class).类是对象的模板或蓝图,类是对象的抽象化,对象是类的实例化.类不代表具体的事物,而对象表示具体的事物. 类的创建 cla ...

  8. 我的Android进阶之旅------>HTTP Header 详解

    HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...

  9. GeekforGeeks Trie - 键树简单介绍 - 构造 插入 和 搜索

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  10. LeetCode:位运算实现加法

    LeetCode:位运算实现加法 写在前面 位运算符 实现加法的思路 两个加数,比如5(101)和6(110),如何不用加法就能得出两者之和呢? 我们知道二进制计算中,如果使用异或将会产生无进位的两者 ...