Country Meow(模拟退火)

Country Meow

[Link](Attachments - 2018-2019 ACM-ICPC, Asia Nanjing Regional Contest - Codeforces)

题意

给你三维平面的一些点,让你找到一个位置到这些点里最远的点最近。

题解

一次模拟退火中温度需要覆盖数据范围的三倍,我们用温度(步长)来不断的随机下去,设置一个终止温度,以及每次的衰减系数,每一次随机一个新的点dt = f(now) - f(pre), 以求更进的距离为例如果(dt < 0) 直接跳到新点,否则有一定概率跳到,一般的概率函数选择 p = e − d t t p=e^{\frac {-dt}{t}} p=etdt。使得不断迫近最优解,

模拟退火有一个初始温度,温度越高,接受较差的解的可能性就越大。每次走完后,都会降低温度,使得接受较差解的可能性变小。在走的过程中,更新最优解的值。

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <ctime>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-5, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
struct Point {
    double x, y, z;
    Point () {}
    Point (double _x, double _y, double _z) {
        x = _x, y = _y, z = _z;
    }
}p[N];
double res = 1e18;
double dist(Point a, Point b) {
    double dx = a.x - b.x, dy = a.y - b.y, dz = a.z - b.z;
    return sqrt(dx * dx + dy * dy + dz * dz);
}
double rand(double l, double r) {
    return (double)rand() / RAND_MAX * (r - l) + l;
}
double calc(Point a) {
    double mxd = 0;
    for (int i = 0; i < n; i ++ ) mxd = max(mxd, dist(a, p[i]));
    res = min(res, mxd);
    return mxd;
}
void simulate_anneal() {
    Point a, b;
    a = {rand(-100000,100000), rand(-100000, 100000), rand(-100000, 100000)};
    for (double t = 100000; t > eps; t *= 0.999) {
        b = {rand(a.x - t, a.x + t), rand(a.y - t, a.y + t), rand(a.z - t, a.z + t)};
        double dt = calc(b) - calc(a);
        if (exp(-dt / t) > rand(0, 1)) a = b;
    }
}
int main() {
         cin >> n;
        for (int i = 0; i < n; i ++ ) cin >> p[i].x >> p[i].y >> p[i].z;
        for (int i = 0; i < 50; i ++ ) simulate_anneal();
        printf("%.10lf\n", res);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值