我对模拟退火的理解: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. easyUI参数传递Long型时,前台解析出错的问题——SKY

    果发现datagrid在显示Long类型数据时有问题.问题如下:比如一个数据ID为20121229101239002,经过转换之后的JSON数据也没有问题,但是在显示的时候就会显示为201212291 ...

  2. 我的Android进阶之旅------>Android自定义窗口标题实例

    该实例的功能比较简单,但是通过该实例的扩展可以在自定义标题中做出菜单导航等实用的功能,为了实现自定义窗口标题,需要做以下几个步骤: 1.给自定义标题提供一个界面 2.将自定义标题应用给Activity ...

  3. Unity 武器拖尾效果

    Pocket RPG Weapon Trails 武器拖尾效果 Asset Store地址:https://www.assetstore.unity3d.com/en/#!/content/2458 ...

  4. java使用poi读写Excel

    package com.demo.excel; import com.demo.pojo.Student; import org.apache.poi.hssf.usermodel.HSSFCell; ...

  5. overflow-y:auto 回到顶部

    overflow-y     内容溢出元素框时发生的事情. overflow-y:auto        内容溢出元素框时自动出现滚动条,滑动滚动条显示溢出的内容. 滚动条回到顶部 var conta ...

  6. Python 3 udp 套接字

    Python 3 udp套接字 TCP是建立可靠连接,并且通信双方都可以以流的形式发送数据.相对TCP,UDP则是面向无连接的协议 使用UDP协议时,不需要建立连接,只需要知道对方的IP地址和端口号, ...

  7. new的越界访问

    今天敲代码的时候发现了一个BUG和大家分享一下,希望大家下次不要犯和我一样的错误. 如果犯了和我一样的错,也能知道自己错在哪里!   <(^-^)> 函数如下:(斐波那契数列的实现) lo ...

  8. unigui中TUniDBEdit的OnEndDrag问题

    非常奇怪,unigui中TUniDBEdit未发布OnEndDrag属性,包括其子类:TUniDBNumberEdit.TUniDBFormattedNumberEdit.而其他数据感知组件都有OnE ...

  9. castle windsor学习----- Referencing types in XML 在xm文件中引用类型

    当从xml引用installer的语法如下 <install type="Acme.Crm.Infrastructure.ServicesInstaller, Acme.Crm.Inf ...

  10. FineReport报表使用

    FineReport报表是帆软公司推出的可以嵌入java的免费报表. FineReport有2部分组成,一有c/s端的报表工具制作cpt结尾的报表文件:二是 java调用报表的web程序. 这里主要说 ...