题目链接: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. Unity 退出游戏 方法

    Application.Quit(); 嗯,没错,这篇就这么短.

  2. mysql中redo和binlog的区别

    影响MySQL中redo的配置参数: innodb_log_file_size:指定每个redo日志大小,默认值48MB innodb_log_files_in_group:指定日志文件组中redo日 ...

  3. Pyhton学习——Day47

    # 转载:http://www.cnblogs.com/yuanchenqi/articles/6357507.html# 外键:一种约束条件,与主键对应# 主表:被绑定的表:字表# 外键约束:# - ...

  4. Server初见——python

    import socketphone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)phone.bind(('127.0.0.1',8080))p ...

  5. CF 689D - Friends and Subsequences

    689D - Friends and Subsequences 题意: 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb( ...

  6. ES6 学习6 数组的扩展

    本章学习要点: 扩展运算符 Array.from() Array.of() 数组实例的 copyWithin() 数组实例的 find() 和 findIndex() 数组实例的 fill() 数组实 ...

  7. web前后端安全问题

    1. 安全问题主要可以理解为以下两方面: 私密性:资源不被非法窃取和利用,只有在授权情况下才可以使用: 可靠性:资料不会丢失.损坏及篡改: 2. web安全的层面 代码层面:写代码时保证代码是安全的, ...

  8. NOI 2018 屠龙勇士 (拓展中国剩余定理excrt+拓展欧几里得exgcd)

    题目大意:略 真是一波三折的一道国赛题,先学了中国剩余定理,勉强看懂了模板然后写的这道题 把取出的宝剑攻击力设为T,可得Ti*x=ai(mod pi),这显然是ax=c(mod b)的形式 这部分用e ...

  9. 洛谷——P2822 组合数问题

    https://www.luogu.org/problem/show?pid=2822 题目描述 组合数C_n^mC​n​m​​表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三 ...

  10. 海尔公布首套智能套装SmartCare,主打智能、简单

        2014CIE智能硬件首发会于11月6-8日在上海国际展览中心举行.吸引了海尔.英特尔.百度等知名企业的积极參与. 作为国内智能家居领域的先驱者,海尔在本次大会上首发了轻量智能套装SmartC ...