[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 ...
随机推荐
- MySQL5.7更改用户名密码
更改用户名密码,官方推荐使用alter ALTER USER test@'%' IDENTIFIED BY '; 还有一种 update mysql.user set authentication_s ...
- P3567 [POI2014]KUR-Couriers
题目描述 Byteasar works for the BAJ company, which sells computer games. The BAJ company cooperates with ...
- windows2008r2系统破解登录密码方法
破解windows 2008 r2系统登录密码方法: 1.重启系统,使用windows2008r2安装光盘引导 按住shift+f10 2.切换到d:windows\system32目录(使用cmd. ...
- 转载:使用Nginx的必备软件(1.3.2)《深入理解Nginx》(陶辉)
原文:https://book.2cto.com/201304/19612.html 如果要使用Nginx的常用功能,那么首先需要确保该操作系统上至少安装了如下软件. (1)GCC编译器 GCC(GN ...
- Ex 6_12 凸多边形的最优三角剖分..._第六次作业
假设顶点的总数为n,从0到n-1. 从序号为0的顶点开始以逆时针方向排序,对于 令子问题A[i,j]为包含顶点i,i+1, . . . j的凸多边形的最小三角剖分代价,dist(i,j)为顶点i到顶点 ...
- [HTML]点击按钮,页面总是跳回顶端的解决方法(Clicking an button,always resets the view to top of page)
1 前言 当网页页面较长或者表单较多时,右侧会出现滚动条,然而经常会出现点击底部的<button>按钮或者<a>超链接,会出现点击后,当前页面会回到顶端. 2 方案 例如样例代 ...
- Go语言规格说明书 之 内建函数(Built-in functions)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...
- Vue.js+Koa2移动电商实战 笔记
地址:http://jspang.com/ https://github.com/shenghy/SmileVue 1.vant https://www.youzanyun.com/zanui/va ...
- Android 截屏与 WebView 长图分享经验总结
最近在做新业务需求的同时,我们在 Android 上遇到了一些之前没有碰到过的问题,截屏分享. WebView 生成长图以及长图在各个分享渠道分享时图片模糊甚至分享失败等问题,在这过程中踩了很多坑,到 ...
- poj1470 LCA倍增法
倍增法模板题 #include<iostream> #include<cstring> #include<cstdio> #include<queue> ...