[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 ...
随机推荐
- C# 汉字与区位码之间的相互转换(中文数字字母可以,支持空格,但是特殊字符未来得及测试)
using System; using System.Text; namespace Test { class MainClass { /// <summary> /// 中文空白字符,用 ...
- tomcat端口冲突,多个tomcat同时启动问题
一台PC机上安装了两个tomcat,需要同时启动,每个tomcat上跑一个程序,但是现在提示端口号冲突,需要手动更改. 需要修改/conf/server.xml四个地方: 1.<Server p ...
- oracle 分区 查询
显示数据库所有分区表的信息:DBA_PART_TABLES 显示当前用户可访问的所有分区表信息:ALL_PART_TABLES 显示当前用户所有分区表的信息:USER_PART_TABLES 显示表分 ...
- cocos2d-x在App中的应用
cocos2d-x是一个应用广泛的开源游戏引擎,主要是应用与开发2D游戏,开源运行于多个平台,如果只是针对于移动端平台而言,可以运行于android和ios平台. cocos2d-x目前的版本是3.1 ...
- 调试Windows Service
调试Windows Service 使用一般的调试方法调试不了Windows Servers,所以参考了一些调试方法 我服务源码中使用了Timer,注意不能使用工具箱内的Timer,用System.T ...
- django----数据库操作(对model增删改查)
优化查询 articles_list=models.Article.objects.all().iterator() for i in articles_list: print(i.title) 添加 ...
- 自动化测试使用cookie跳过验证码
准备工具: fiddler Python+selenium 安装fidder fidder官方下载地址 fidder首次安装需要设置才能抓取https参考如下 fidder设置抓取https 开始 1 ...
- String:(字符串)中常用的方法
package stringyiwen; //字符串中常用的方法public class StringTest03 { public static void main(String[] args) { ...
- ERP完善合同起草(二十八)
前端的代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CRMCont ...
- GDIPlus非典型误用一例
// ** 初始化GDI+ Gdiplus::GdiplusStartupInput gdiplusStartupInput; // ** 该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标 ...