我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html

我对爬山的理解:https://www.cnblogs.com/AKMer/p/9555215.html

题目传送门:http://poj.org/problem?id=2420

这题就是要我们求平面图费马点……

然后我似乎先写了广义费马点……顺序错了……至于对费马点的解释去这里看吧……

BZOJ3680吊打XXX:https://www.cnblogs.com/AKMer/p/9588724.html

时间复杂度:\(O(能A)\)

空间复杂度:\(O(能A)\)

爬山算法代码如下:

#include <ctime>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std; #define sqr(x) ((x)*(x)) const int maxn=105; int n;
double ans=1e18,ansx,ansy; struct computer {
double x,y;
}p[maxn]; double len() {
double x=rand()%200000-100000;
return x/100000;
} double dis(double x1,double y1,double x2,double y2) {
return sqrt(sqr(x1-x2)+sqr(y1-y2));
} double calc(double x,double y) {
double tmp=0;
for(int i=1;i<=n;i++)
tmp+=dis(x,y,p[i].x,p[i].y);//除了这里没有乘权值就跟吊打XXX没有任何区别了
if(tmp<ans) {ans=tmp;ansx=x;ansy=y;}
return tmp;
} int main() {
srand(time(0));
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
ansx+=p[i].x,ansy+=p[i].y;
}ansx/=n;ansy/=n;double now_x=ansx,now_y=ansy;
for(int T=1e7;T>=1e-7;T*=0.98) {
double nxt_x=now_x+len()*T;
double nxt_y=now_y+len()*T;
if(calc(nxt_x,nxt_y)<calc(now_x,now_y))
now_x=nxt_x,now_y=nxt_y;
}
printf("%.0lf\n",ans);
return 0;
}

模拟退火代码如下:

#include <ctime>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std; #define sqr(x) ((x)*(x)) const int maxn=101;
const double T_0=1e-7;
const double del_T=0.98; int n;
double ans=1e18,ansx,ansy; struct computer {
double x,y;
}p[maxn]; double len() {
double x=rand()%200000-100000;
return x/100000;
} double dis(double x1,double y1,double x2,double y2) {
return sqrt(sqr(x1-x2)+sqr(y1-y2));
} double calc(double x,double y) {
double tmp=0;
for(int i=1;i<=n;i++)
tmp+=dis(x,y,p[i].x,p[i].y);
if(tmp<ans) {ans=tmp;ansx=x;ansy=y;}
return tmp;
} void Anneal() {
double T=10000000,now_x=ansx,now_y=ansy;
while(T>=T_0) {
double nxt_x=now_x+len()*T;
double nxt_y=now_y+len()*T;
double tmp1=calc(now_x,now_y);
double tmp2=calc(nxt_x,nxt_y);
if(tmp2<tmp1||exp((tmp1-tmp2)/T)*RAND_MAX>rand())
now_x=nxt_x,now_y=nxt_y;
T*=del_T;
}
} int main() {
srand(time(0));
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
ansx+=p[i].x,ansy+=p[i].y;
}ansx/=n;ansy/=n;Anneal();
printf("%.0lf\n",ans);
return 0;
}

因为这题要我们求的答案是总距离(并且还是保存整数位的),所以相对吊打XXX简单多了。两种算法都可以\(A\)我自己造的数据

POJ2420:A Star not A Tree?的更多相关文章

  1. POJ 2420:A Star not a Tree?

    原文链接:https://www.dreamwings.cn/poj2420/2838.html A Star not a Tree? Time Limit: 1000MS   Memory Limi ...

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

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

  3. 【POJ2420】A star not a tree?

    蒟蒻开始学模拟退火…… 起初一直不肯学,因为毕竟玄学算法…… 哎呀玄学怎么就没用呢?对不对? #include<iostream> #include<cstdio> #incl ...

  4. 【POJ】【2420】A Star not a Tree?

    模拟退火 Orz HZWER 这题的题意是在二维平面内找一点,使得这点到给定的n个点的距离和最小……0.0 模拟退火算法请戳这里 //POJ 2420 #include<ctime> #i ...

  5. poj-2420 A Star not a Tree?(模拟退火算法)

    题目链接: A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5219   Accepte ...

  6. POJ2069:Super Star

    我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...

  7. 模拟退火算法A Star not a Tree?(poj2420)

    http://write.blog.csdn.net/postedit A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  8. poj2420 A Star not a Tree? 找费马点 模拟退火

    题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...

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

随机推荐

  1. swift打印方法名文件路径

    1.打印方法名 print(#function) 1.打印文件路径 print(#file)

  2. Unicode Category

    Lu(letter,uppercase):大写字母. Ll(letter.lowercase):小写字母. Lt(letter,titlecase):词首字母大写的字母. Lm(letter,modi ...

  3. python+NLTK 自然语言学习处理六:分类和标注词汇一

    在一段句子中是由各种词汇组成的.有名词,动词,形容词和副词.要理解这些句子,首先就需要将这些词类识别出来.将词汇按它们的词性(parts-of-speech,POS)分类并相应地对它们进行标注.这个过 ...

  4. crontab定时任务写法记录

    基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4列表示 ...

  5. python基础5 ---python文件处理

    python文件处理 一.文件处理的流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件的操作方法 1.文件打开模式格式: 文件句柄 = open('文件路径', ...

  6. LeetCode:柠檬水找零【860】

    LeetCode:柠檬水找零[860] 题目描述 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向 ...

  7. jQuery:[1]实现图片上传并预览

    jQuery:[1]实现图片上传并预览 原理 预览思路 1.当上传对象的input被触发并选择本地图片之后获取要上传的图片对象的URL: 2.把对象URL赋值给实现写好的img标签的src属性 Fil ...

  8. HTML相对路径与绝对路径

    在网页制作的过程中,少不了跟路径打交道,比如,包含一个文件,插入一个图片等,与路径都有关系,如果使用了错误的文件路径,就会导致引用失效(无法浏览链接文件,或无法显示插入的图片等).初学者可能会感到困惑 ...

  9. jQuery图片水平滑动延迟加载动画

    在线演示 本地下载

  10. 2.微信小程序-B站:需要先知道这些

    文件结构 小程序包含一个描述整体程序的 app 和多个描述各自页面的 page.一个小程序主体部分由三个文件组成,必须放在项目的根目录,如下: 文件 必须 作用 app.js 是 小程序逻辑 app. ...