poj2420 A Star not a Tree? 模拟退火
题目大意:
给定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? 模拟退火的更多相关文章
- poj-2420 A Star not a Tree?(模拟退火算法)
题目链接: A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5219 Accepte ...
- Poj2420 A Star not a Tree? 模拟退火算法
题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...
- poj2420 A Star not a Tree? 找费马点 模拟退火
题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...
- uva 10228 - Star not a Tree?(模拟退火)
题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...
- 【模拟退火】poj2420 A Star not a Tree?
题意:求平面上一个点,使其到给定的n个点的距离和最小,即费马点. 模拟退火的思想是随机移动,然后100%接受更优解,以一定概率接受更劣解.移动的过程中温度缓慢降低,接受更劣解的概率降低. 在网上看到的 ...
- 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)的,复杂度相 ...
- POJ 2420 A Star not a Tree?(模拟退火)
题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...
- poj2420A Star not a Tree?(模拟退火)
链接 求某一点到其它点距离和最小,求这个和,这个点 为费马点. 做法:模拟退火 #include <iostream> #include<cstdio> #include< ...
随机推荐
- 大组合取模之:1<=n<=m<=1e6,1<=p<=1e9
/****************************** 大组合取模之:1<=n<=m<=1e6,1<=p<=1e9 使用:程序最开始调用getprime(),需要 ...
- This instability is a fundamental problem for gradient-based learning in deep neural networks. vanishing exploding gradient problem
The unstable gradient problem: The fundamental problem here isn't so much the vanishing gradient pro ...
- 摇一摇js代码
init(); var SHAKE_THRESHOLD = 3000; var last_update = 0; var x = y = z = last_x = last_y = last_z = ...
- MySQL查看和修改字符集的方法
一.查看字符集 1.查看MYSQL数据库服务器和数据库字符集 方法一:show variables like '%character%';方法二:show variables like 'collat ...
- 关于随机浏览头伪装fake-UserAgent
使用: from fake_useragent import UserAgent ua = UserAgent() #ie浏览器的user agent print(ua.ie) Mozilla/5.0 ...
- linux 指令(经常更新)
添加一个服务 # sudo update-rc.d 服务名 defaults 99 删除一个服务 # sudo update-rc.d 服务名 remove 临时重启一个服务 # /etc/init. ...
- [原创]java WEB学习笔记43:jstl 介绍,core库详解:表达式操作,流程控制,迭代操作,url操作
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 【Flask】查询分页问题处理
遇到两次查询结果分页的问题, 查询出结果后, 翻页时导致查询条件失效. 处理方式 1. 路由中不放page参数 写成 @testfile.route("/test-file", m ...
- Tab动画菜单
在线演示 本地下载
- ScreenOS学习笔记
安全区段 第2层 V1-Trust 同一区段内的接口通信不需要策略,不同区段之间的接口通信则需要策略. Global区段没有接口 V1-Untrust V1-DMZ 第3层 Trust Untrust ...