【BZOJ3680】【JSOI2004】平衡点/吊打XXX(膜你退火)

Description

click me


Solution

膜你退火模板题。

大概的思路是:
设定一个初始温度 T T ,每次降温×dt,温度越高,移动幅度越大,可以接受更劣解的概率越大。


Code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    puts("nan nan");
    return 0;
}

















滑稽

真·Code

/************************************************
 * Au: Hany01
 * Date: May 28th, 2018
 * Prob: [BZOJ3680] 吊打XXX
 * Email: hany01@foxmail.com
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const double eps = 1e-15;
const int maxn = 1005, Times = 15;

int n, w[maxn], x[maxn], y[maxn];

double randdec(double T) { return ((rand() + rand()) - RAND_MAX) * 1. / RAND_MAX * T * 1e-2; }

inline double calc(double nx, double ny)
{
    double sx = 0, sy = 0, len, dx, dy;
    For(i, 1, n)
    {
        dx = x[i] - nx, dy = y[i] - ny, len = sqrt(dx * dx + dy * dy);
        if (fabs(len) < eps) continue;
        sx += w[i] * dx / len, sy += w[i] * dy / len;
    }
    return sqrt(sx * sx + sy * sy);
}

int main()
{
#ifdef hany01
    File("bzoj3680");
#endif

    //random_device rand;

    static double xba, yba, ansx, ansy, tba, ans;

    n = read();
    For(i, 1, n) xba += x[i] = read(), yba += y[i] = read(), w[i] = read();
    ansx = xba /= n, ansy = yba /= n, tba = ans = calc(xba, yba);

    for (int Case = Times; Case --; )
    {
        double nowx = xba, nowy = yba, now = tba, res, newx, newy;
        for (double T = 1e6; T >= eps; T *= 0.99)
        {
            newx = nowx + randdec(T), newy = nowy + randdec(T), res = calc(newx, newy);
            if (res < ans) ans = res, ansx = newx, ansy = newy;
            if (res < now || exp((now - res) / T) * RAND_MAX < rand())
                nowx = newx, nowy = newy, now = res;
        }
    }
    printf("%.3lf %.3lf\n", ansx, ansy);

    return 0;
}
//为君持酒劝斜阳,且向花间留晚照。
//    -- 宋祁《玉楼春·春景》
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值