18年南京区域赛D【三分套三分套三分】【模拟退火】

三分的模板套一套就行了没啥好说的。。

#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#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 cal(double x, double y, double z) {
    double rt = 0;
    for(int i = 1; i <= n; i++) {
        double dx = x-cc[i].x, dy = y-cc[i].y, dz = z-cc[i].z;
        rt = max(rt, sqrt(dx*dx+dy*dy+dz*dz));
    }
    return rt;
}
double C2(double x, double y) {
    double res = 1e18;
    double rz = 100000, lz = -100000;
    for(int i = 1; i<= 50; i++) {
        double l = lz+(rz-lz)/3, r = lz+(rz-lz)/3*2;
        double ls = cal(x, y, l), rs = cal(x, y, r);
        res = min(res, min(ls, rs));
        if(ls > rs) lz = l;
        else rz = r;
    }
    return res;
}
double C1(double x) {
    double res = 1e18;
    double ry = 100000, ly = -100000;
    for(int i = 1; i<= 50; i++) {
        double l = ly+(ry-ly)/3, r = ly+(ry-ly)/3*2;
        double ls = C2(x, l), rs = C2(x, r);
        res = min(res, min(ls, rs));
        if(ls > rs) ly = l;
        else ry = r;
    }
    return res;
}
void solve() {
    double res = 1e18;
    for(int i = 1; i <= n; i++) {
        scanf("%lf%lf%lf", &cc[i].x, &cc[i].y, &cc[i].z);
    }
    double rx = 100000, lx = -100000;
    for(int i = 1; i<= 50; i++) {
        double l = lx+(rx-lx)/3, r = lx+(rx-lx)/3*2;
        double ls = C1(l), rs = C1(r);
        res = min(res, min(ls, rs));
        if(ls > rs) lx = l;
        else rx = r;
    }
    printf("%.5lf\n", res);
    return;
}
int main() {
    //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(scanf("%d", &n) == 1 && n){
        solve();
    }
    return 0;
}

模拟退火也是板子。。。一发就过了。。

#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, resz;
double cal(double X, double Y, double Z) {
    double rt = 0;
    for(int i = 1; i <= n; i++) {
        double deltax = X-cc[i].x, deltay = Y-cc[i].y, deltaz = Z-cc[i].z;
        rt  = max(rt, sqrt(deltax*deltax+deltay*deltay+deltaz*deltaz));
    }
    return rt;
}
void simulate_anneal() {
    t = 2000;
    double x = resx, y = resy, z = resz;
    while(t > 1e-14) {
        double X = x + ((rand()<<1)-RAND_MAX)*t;
        double Y = y + ((rand()<<1)-RAND_MAX)*t;
        double Z = z + ((rand()<<1)-RAND_MAX)*t;
        double temp = cal(X, Y, Z);
        if(temp < res) {
            res = temp;
            resx = X;resy = Y;resz = Z;
            x = X;y = Y;z = Z;
        }
        else if(exp((res-temp)/t)*RAND_MAX > rand())x = X, y = Y, z = Z;
        t *= delta;
    }
}
void solve() {
    res = 1e18;
    resx = 0; resy = 0; resz = 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;resz += cc[i].z;}
    resx /= n;resy /= n;resz /= n;
    simulate_anneal();
    simulate_anneal();
    simulate_anneal();
    printf("%.5f\n", res);
    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(cin>>n){
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值