模拟退火 (poj 2420, poj 2069)
来源:互联网 发布:护手霜推荐 知乎 编辑:程序博客网 时间:2024/05/17 06:37
模拟退火基本知识
其伪代码如下:
例子:
poj 2420
题意:
平面上给你n个点(xi,yi),让你求一个点,到这n点的距离和最小。
限制:
1 <= n <= 100
0 <= xi,yi <= 1e4, 为整数
poj 2069 Super Star
题意:
给定n个点(xi, yi, zi), 要求覆盖这些点的最小球半径。
限制:
4 <= n <= 30
0 <= xi, yi, zi <= 100
其伪代码如下:
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
例子:
poj 2420
题意:
平面上给你n个点(xi,yi),让你求一个点,到这n点的距离和最小。
限制:
1 <= n <= 100
0 <= xi,yi <= 1e4, 为整数
/*poj 2420 题意: 平面上给你n个点(xi,yi),让你求一个点,到这n点的距离和最小。 限制: 1 <= n <= 100 0 <= xi,yi <= 1e4, 为整数 思路: 模拟退火 模拟退火基本知识: 其伪代码如下: 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*/#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 <= 30 0 <= 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;}
0 0
- 模拟退火 (poj 2420, poj 2069)
- POJ 2420 模拟退火
- poj 2420 模拟退火
- POJ 2420 模拟退火求费马点
- poj 2420 & poj1379 模拟退火
- 模拟退火算法 POJ 2420
- 模拟退火 poj 1379
- poj 1379 模拟退火
- poj 3285 模拟退火,
- POJ 1379 模拟退火
- POJ 1379模拟退火
- 【模拟退火】POJ 2420/ZOJ 1901 费马点
- POJ 2420 二分法 模拟退火算法
- POJ 2420 模拟退火 解题报告
- poj 2069 super star 模拟退火
- POJ 2069 最小球覆盖模拟退火
- POJ, 2069 Super Star(模拟退火算法)
- POJ 2069 Super Star(模拟退火)
- Vim 文本编辑器主要内容
- Spark进阶视频之Scala中上下文界定内幕中的隐式参数与隐式参数的实战详解及其在Spark中的应用源码解析
- Android中各种drawable的使用
- 电影评分星级显示效果的实现
- 关于项目跟进
- 模拟退火 (poj 2420, poj 2069)
- GridView的按下或点击选中后的效果如何去除
- 如何控制 VC 中控件间的TAB切换顺序
- 餐饮软件简单的分析和一些看法
- 【前端】浮动后父容器高度自适应
- 开始
- 互联网产品的测试
- 文件小结
- android压力测试命令monkey详解