[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 ...
随机推荐
- maven的动态打包功能
对于maven而言,打包是其一个非常重要的功能,不仅仅是简单的编译打包的概念,其还通过各种插件支持各种灵活的打包策略.现举一个例子讲解如何动态实现一个web项目的打包: 需求: 现需要对一个web项目 ...
- mybatis主键自增长
在数据库中建序列:SEQ_T_RESOURCE 第一种:会在传入该方法的的参数集合中添加主键元素,相当于:map.put("RES_ID",xxx); <insert id= ...
- rabbitmq 源码安装
官网地址:rabbitmqhttp://www.rabbitmq.com/releases/rabbitmq-server/官网地址:erlanghttp://erlang.org/download/ ...
- python中对列表和循环使用的小练习
#author devilf product_list = [ (), (), (), (), () ] shop_list = [] salary = input('pls enter your s ...
- Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
# yum install -y vim Loaded plugins: fastestmirror, presto Loading mirror speeds from cached hostfil ...
- Spring整合strus2简单应用总结
本身strus2没接触过,所以这块学的一知半解,正常不整合的还没学(接着学) step: 1.创建web工程 2.在/WEB-INF/lib引入jar包 asm-3.3.jarasm-commons- ...
- Protocol Buffers简明教程
随着微服务架构的流行,RPC框架渐渐地成为服务框架的一个重要部分. 在很多RPC的设计中,都采用了高性能的编解码技术,Protocol Buffers就属于其中的佼佼者. Protocol Buffe ...
- JDBC辅助类封装 及应用
一:代码图解: 二:配置文件: driverClassName=com.mysql.jdbc.Driver url=jdbc\:mysql\://127.0.0.1\:3306/xlzj_sh_new ...
- JQuery 插件一般方法
如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...
- Reactor模型-多线程程版
1.概述 在Reactor单线程版本的设计中,I/O任务乃至业务逻辑都由Reactor线程来完成,这无疑增加了Reactor线程的负担,高负载情况下必然会出现性能瓶颈.此外,对于多处理器的服务器来说, ...