【模拟退火】【爬山算法】模板

学习博客
题目链接
玄学算法。。。一开始不知道调参wa到怀疑人生
后来尝试改一下delta竟然过了。。。退火和爬山的区别其实就是当没有更优解的时候退火会以一个概率来接收,爬山就不接受。所以把else if(exp((res-temp)/t)*RAND_MAX > rand())注释掉就是爬山了。。

#pragma GCC optimize(2)
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define ft first
#define sd second
#define all(c) ((c).begin()), ((c).end())
#define mp(a, b) make_pair(a, b)
#define pb(x) push_back(x)
const int maxn = 1e5+5;
typedef long long ll;
const ll mod = 1e9+7;
int Case = 1;
int n, m;
struct node{
    double x, y, z;
}cc[maxn];
double res, t;
const double delta = 0.993;
double resx, resy;
double cal(double X, double Y) {
    double rt = 0;
    for(int i = 1; i <= n; i++) {
        double deltax = X-cc[i].x, deltay = Y-cc[i].y;
        rt  += sqrt(deltax*deltax+deltay*deltay)*cc[i].z;
    }
    return rt;
}
void simulate_anneal() {
    t = 2000;
    double x = resx, y = resy;
    while(t > 1e-14) {
        double X = x + ((rand()<<1)-RAND_MAX)*t;
        double Y = y + ((rand()<<1)-RAND_MAX)*t;
        double temp = cal(X, Y);
        if(temp < res) {
            res = temp;
            resx = X;resy = Y;
            x = X;y = Y;
        }
        //else if(exp((res-temp)/t)*RAND_MAX > rand())x = X, y = Y;
        t *= delta;
    }
}
void solve() {
    cin>>n;res = 1e18;
    resx = 0, resy = 0;
    for(int i = 1; i <= n; i++)
        {cin>>cc[i].x>>cc[i].y>>cc[i].z; resx += cc[i].x; resy += cc[i].y;}
    resx /= n;resy /= n;
    simulate_anneal();
    simulate_anneal();
    simulate_anneal();
    printf("%.3f %.3f\n", resx, resy);
    return;
}
int main() {
    srand(19260817);
    ios::sync_with_stdio(false);cin.tie(0);std::cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt","w",stdout);
#endif
    //scanf("%d", &Case);
    while(Case--){
        solve();
    }
    return 0;
}
全部评论

相关推荐

HR_丸山彩同学:你的项目描述里,系统设计讲了很多:MemCube是什么、三级存储架构怎么设计、四种遗忘策略分别是什么。这些面试的时候讲没问题,但简历上不需要这么细。 简历要突出的是影响力,不是实现细节。面试官看简历的时候想知道的是「这个项目有多大价值」,不是「这个项目具体怎么实现的」。实现细节是面试时候聊的 怎么改:技术细节可以精简为一句「采用三级存储架构+四种遗忘策略」,把省出来的篇幅用来写影响力。比如:项目有没有开源?有没有写成技术博客?有没有被别人使用过? 校园经历没有任何信息量,任何人都可以写这句话,写了等于没写。更关键的是,你投的是技术岗,校园活动经历本来就不是加分项。如果非要写,必须写出具体的数字和成果。如果你没有这些数字,那就老老实实删掉 「端到端耗时缩减30-40%」要给出确切数字和绝对值。从1000ms降到600ms是降了40%,从100ms降到60ms也是降了40%,但这两个含义完全不一样。其他也是,涉及到数据,准备好证据,口径统一,面试会问 「熟练」「熟悉」「了解」混在一起用,读起来很乱。而且「了解前端需求」最好改成「具备前后端协作经验」
点赞 评论 收藏
分享
01-12 09:24
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务