POJ-2420 A Star not a Tree? 梯度下降 | 模拟退火
题目链接: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? 梯度下降 | 模拟退火的更多相关文章
- 三分 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? 爬山算法
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? (计算几何-费马点)
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3435 Accepted: 172 ...
- 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? —— 模拟退火
题目:http://poj.org/problem?id=2420 给出 n 个点的坐标,求费马点: 上模拟退火. 代码如下: #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?【爬山法】
题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...
- 【POJ】2420 A Star not a Tree?(模拟退火)
题目 传送门:QWQ 分析 军训完状态不好QwQ,做不动难题,于是就学了下模拟退火. 之前一直以为是个非常nb的东西,主要原因可能是差不多省选前我试着学一下但是根本看不懂? 骗分利器,但据说由于调参困 ...
随机推荐
- hdu1542 矩形面积并(线段树+离散化+扫描线)
题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
- Python数据分析8-----网页文本处理
1.去除网页的标签,如<br/> from bs4 import BeautifulrSoup preData=BeautifulSoup(data,'html.parser').get_ ...
- POJ 2356 Find a multiple( 鸽巢定理简单题 )
链接:传送门 题意:题意与3370类似 注意:注意输出就ok,输出的是集合的值不是集合下标 /***************************************************** ...
- [luogu4133 BJOI2012] 最多的方案 (计数dp)
题目描述 第二关和很出名的斐波那契数列有关,地球上的OIer都知道:F1=1, F2=2, Fi = Fi-1 + Fi-2,每一项都可以称为斐波那契数.现在给一个正整数N,它可以写成一些斐波那契数的 ...
- Shell(五)Shell输入/输出重定向
Shell 输入/输出重定向 大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回到您的终端.一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端.同样,一个命令 ...
- Python 绘图与可视化 matplotlib(上)
参考链接:https://www.cnblogs.com/dudududu/p/9149762.html 更详细的:https://www.cnblogs.com/zhizhan/p/5615947. ...
- [luogu]P3572 [POI2014]PTA-Little Bird(单调队列)
P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...
- javascript深度克隆函数deepClone
javascript深度克隆函数deepClone function deepClone(obj) { var _toString = Object.prototype.toString; // nu ...
- vuejs 基础总结(one)
vuejs 入门知识点 1.active-class 是哪个组件的属性?嵌套路由怎么定义 (1).active-class 是 vue-router 模块的 router-link 组件的属性 (2) ...
- 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] D】Single-use Stones
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设长度为L的所有区间里面,石头的个数的最小值为k 设取到k的区间为l,r 那么k就为最多能通过的青蛙个数. 假设k再大一点.比如为k ...