[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 ...
随机推荐
- 内核中container_of宏的详细分析【转】
转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html 内核中container_of宏的详细分析 16年2月28日09:00:37 内核中 ...
- gdb revert, Go to previous line in gdb
Yes! With the new version 7.0 gdb, you can do exactly that! The command would be "reverse-step& ...
- genstr.py
#!/usr/bin/python #-*- coding:utf-8 –*- import os import sys import re import shutil import xlrd imp ...
- @RequestBody,@ResponseBody
@RequestBody 作用: i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上: ...
- centos中selinux功能及常用服务配置
SELinux: Secure Enhenced Linux 常用命令 获取selinux的当前状态: # getenforce 临时启用或禁用: # setenfoce 0|1 永久性启用,需要修改 ...
- CopyPropertis
commons-beanutils.jar PropertyUtils.copyProperties(Object dest, Object orig) spring-beans.jar BeanUt ...
- Storm的部署
配置方案如下 node1 Nimbus zookeeper node2 Supervisor zookeeper node3 Supervisor zookeeper node4 Supervisor ...
- Python 列表推导、迭代器与生成器
1.列表推导 1 2 3 4 5 6 7 8 9 10 11 numbers = [i for i in range(10) if i % 2 == 0] print(numbers) seq = ...
- 性能测试七:jmeter进阶之文件上传下载、定时器
一.上传下载 上传: 1,POST请求,勾选 use …for post 2,同请求一起发送文件里,填写文件名称,参数名称 3,MIME类型: application/octet-stream(非必须 ...
- Chakra调试笔记 TypedArray
一.TypedArray类型 TypedArray是漏洞中常见到的结构,手册用法有四 1.new TypedArray(length); //byteLength=length * sizeof(Ty ...