算法题中模拟退火算法的两种实现(POJ 1379、SCU 4369)

一、步长 *= 降火速度

二、当某次随机出来所有方向的点都没有比当前点更接近正解时,步长 *= 降火速度

// 一、poj 1379
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
using namespace std;

const int N = 11111, P = 15, D = 30;  // P为随机出来的点数,D为随机出来的方向数
const double EPS = 1e-5, PI = acos(-1.0), INF = 1e20;

int m;
double dist[N];

struct Point
{
	double x, y;
	Point() {}
	Point(double x, double y) : x(x), y(y) {}
} p[N], tp[N];

Point operator - (const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); }

inline double Veclen(const Point &a) { return sqrt(a.x * a.x + a.y * a.y); }

inline double cal(const Point &a)
{
	double ret = INF;
	for(int i = 0; i < m; ++i) {
		ret = min(ret, Veclen(p[i] - a));
	}
	return ret;
}

int main()
{
	srand(time(0));
	int t, x, y;
	scanf("%d", &t);
	while(t-- && scanf("%d%d%d", &x, &y, &m)) {
		for(int i = 0; i < m; ++i) {
			scanf("%lf%lf", &p[i].x, &p[i].y);
		}
		for(int i = 0; i < P; ++i) {
			tp[i].x = (rand() % 1000 + 1) / 1000.0 * x;
			tp[i].y = (rand() % 1000 + 1) / 1000.0 * y;
			dist[i] = cal(tp[i]);
		}
		double step = Veclen(Point(x * 1.0, y * 1.0)) / 2;   // 此处为初始步长
		while(step > EPS) {
			for(int i = 0; i < P; ++i) {
				Point cur, pre = tp[i];
				for(int j = 0; j < D; ++j) {
					double ang = (rand() % 1000 + 1) / 1000.0 * 2 * PI;
					cur = Point(pre.x + cos(ang) * step, pre.y + sin(ang) * step);
					if(cur.x < 0 || cur.x > x || cur.y < 0 || cur.y > y) continue;
					double ret = cal(cur);
					if(ret > dist[i]) {
						dist[i] = ret;
						tp[i] = cur;
					}
				}
			}
			step *= 0.8;   // 0.8为降火速度
		}
		int ans = 0;
		for(int i = 1; i < P; ++i) {
			if(dist[i] > dist[ans]) {
				ans = i;
			}
		}
		printf("The safest point is (%.1f, %.1f).\n", tp[ans].x, tp[ans].y);
	}
	return 0;
}


// 二、SCU 4369
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>

const int N = 2333, D = 8;
const double EPS = 1e-7, PI = acos(-1.0);

struct Point
{
	double x, y, w;
	Point() { x = y = w = 0; }
	Point(double x, double y) : x(x), y(y) {}
	void input() { scanf("%lf%lf%lf", &x, &y, &w); }
} p[N];

Point operator - (const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); }

inline double Veclen(const Point &a) { return sqrt(a.x * a.x + a.y * a.y); }

int n;

double cal(const Point &a)
{
	double ret = 0;
	for(int i = 0; i < n; ++i) {
		ret += Veclen(p[i] - a) * p[i].w;
	}
	return ret;
}

int main()
{
	srand(time(0));
	while(~scanf("%d", &n)) {
		for(int i = 0; i < n; ++i) {
			p[i].input();
		}
		Point a;
		for(int i = 0; i < n; ++i) {
			a.x += p[i].x * p[i].w;
			a.y += p[i].y * p[i].w;
			a.w += p[i].w;
		}
		a.x /= a.w, a.y /= a.w, a.w /= n;
		double step = 20, val = cal(a);
		while(step > EPS) {
			Point cur, pre = a;
			bool flag = 0;
			for(int i = 0; i < D; ++i) {
				double ang = (rand() % 1000) / 1000.0 * 2 * PI;
				cur.x = pre.x + cos(ang) * step;
				cur.y = pre.y + sin(ang) * step;
				double ret = cal(cur);
				if(ret + EPS < val) {
					val = ret;
					a = cur;
					flag = 1;
				}
			}
			if(!flag) {
				step *= 0.5;
			}
		}
		printf("%.2f\n", val / n);
	}
	return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值