[POJ2420]A Star not a Tree?(模拟退火)
题目链接:http://poj.org/problem?id=2420
求费马点,即到所有其他点总和距离最小的点。
一开始想枚举一个坐标,另一个坐标二分的,但是check的时候还是O(n)的,复杂度相当于O(n^2lgn),没意义。
学习一种神贪心,模拟退火。感觉和启发式搜索有点像啊,又有点像牛顿迭代。
思路就是,固定一个点和一个步长,从这个点开始向四个方向扩展,扩展的步长就是当前步长。如果扩展到的点可以更新答案,那么记住这个点,也就是说这个贪心方向(梯度?)是正确的。就可以拿着这个点继续沿着这个方向走了(剩余的方向可以不考虑了)。
如果四个方向都不能走,说明步长过长,这个时候模拟退火,将步长按比率缩小。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std; typedef pair<double, double> pdd;
#define x first
#define y second
const double eps = 1e-;
const double delta = 0.98;
const int maxn = ;
const int dx[] = {, -, , };
const int dy[] = {, , , -};
int n;
double ret;
pdd cur;
pdd p[maxn]; double dist(pdd a, pdd b) {
double p = a.x - b.x;
double q = a.y - b.y;
return sqrt(p * p + q * q);
} int main() {
// freopen("in", "r", stdin);
while(~scanf("%d", &n)) {
for(int i = ; i < n; i++) {
scanf("%lf %lf", &p[i].x, &p[i].y);
}
int t = ;
ret = 1e100;
cur = pdd(., .);
while(t > eps) {
bool flag = ;
while(flag) {
flag = ;
for(int i = ; i < ; i++) {
pdd pos = pdd(cur.x+dx[i]*t, cur.y+dy[i]*t);
double tmp = .;
for(int j = ; j < n; j++) {
tmp += dist(pos, p[j]);
}
if(ret - tmp > eps) {
ret = tmp;
cur = pos;
flag = ;
break;
}
}
}
t *= delta;
}
printf("%.0f\n", ret);
}
return ;
}
[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? 模拟退火
题目大意: 给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\)) 题解 模拟退火上 #include <cmath> #include <cstdio&g ...
- 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个点,找一个点,使得这个点到其余所有点距离之和最小. 思路 一开始就在抖机灵考虑梯度下降,猜测是个凸 ...
- 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< ...
随机推荐
- 阻止网页内部滚动条mousewheel事件冒泡
function preventScroll(id){ var _this = document.getElementById(id); if(navigator.userAgent.indexOf( ...
- 可爱的Python_课后习题_CDay−5 Python 初体验和原始需求
计算今年是否是闰年.判断闰年条件,满足年份模400 为0,或者模4 为0 但模100不为0. def is_learp_year(year): """判断年份是否为润年& ...
- express 的 app.get和app.use
1.若调用app.get()时只有一个参数,则认为是取设置值,否则认为是注册路由 2.所有被 app.use() 接收的 handle 会被放到一个 stack 里边 app.get() 执行的时候会 ...
- Redis集群~StackExchange.redis连接Twemproxy代理服务器
回到目录 本文是Redis集群系列的一篇文章,主要介绍使用StackExchange.Redis进行Twemproxy(文中简称TW)代理服务的连接过程,事务上,对于TW来说,我们需要理解一下它的物理 ...
- PCB的封装尺寸
PCB封装主要分为贴片式与插件式 1)贴片元件封装说明发光二极管:颜色有红.黄.绿.蓝之分,亮度分普亮.高亮.超亮三个等级,常用的封装形式有三类:0805.1206.121 (常用封装为RB.1/. ...
- Spting--DI/IOC
DI/IOC <bean> 代表由容器构建的对象(通过反射构建,且类必须有无参的构造方法) 公共属性 id="唯一的id" 在容器中是唯一的 name="类 ...
- OpenCV Haartraining
opencv_haartraining.exe -data xml -vec pos.vec -bg neg/neg.txt -w 20 -h 20 -mem 144 opencv_haartrain ...
- 屌丝程序员的梦想 (六) 我也写个开源CMS
离开上家公司之后,我没急着找下家公司,一直以来都是为公司做各个细小的功能却没有属于自己的完整的项目 思来想去,我准备用自己熟悉的thinkphp 和 extjs写一个开源的cms,从用户系统,文章系统 ...
- category用法
1.今天在复习之前的category的时候,遇到一个这样子的问题.查来一下,原来苹果的官方文档中有说明,苹果时不提倡我们在类别中重写原文件的内容,如果要重写,就继承他,然后重写,但是在项目中,有很多前 ...
- 浅谈js回调函数
回调函数原理: 我现在出发,到了通知你”这是一个异步的流程,“我出发”这个过程中(函数执行),“你”可以去做任何事,“到了”(函数执行完毕)“通知你”(回调)进行之后的流程 例子 1.基本方法 ? 1 ...