[POJ2420]A Star not a Tree?
来源:
Waterloo Local 2002.01.26
题目大意:
找出$n$个点的费马点。
思路:
模拟退火。
首先任取其中一个点(或随机一个坐标)作为基准点,每次向四周找距离为$t$的点,如果找到的点总距离更小,就把该点作为新的基准点。
每次找完后降低温度$t$,重复上述过程,直到温度$t$低于阈值$threshold$。
另外注意在POJ上double类型输出不能用%lf,只能用%f,一开始因为这个WA了好几次。
#include<cmath>
#include<cstdio>
const double inf=1e9;
const int N=;
const double threshold=1e-,delta=0.98,initial_temperature=;
const double d[][]={{,},{,},{,-},{-,}};
int n;
struct Point {
double x,y;
};
Point p[N];
inline double sqr(const double x) {
return x*x;
}
inline double dist(const Point a,const Point b) {
return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
inline double getsum(const Point x) {
double ret=;
for(int i=;i<n;i++) {
ret+=dist(x,p[i]);
}
return ret;
}
int main() {
scanf("%d\n",&n);
for(int i=;i<n;i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
}
Point nowp=p[];
double ans=getsum(nowp),t=initial_temperature;
while(t>threshold) {
bool finished=false;
while(!finished) {
finished=true;
for(int i=;i<;i++) {
Point nextp;
nextp.x=nowp.x+d[i][]*t;
nextp.y=nowp.y+d[i][]*t;
double tmp=getsum(nextp);
if(tmp<ans) {
ans=tmp;
nowp=nextp;
finished=false;
}
}
}
t*=delta;
}
printf("%.0f\n",ans);
return ;
}
[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 ...
- Poj2420 A Star not a Tree? 模拟退火算法
题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...
- 【模拟退火】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)的,复杂度相 ...
- poj2420 A Star not a Tree? 模拟退火
题目大意: 给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\)) 题解 模拟退火上 #include <cmath> #include <cstdio&g ...
- [日常摸鱼]poj2420 A Star not a Tree?
题意:给定$n$个点,找一个点使得这个点到所有点的距离之和最小,求出这个最小距离 传说中的模拟退火- #include<cstdio> #include<ctime> #inc ...
- 模拟退火算法A Star not a Tree?(poj2420)
http://write.blog.csdn.net/postedit A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Tot ...
随机推荐
- ftpdata目录下日期目录权限问题
由于APP Server由root用户启动,创建目录默认为root:root用户:用户组权限 需chown -R wingupload ftpdata执行后,WING才能上传成功 或者写成脚本,每天凌 ...
- freeRTOS中文实用教程1--任务
1.前言 FreeRTOS是小型多任务嵌入式操作系统,硬实时性.本章主要讲述任务相关特性及调度相关的知识. 2. 任务的总体特点 任务的状态 (1)任务有两个状态,运行态和非运行态 (2)任务由非运行 ...
- gunicorn+flask使用与配置
gun.conf的内容 import os bind = '10.1.240.222:5000' workers = 4 backlog = 2048 worker_class = "syn ...
- centos7.2环境下安装smokeping对网络状态进行监控
centos7.2环境下安装smokeping对网络状态进行监控 安装smokeping建议用centos7,用centos6.5一直卡在smokeping那里,下载不了perl的扩展插件,可能是因为 ...
- 【linux】centos6.9安装gearman
1.确认yum源没问题,如果有问题,参照这里更换 2. yum install -y boost-devel gperf libevent-devel libuuid-devel yum instal ...
- Python 定值类
1.__str__和__repr__ 如果要把一个类的实例变成 str,就需要实现特殊方法__str__(): class Person(object): def __init__(self, nam ...
- Hibrenate关系映射(一对一外键关联)
一.一对一(单向):使用外部索引将其中的一个类作为parent,相对应的一个就是子类,并且参照父 类的主键ID来生成数据库表.(比如:可以将husband中设置一个wife_id对应wife中的主键i ...
- 步步为营-33-Md5(32)加密与Base64加密
说明: 1:直接贴码 using System; using System.Collections.Generic; using System.ComponentModel; using System ...
- node.js开发博客系统---前端项目搭建(一)
Express: https://github.com/petecoop/generator-express 安装node.js和npm 执行: npm install -g yo npm insta ...
- 【Algorithm | 链表】单链表“环”、“环的起点”、“环的长度”问题
参考资料 • Floyd判圈算法 { 链接 } • 单链表“环”.“环的起点”.环的长度”问题 { 链接 } 链表环的问题 一.判断链表有换 使用两个指针slow和fast.两个指针开始时均在头节点处 ...