三维最小球覆盖(模拟退火)

地址:https://codeforces.com/gym/101981/attachments

代码:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <math.h>
#include <map>
#include <sstream>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <array>
#include <climits>
using namespace std;
#define   LL long long
#define ull unsigned long long 
#define LD long double 
const LL MAX = (LL)1e18;
const int  N = 1e3 + 10;
const double eps = 1e-3;
const double PI = acos(-1);
typedef struct Point 
{
    double x, y, z;
    Point operator -(const Point  &u)const 
    {
       return  {x - u.x, y - u.y, z - u.z};
    }

};
int n;
Point p[N];
double getlen(Point a, Point b) 
{
    Point d=a-b;
    return sqrt(d.x * d.x + d.y * d.y + d.z * d.z);
}
double solve() 
{
    double st = 1000, rate = 0.99999;
    Point now = p[1];
    double ans = MAX;
    while (st > eps) 
    {
        int cur = 0;
        double maxx =0;
        for (int i = 1; i <= n; i++)
            if (getlen(now, p[i]) > maxx) cur = i, maxx = getlen(now, p[i]);
        ans = min(ans, getlen(now, p[cur]));
        now.x += (p[cur].x - now.x) * (st / 1000);
        now.y += (p[cur].y - now.y) * (st / 1000);
        now.z+= (p[cur].z - now.z) * (st / 1000);
        st *= rate;
    }
    //cout << now.x << " " << now.y << " " << now.z << endl;
    return ans;

}
int  main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y >> p[i].z;
    printf("%.10lf\n", solve());
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值