【POJ】2420 A Star not a Tree?(模拟退火)
题目
传送门:QWQ
分析
军训完状态不好QwQ,做不动难题,于是就学了下模拟退火。
之前一直以为是个非常nb的东西,主要原因可能是差不多省选前我试着学一下但是根本看不懂?
骗分利器,但据说由于调参困难,很多情况比不上多点爬山?
总体来说模拟退火的精髓就是:

可以看到T越大,转移的概率越高。 exp是在cmath里面有的,直接用就ok。
本题就是找出n个点的费马点,即找出一个点到这n个点的距离和最小。
代码
// #include <bits/stdc++.h>
// POJ 2420
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxn=;
const double pace=0.95;
struct Point { double x,y; }; int n;
Point p[maxn]; double sqr(double x){ return x*x; }
double dis(double x,double y,Point a){ return sqrt(sqr(x-a.x)+sqr(y-a.y)); }
double getnum(double x,double y){
double ans=;
for(int i=;i<=n;i++) ans+=dis(x,y,p[i]);
return ans;
}
int main(){
// srand(time(NULL));
srand();
Point ans;
scanf("%d",&n);
for(int i=;i<=n;i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
ans.x+=p[i].x; ans.y+=p[i].y;
}
ans.x/=n; ans.y/=n;
double t=1e5, end=0.02, pre=getnum(ans.x,ans.y);
while(t>end){ double xx=, yy=;
t*=pace;
for(int i=;i<=n;i++){
xx+=(p[i].x-ans.x)/dis(ans.x,ans.y,p[i]);
yy+=(p[i].y-ans.y)/dis(ans.x,ans.y,p[i]);
}
double tmp=getnum(ans.x+xx*t,ans.y+yy*t);
if(tmp<pre){
pre=tmp; ans.x+=xx*t; ans.y+=yy*t;
}
else if(exp((pre-tmp)/t)>(rand()%)/10000.0){
ans.x+=xx*t; ans.y+=yy*t;
}
t*=pace;
}
printf("%.0f",pre);
return ;
}
【POJ】2420 A Star not a Tree?(模拟退火)的更多相关文章
- poj 2420 A Star not a Tree? —— 模拟退火
题目:http://poj.org/problem?id=2420 给出 n 个点的坐标,求费马点: 上模拟退火. 代码如下: #include<iostream> #include< ...
- poj 2420 A Star not a Tree?——模拟退火
题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include ...
- POJ 2420 A Star not a Tree?(模拟退火)
题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...
- 三分 POJ 2420 A Star not a Tree?
题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...
- [POJ 2420] A Star not a Tree?
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4058 Accepted: 200 ...
- POJ 2420 A Star not a Tree? (计算几何-费马点)
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3435 Accepted: 172 ...
- POJ 2420 A Star not a Tree? 爬山算法
B - A Star not a Tree? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...
- POJ 2420 A Star not a Tree?【爬山法】
题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...
- uva 10228 - Star not a Tree?(模拟退火)
题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...
随机推荐
- Git内网服务搭建全过程
看到一篇搭建git服务器的文章,主要是公司内网搭建的,讲得非常详细,比廖雪峰的要完整,必须赞! http://developer.51cto.com/art/201507/483448.htm
- Linux下mysql操作
1.linux下MYSQL的启动与访问 http://www.cnblogs.com/hunter007/articles/2251795.html 2.linux下mysql基本的操作 http:/ ...
- react 部分ES6写法
react+react-router+antd 栗子:https://github.com/Aquarius1993/reactApp 模块: 1. 引入模块 import React from 'r ...
- SVM核技巧之终极分析
参考文献: http://www.blogjava.net/zhenandaci/archive/2009/03/01/257237.html http://www.cnblogs.com/jerry ...
- caffe测试mnist问题-error while loading shared libraries
问题描述 build/examples/mnist/convert_mnist_data.bin: error while loading shared libraries: libcudart.so ...
- 【vue】vue-cli 脚手架项目简介(一) - package.json
vue-cli是用来生成 vue项目的命令行工具,它的使用方法是这样的: vue init <template-name> <project-name>第二个参数 templa ...
- 使用systemd严格保证启动顺序
需求: 服务B要在服务A之后启动,且由于存在强内在依赖关系,B必须在A完成初始化之后才能被启动. 解决方法: 首先使用systemd,service脚本需要配置服务B要after服务A. 其次,A服务 ...
- ThinkPHP5 控制器中怎么实现 where id = 2 or id = 3 这个查询语句?
使用 whereOr whereIn(); (来自 ★C̶r̶a̶y̶o̶n-杭州 ) 为什么不用数组啊,array('eq', array(1,2),'or') (来自 supler)
- yarn 知识点
yarn 与 npm 功能对应表格: 命令 yarn npm 初始化 yarn init npm init 安装项目所有包(注意) yarn npm install 添加 dependencies y ...
- MSCRM 2011中过滤化查询的实现方法和禁用选择视图
MS CRM 2011中过滤化查询的实现方法 使用过滤化查询,可以实现根据表单上的其他信息作为搜索条件,限制Lookup查找框中的搜索结果数量的目的. 在讲解具体的实现方法之前,先设定一个应用场景: ...