模拟退火基本知识
其伪代码例如以下:

Let s = s0
For k = 0 through k_max (exclusive):T := temperature(k / k_max)Pick a random neighbour, s_new := neighbour(s)If P(E(s), E(s_new), T) > random(0, 1), move to the new state:s := s_new
Output: the final state s

样例:

poj 2420

题意:
平面上给你n个点(xi,yi),让你求一个点。到这n点的距离和最小。

限制:
1 <= n <= 100
0 <= xi,yi <= 1e4, 为整数

/*poj 2420题意:平面上给你n个点(xi,yi),让你求一个点,到这n点的距离和最小。限制:1 <= n <= 1000 <= xi,yi <= 1e4, 为整数思路:模拟退火模拟退火基本知识:其伪代码例如以下:Let s = s0For k = 0 through k_max (exclusive):T := temperature(k / k_max)Pick a random neighbour, s_new := neighbour(s)If P(E(s), E(s_new), T) > random(0, 1), move to the new state:s := s_newOutput: the final state s
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
const double E = exp(1.0);
struct Pt{double x,y;
}p[105];
double sqr(double x){return x*x;
}
double dis(Pt a, Pt b){return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));
}
double get_sum(Pt p0, int n){double ret = 0;for(int i = 0; i < n; ++i)ret += dis(p0, p[i]);return ret;
}
int main(){srand(1123);int n;int limit = 10000; //(x,y)的范围while(scanf("%d", &n) != EOF){double x0 = 0, y0 = 0;for(int i = 0; i < n; ++i){scanf("%lf%lf", &p[i].x, &p[i].y);x0 += p[i].x;y0 += p[i].y;}x0 /= n;y0 /= n;double ans = get_sum((Pt){x0, y0}, n);double temp = 1e5; //初始温度, 依据题目改动while(temp > 0.02){ //0.02为温度的下限, 若温度temp达到下限, 则停止搜索double x = 0, y = 0;for(int i = 0; i < n; ++i){ //获取步长的规则依据题目而定x += (p[i].x - x0) / dis((Pt){x0, y0}, p[i]);y += (p[i].y - y0) / dis((Pt){x0, y0}, p[i]);}double tmp = get_sum((Pt){x0 + x * temp, y0 + y * temp}, n); //标函数E(x_new);if(tmp < ans){ans = tmp;x0 += x * temp;y0 += y * temp;} else if(pow(E, (ans - tmp) / temp) > (rand() % limit) / (double)limit){ans = tmp;x0 += x * temp;y0 += y * temp;}temp *= 0.9; //0.9为降温退火速率, (范围为0~1, 越大得到全局最优解的概率越高, 执行时间越长}printf("%.0f\n", ans);}return 0;
}

poj 2069 Super Star

题意:
给定n个点(xi, yi, zi), 要求覆盖这些点的最小球半径。

限制:
4 <= n <= 30
0 <= xi, yi, zi <= 100

/*poj 2069 Super Star题意:给定n个点(xi, yi, zi), 要求覆盖这些点的最小球半径。限制:4 <= n <= 300 <= xi, yi, zi <= 100思路:模拟退火*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
using namespace std;
const double E = exp(1.0);
struct Pt{double x, y, z;
}p[35];
double sqr(double x){return x * x;
}
double dis(Pt a, Pt b){return sqrt(sqr(a.x - b.x) +sqr(a.y - b.y) +sqr(a.z - b.z));
}
double get_max_r(Pt p0, int n){double ret = 0;for(int i = 0; i < n; ++i)ret = max(ret, dis(p0, p[i]));return ret;
}
int main() {int n;int limit = 100;while(scanf("%d", &n) && n){double x0 = 0, y0 = 0, z0 = 0;for(int i = 0; i < n; ++i){scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z);x0 += p[i].x;y0 += p[i].y;z0 += p[i].z;}x0 /= n;y0 /= n;z0 /= n;//cout<<x0<<' '<<y0<<' '<<z0<<endl;double ans = get_max_r((Pt){x0, y0, z0}, n);double temp = 1e5;while(temp > 1e-8){double x = 0, y = 0, z = 0;double max_r = 0;for(int i = 0; i < n; ++i){double r = dis((Pt){x0, y0, z0}, p[i]);if(r > max_r){x = (p[i].x - x0) / r;y = (p[i].y - y0) / r;z = (p[i].z - z0) / r;max_r = r;}}double tmp = get_max_r((Pt){x0 + x * temp,y0 + y * temp,z0 + z * temp}, n);if(tmp < ans){ans = tmp;x0 += x * temp;y0 += y * temp;z0 += z * temp;} else if(pow(E, (ans - tmp) / temp) > (rand() % limit) / (double)limit){ans = tmp;x0 += x * temp;y0 += y * temp;z0 += z * temp;}temp *= 0.998;}printf("%.5f\n", ans);}return 0;
}

转载于:https://www.cnblogs.com/jzdwajue/p/6913402.html

模拟退火 (poj 2420, poj 2069)相关推荐

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

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

  2. POJ 1573 POJ 2632(两道有趣的Robot)实例

    /* ** POJ 2632 Crashing Robots ** Created by Rayn @@ 2014/04/16 ** 坑爹的模拟题,脑壳不清晰的就要被坑惨了 */ #include & ...

  3. poj 1515+poj 1438(边双连通)

    题目链接:http://poj.org/problem?id=1515 思路:题目的意思是说将一个无向图改成有向图,使其成为强连通,输出所有的边.我们可以求无向图的边双连通分量,对于同一个双连通分量, ...

  4. poj 3279 poj 1753

    poj 3279: Description Farmer John knows that an intellectually satisfied cow is a happy cow who will ...

  5. Bailian2694 逆波兰表达式(POJ NOI0202-1696, POJ NOI0303-1696)【文本】

    问题链接:POJ NOI0202-1696 逆波兰表达式. 问题链接:POJ NOI0303-1696 逆波兰表达式. 逆波兰表达式 描述 逆波兰表达式是一种把运算符前置的算术表达式,例如普通的表达式 ...

  6. POJ - 3984+POJ - 3414(BFS+路径记录)

    题目一 迷宫问题 题目链接: http://poj.org/problem?id=3984 题目: Description 定义一个二维数组: int maze[5][5] = {0, 1, 0, 0 ...

  7. 线段树区间染色 浮水法 学习小记 Poj 2777 + Poj 2528

    复习过的东西必须时常拿来练练,虽说都是水题.... Poj 2777 典型的区间染色问题,貌似我的写法为了不数组越界必须多开一倍的数组空间.....还没有想到解决方法,有机会参考一下别人的写法. Po ...

  8. 【POJ】POJ题目分类

    转自:http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期: 一.基本算法:      (1)枚举. (poj1753, ...

  9. POJ 计算几何入门题目推荐

      其实也谈不上推荐,只是自己做过的题目而已,甚至有的题目尚未AC,让在挣扎中.之所以推荐计算几何题,是因为,本人感觉ACM各种算法中计算几何算是比较实际的算法,在很多领域有着重要的用途(例如本人的专 ...

  10. poj计算几何题推荐

    POJ 计算几何入门题目推荐(转)       其实也谈不上推荐,只是自己做过的题目而已,甚至有的题目尚未AC,让在挣扎中.之所以推荐计算几何题,是因为,本人感觉ACM各种算法中计算几何算是比较实际的 ...

最新文章

  1. 指针小白:修改*p与p会对相应的地址的变量产生什么影响?各个变量指针的长度为多少?...
  2. [转]关于MyEclipse下的项目无法使用BASE64Encoder问题的解决办法
  3. docker从私有仓库Harbor, push 及 pull 镜像
  4. python处理大型矩阵_在python中处理大型密集矩阵
  5. 【深度学习】图像数据集处理常用方法合集(部分基于pytorch)
  6. html单选框背景图片,如何更改radio、checkbox选项框背景图?
  7. 【数字信号处理】序列傅里叶变换 ( 序列傅里叶变换定义详细分析 | 证明单位复指数序列正交完备性 | 序列存在傅里叶变换的性质 | 序列绝对可和 → 序列傅里叶变换一定存在 )
  8. Spring MVC生成PDF文件代码示例
  9. C++设计模式之工厂方法模式
  10. Careercup - Microsoft面试题 - 5428361417457664
  11. 基于代码生成器的JAVA快速开发平台----JEECG 3.7.8 版本发布
  12. java删_JAVA删除文件夹
  13. 合作式智能运输系统 车用通信系统应用层及应用数据交互标准 第二阶段_为什么一定要了解OPC UA TSN——未来的工业通信标准...
  14. Exchange Server 2007迁移Exchange Server 2010 (15)---启用Outlook anywhere
  15. BI系统的应用组织思路与数据分析模式
  16. 苹果手用计算机解锁手机密码,苹果手机怎么强制解锁 iPhone强制解锁密码教程...
  17. Python福彩3D单选单复式排列计算器
  18. 单片机外设篇——SPI协议
  19. cannot open shared object file
  20. supersu二进制更新安装失败_Supersu提示更新二进制文件解决方案

热门文章

  1. 设置将Maven的jar包发布到lib
  2. HDU 5351 MZL's Border (规律,大数)
  3. 使用result配置结果视图
  4. vmware开机自动进入BIOS vmware 进入BIOS方法
  5. mysql binlog rotate_mysql binlog日志存储格式
  6. 苹果6用U盘越狱_iOS12.3.1 A12 支持在线越狱?作者黑人问号
  7. 如何把APP加到HTML,如何将 Microsoft 服务添加到你的应用 (HTML)
  8. 在mybatis里面配置外部资源文件
  9. Nginx源码分析 - 核心模块初始化顺序,根据ctx创建上下文
  10. mac上利用minikube搭建kubernetes(k8s)环境