算法題中模擬退火算法的兩種實現(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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章