题目链接:https://cn.vjudge.net/problem/POJ-2420

题意

给出n个点,找一个点,使得这个点到其余所有点距离之和最小。

思路

一开始就在抖机灵考虑梯度下降,猜测是个凸优化问题,完全在抖机灵。

最后实在是没得其他思路了,看了看题解。

居然是模拟退火,而且写的貌似没有随机这个因素,完全是爬山法好吧?

梯度下降,复杂度O(60000n)

提交过程

WA 偏导方程没给对
AC 其实maxEpoch没必要这么大,只要发现多次best值更新小于1即可退出循环

代码

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const double learningRate=1.4, step=1;
const int maxEpoch=60000, maxn=100+20;
double nx[maxn], ny[maxn];
int n;
struct Point{
double x, y;
Point(double x, double y):x(x), y(y) {}
};
double getDis(int ax, int ay, int bx, int by){
return sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by));
} double criter(Point p){
double dis=0;
for (int i=0; i<n; i++)
dis+=getDis(p.x, p.y, nx[i], ny[i]);
return dis;
} double SGD(void){
double x=0.5e4, y=0.5e4, best;
for (int epoch=0; epoch<=maxEpoch; epoch++){
double base=criter(Point(x, y));
double dx=(criter(Point(x+step, y))-base)/step;
double dy=(criter(Point(x, y+step))-base)/step; x-=dx*learningRate;
y-=dy*learningRate;
if (epoch==0) best=base;
else best=min(best, base); // if ((epoch+1)%1000==0)
// printf("%.3f,%.3f %.3fbase %.3fbest\n", dx, dy, base, best);
}return criter(Point(x, y));
} int main(void){
while (scanf("%d", &n)==1 && n){
for (int i=0; i<n; i++) scanf("%lf%lf", &nx[i], &ny[i]);
printf("%.0f\n", SGD());
} return 0;
}
Time Memory Length Lang Submitted
563ms 400kB 1221 G++ 2018-08-09 04:36:51

POJ-2420 A Star not a Tree? 梯度下降 | 模拟退火的更多相关文章

  1. 三分 POJ 2420 A Star not a Tree?

    题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...

  2. [POJ 2420] A Star not a Tree?

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4058   Accepted: 200 ...

  3. 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 ...

  4. POJ 2420 A Star not a Tree? (计算几何-费马点)

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3435   Accepted: 172 ...

  5. poj 2420 A Star not a Tree?——模拟退火

    题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include ...

  6. poj 2420 A Star not a Tree? —— 模拟退火

    题目:http://poj.org/problem?id=2420 给出 n 个点的坐标,求费马点: 上模拟退火. 代码如下: #include<iostream> #include< ...

  7. POJ 2420 A Star not a Tree?(模拟退火)

    题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...

  8. POJ 2420 A Star not a Tree?【爬山法】

    题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...

  9. 【POJ】2420 A Star not a Tree?(模拟退火)

    题目 传送门:QWQ 分析 军训完状态不好QwQ,做不动难题,于是就学了下模拟退火. 之前一直以为是个非常nb的东西,主要原因可能是差不多省选前我试着学一下但是根本看不懂? 骗分利器,但据说由于调参困 ...

随机推荐

  1. ABBYY简体中文版终身授权半价来袭,真的是5折!

    经过了一个春秋,心心念念的双十一终于要来了,一年时间并不长,但这一个月尤其慢!ABBYY官方称为回馈广大用户的支持与厚爱,双十一期间,ABBYY价格感人,诱惑难挡. 说到双十一活动,方式也是五花八门, ...

  2. Windows桌面美化

    [工具链接]链接: https://pan.baidu.com/s/12aUGsu91F8WfaW5HU5ps3g 提取码: dnan [样例] [美化步骤] 1.解压下载文件,安装两个软件: Sta ...

  3. tomcat闪退无法启动 the catalina_home environment variable is not defined correctly this environment variable is needed to run this program

    未成功配置CATALINA_HOME 1.计算机>属性>环境变量, 新建环境变量.变量名为CATALINA_HOME ,变量值tomcat的解压目录,注意后面不用多加“\”或者“;” 2. ...

  4. 修改UTC时间

    /sbin/hwclock --systohc date按照时间修正.

  5. urlrewrite地址重写的使用

    地址重写: 主要是为了站点的安全. 比如我们平时的地址请求 地址重写前,訪问路径是: /read.egov?action=read&bid=2 地址重写后,訪问路径是:/read-read-2 ...

  6. linux中sed的使用方法具体解释(对行数据的加入、删除等)

    sed使用语法 [root@fwq test]# sed --help 使用方法: sed [选项]... {脚本(假设没有其它脚本)} [输入文件]... -n, --quiet, --silent ...

  7. WPF获取和设置鼠标位置与progressbar的使用方法

    一.WPF 中获取和设置鼠标位置 方法一:WPF方法 Point p = Mouse.GetPosition(e.Source as FrameworkElement); Point p = (e.S ...

  8. m_Orchestrate learning system---十二、为什么thinkphp验证场景里面的多个属性之间是逗号

    m_Orchestrate learning system---十二.为什么thinkphp验证场景里面的多个属性之间是逗号 一.总结 一句话总结:因为是数组啊 1 protected $scene ...

  9. 编程与算法中的端点问题(linspace(a, b, n),endpoint)

    左闭右开,[0, n) ⇒ [0, n-1],共 n 个元素: 1. 列表长与编号 列表(seq,也可以是数组等线性结构)的长度要比末尾元素的编号多 1,比如一个列表内含有 5 个元素,最后一个元素的 ...

  10. ORA-1157错误解决

    一.错误描述 ORA-1157, "cannot identify/lock data file %s - see DBWR trace file" 引起的原因: 因为数据文件已经 ...