Problem D. Country Meow (三分|模拟退火)

Problem D. Country Meow
Input file: standard input
Output file: standard output
In the 24th century, there is a country somewhere in the universe, namely Country Meow. Due to advanced
technology, people can easily travel in the 3-dimensional space.
There are N cities in Country Meow. The i-th city is located at (xi, yi, zi) in Cartesian coordinate.
Due to the increasing threat from Country Woof, the president decided to build a new combatant
command, so that troops in different cities can easily communicate. Hence, the Euclidean distance between
the combatant command and any city should be minimized.
Your task is to calculate the minimum Euclidean distance between the combatant command and the
farthest city.
Input
The first line contains an integer N (1 ≤ N ≤ 100).
The following N lines describe the i-th city located.Each line contains three integers xi, yi, zi(−100000 ≤ xi, yi, zi ≤ 100000).
Output
Print a real number — the minimum Euclidean distance between the combatant command and the farthest
city. Your answer is considered correct if its absolute or relative error does not exceed 10−3
. Formally, let
your answer be a, and the jury’s answer be b. Your answer is considered correct if |a−b|
max(1,|b|) ≤ 10−3
.
Examples
standard input standard output
3
0 0 0
3 0 0
0 4 0
2.500000590252103
4
0 0 0
1 0 0
0 1 0
0 0 1

// 模拟退火代码
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;

struct node {
    double x, y, z;
} s[N], a, b;
double ans = 1e8;
int n;

double get_len(node a, node b) {
    double dx = a.x - b.x;
    double dy = a.y - b.y;
    double dz = a.z - b.z;
    return sqrt(dx * dx + dy * dy + dz * dz);
}
double calc(node a) {
    double m = 0;
    for (int i = 0; i < n; i++) m = max(m, get_len(a, s[i]));
    ans = min(ans, m);
    return m;
}
double rand(double l, double r) {
    return (double)rand() / RAND_MAX * (r - l) + l;
}
void solve() {
    a = {rand(-100000, 100000), rand(-100000, 100000), rand(-100000, 100000)};
    for (double t = 100000; t > 0.000001; 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() {
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i < n; i++) cin >> s[i].x >> s[i].y >> s[i].z;
    for (int i = 0; i < 50; i++) {
        solve();
    }
    printf("%.10f\n", ans);
    return 0;
}
// 三分代码
#include <bits/stdc++.h>
using namespace std;

const int inf = 0x3f3f3f3f;
const int N = 110;
double x[N], y[N], z[N], p[3];
int n;
double dis(double a, double b, double c) 
{
    double ans = 0, s;
    for (int i = 0; i < n; i++) 
    {
        s = (a - x[i]) * (a - x[i]) + (b - y[i]) * (b - y[i]) + (c - z[i]) * (c - z[i]);
        ans = max(ans, sqrt(s));
    }
    return ans;
}
double solve(int x) 
{
    if (x == 3) return dis(p[0], p[1], p[2]);
    double l = -1e5, r = 1e5, ans = inf;
    while (abs(l - r) > 1e-3) 
    {
        double l1, l2, ans1, ans2;
        l1 = l + (r - l) / 3;
        l2 = r - (r - l) / 3;
        p[x] = l1;
        ans1 = solve(x + 1);
        p[x] = l2;
        ans2 = solve(x + 1);
        if (ans1 > ans2) 
           l = l1,ans = ans2;
        else 
           r = l2,ans = ans1;
    }
    return ans;
}
int main() 
{
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i < n; i++) 
    {
        cin >> x[i] >> y[i] >> z[i];
    }
    printf("%.10f\n", solve(0));
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值