题目链接: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. 前端框架easyui layout, Tabs,tree

    一.三大前端框架的 1.easyui=jquery+html4(用来做后台的管理界面) 不要钱,开发速度快,不好看,不支持响应式 2.bootstrap=jquery+html5 好看,开发速度快,部 ...

  2. adb屏幕截屏

    import subprocess #执行结果使用管道输出,对于参数是字符串,需要指定shell=Trueprocess = subprocess.Popen('adb shell screencap ...

  3. win10x64位系统中nodejs的安装和配置

    官网http://nodejs.cn/download/ 2.下载完成后点击安装包 下一步,安装过的,这里根据自己的需求选择.选择第直接正常安装. 这一步是安装的内容,第一个是安装所有的模块,建议全部 ...

  4. IOS - JSON数据解析 小3种方法

    [manager GET:serverURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject ...

  5. 2019-03-19 SQL Server简单存储过程的创建 删除 执行

    --创建名为 Get 的有输入参数的存储过程 create proc Get --设置默认值 @TrustId int ='001' as begin select * from [DealStruc ...

  6. 【【henuacm2016级暑期训练】动态规划专题 H】Greenhouse Effect

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 原题意等价于:给你一个序列(实数的位置没用!)..你可以改变其中某些元素的位置(插入到某些位置中间. 然后让他变成有序的. (有序的 ...

  7. python多线程编程代码

    参考了这篇文章,写的挺好的. http://blog.csdn.net/abcjennifer/article/details/49474897 import time import threadin ...

  8. extjs动态导入

    Ext.Loader.setConfig({enabled: true}); Ext.Loader.setPath("util", "../wx/jsUtil" ...

  9. spring boot系统学习(知识点笔记)

    一.http的注解配置 1.@SpringBootAplication=@SpringBootConfiguration(其实就是个@Configuration)+@EnableAutoConfigu ...

  10. iOS开发之十万个为什么&lt;1&gt;

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...