题目

给出 $N(1 \leq N \leq 100)$ 个点的坐标 $x_i,y_i,z_i$($-100000 \leq x_i,y_i,z_i \leq 100000$),求包围全部点的最小的球。

2018南京区域赛D题

分析

方法一:模拟退火

模拟退火是 解决最小球覆盖的经典方法,效果也非常好。

随机得到球的中心,如果更小的半径或设定的概率,则转移。(详细解释见链接

//这个代码严格说不是模拟退火

有一个事实:最小球的球心,它不然是一个确定的点,就是距它最远的4个点且等距

于是,我们任选一个点作为初始球心,再在点集中找到距它最远的点,我们让球心靠近最远的点,同时使步长逐渐变小,不断重复此过程,就可以让球心到达正确的位置

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <set>
#define LL long long
using namespace std;
const int MAXN = ;
const double eps = 1e-;
struct Point
{
double x, y, z;
Point(double x = , double y = , double z = ) : x(x), y(y), z(z) { }
};
Point operator - (Point A, Point B)
{
return Point(A.x - B.x, A.y - B.y, A.z - B.z);
}
double dis(Point A, Point B)
{
double x = A.x - B.x;
double y = A.y - B.y;
double z = A.z - B.z;
return sqrt(x * x + y * y + z * z);
}
int n;
Point p[MAXN];
double SA(Point start)
{
double delta = 100.0;
double ans = 1e20;
while(delta > eps)
{
int d = ;
for(int i=;i<n;i++)
{
if(dis(p[i], start) > dis(p[d], start))
d = i;
}
double r = dis(start, p[d]);
ans = min(ans, r);
start.x += (p[d].x - start.x) / r * delta;
start.y += (p[d].y - start.y) / r * delta;
start.z += (p[d].z - start.z) / r * delta;
delta *= 0.98;
}
return ans;
}
int main()
{
int T, kcase = ;
//scanf("%d", &T);
while(scanf("%d", &n) && n)
{
//scanf("%d", &n);
for(int i=;i<n;i++)
scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z);
printf("%.8f\n", SA(Point(,,)));
}
return ;
}

这有一个严格按模拟退火的定义写的(精度较低,但更好理解

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 1, i##_end_ = (n); i <= i##_end_; ++i)
using namespace std;
typedef pair<int, int> pii;
typedef long long ll; const double eps = 1e-;
int sgn(double x) {
if(fabs(x) < eps) return ;
return x < ? - : ;
}
struct Point {
double x, y, z;
Point(double xp=, double yp=, double zp=): x(xp), y(yp), z(zp) { }
Point operator + (const Point& rhs) const { return Point(x+rhs.x, y+rhs.y, z+rhs.z); }
Point operator - (const Point& rhs) const { return Point(x-rhs.x, y-rhs.y, z-rhs.z); }
Point operator * (const double& k) const { return Point(x*k, y*k, z*k); }
Point operator / (const double& k) const { return Point(x/k, y/k, z/k); }
bool operator < (const Point& rhs) const { return x < rhs.x || (x==rhs.x && y<rhs.y) || (x==rhs.x&&y==rhs.y&&z<rhs.z); }
bool operator == (const Point& rhs) const {return sgn(x - rhs.x) == && sgn(y - rhs.y) == && sgn(z-rhs.z)==; }
void scan() { scanf("%lf%lf%lf", &x, &y, &z); }
};
typedef Point Vector; double dot(Vector x, Vector y) { return x.x*y.x + x.y*y.y + x.z*y.z; }
double length(Vector x) { return sqrt(dot(x, x)); }
double dist2(Point A, Point B) { return dot(A - B, A - B); }
// Circle
struct Circle {
Point o;
double r;
Circle(Point O, double R): o(O), r(R) { }
}; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); double Eval(const vector<Point>& pt, Point o) {
double res = ;
for(auto g : pt) res = max(res, dist2(g, o));
return res;
}
uniform_real_distribution<double> rgen(0.0, 1.0);
double Rand(){ return rgen(rng); } Circle MinCircleAnneal(const vector<Point>& pt, double T, double dec, double ed) {
Point pcur, pbest, pnew; int sz = pt.size();
for(auto g : pt) pcur = pcur + g;
pbest = pcur = pcur / sz; double vcur = Eval(pt, pcur), vnew, vbest = vcur; while(T > ed) {
pnew = pcur + Point((Rand()*2.0-) * T, (Rand()*2.0-1.0) * T, (Rand()*2.0-) * T);
vnew = Eval(pt, pnew);
if(vnew <= vbest) vbest = vcur = vnew, pbest = pcur = pnew;
if(vnew <= vcur || Rand() < exp(-(vnew-vcur)/T))
vcur = vnew, pcur = pnew;
T *= dec;
} return Circle(pbest, sqrt(vbest));
} int n;
int main() {
scanf("%d", &n);
vector<Point> p(n);
for(int i = ; i < n; ++i) p[i].scan();
double ans = 1e13;
rep(i, ) {
Circle cir = MinCircleAnneal(p, 100000.0, 0.999, 3e-);
ans = min(ans, cir.r);
}
printf("%.10f\n", ans); return ;
}

2、三分套三分套三分

代码也很好理解,直接看代码吧

/*
三分求球的最小覆盖
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const ll mod=;
const double eps=1e-;
int n;
double x[],y[],z[];
double dis3(double a,double b,double c){
double ans=;
for(int i=;i<=n;i++){
ans=max(ans,(x[i]-a)*(x[i]-a)+(y[i]-b)*(y[i]-b)+(z[i]-c)*(z[i]-c));
}
return ans;
}
double dis2(double a,double b){
double l=-;
double r=;
double ans=;
while(r-l>=eps){
double rmid=(r+l)/;
double lmid=(l+rmid)/;
if(dis3(a,b,lmid)<dis3(a,b,rmid)){
r=rmid;
}
else l=lmid;
}
return dis3(a,b,l);
}
double dis(double a){
double l=-;
double r=;
while(r-l>=eps){
double rmid=(r+l)/;
double lmid=(l+rmid)/;
if(dis2(a,lmid)<dis2(a,rmid)){
r=rmid;
}
else l=lmid;
}
return dis2(a,l);
}
int main(){
// int n;
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
double l=-;
double r=;
while(r-l>=eps){
double rmid=(r+l)/;
double lmid=(l+rmid)/;
if(dis(lmid)<dis(rmid)){
r=rmid;
}
else l=lmid;
}
printf("%.8f\n",sqrt(dis(l)));
return ;
}

不管用上面哪种方法,都需要结合题目的精度要求,调节参数,达到精度和时间的平衡。

1. http://www.voidcn.com/article/p-ffokglab-uy.html

2. https://www.cnblogs.com/MekakuCityActor/p/10613934.html

3. https://www.cnblogs.com/heisenberg-/p/6827790.html

最小球覆盖——模拟退火&&三分套三分套三分的更多相关文章

  1. POJ 最小球覆盖 模拟退火

    最小球覆盖:用半径最小的球去覆盖所有点. 纯粹的退火算法,是搞不定的,精度不够,不然就会TLE,根本跑不出答案来. 任取一点为球心,然后一点点靠近最远点.其实这才是最主要的. 因为:4个点确定一个球, ...

  2. D.Country Meow 最小球覆盖 三分套三分套三分 && 模拟退火

    // 2019.10.3 // 练习题:2018 ICPC 南京现场赛 D Country Meow 题目大意 给定空间内 N 个点,求某个点到 N 个点的距离最大值的最小值.   思路 非常裸的最小 ...

  3. HDU5126---stars (CDQ套CDQ套 树状数组)

    题意:Q次操作,三维空间内 每个星星对应一个坐标,查询以(x1,y1,z1) (x2,y2,z2)为左下顶点 .右上顶点的立方体内的星星的个数. 注意Q的范围为50000,显然离散化之后用三维BIT会 ...

  4. Super Star(最小球覆盖)

    Super Star http://poj.org/problem?id=2069 Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

  5. POJ2069 最小球覆盖 几何法和退火法

    对这种问题不熟悉的读者 可以先去看一看最小圆覆盖的问题 ZOJ1450 现在我们来看最小球覆盖问题POJ2069 题目很裸,给30个点 求能覆盖所有点的最小球的半径. 先给出以下几个事实: 1.对于一 ...

  6. 2022-08-25-cdn套中套

    layout: post cid: 19 title: cdn套中套 slug: 19 date: 2022/08/25 20:32:00 updated: 2022/08/26 11:20:20 s ...

  7. POJ2069 最小球体覆盖, 模拟退火

    只是套了个模板,模拟退火具体的过程真心不懂阿 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #in ...

  8. hihocoder 1142 三分求极值【三分算法 模板应用】

    #1142 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一 ...

  9. Gym - 101981D The 2018 ICPC Asia Nanjing Regional Contest D.Country Meow 最小球覆盖

    题面 题意:给你100个三维空间里的点,让你求一个点,使得他到所有点距离最大的值最小,也就是让你找一个最小的球覆盖掉这n个点 题解:红书模板题,这题也因为数据小,精度也不高,所以也可以用随机算法,模拟 ...

随机推荐

  1. SQLServer---------使用Excel 往sqlServer数据库中导入数据

    1.右击创建好的表选择编辑200行 2.保证Excel的字段顺序与数据中顺序一致 3.选中好了后进行复制 4.打开文本   一个快捷方式 将excel 中的数据 黏贴放到文本中 5.点击sql    ...

  2. [转帖]【Ubuntu】Ubuntu 各版本代号简介

    [Ubuntu]Ubuntu 各版本代号简介 https://www.jianshu.com/p/7b351fde8799 一.版本及代号说明 Ubuntu中,每个版本都有一个更为特色的名字,这个名字 ...

  3. 如何用Python制作优美且功能强大的数据可视化图像

    第一个案例 首先开始来绘制你的第一个图表 from pyecharts import Bar '''遇到不懂的问题?Python学习交流群:1004391443满足你的需求,资料都已经上传群文件,可以 ...

  4. Python 3 + Selenium 3 实现汉堡王客户调查提交

    用Python 3 + Selenium 3实现汉堡王客户调查的自动填写,可以用来作为 python selenium的入门学习实现脚本,列举了几个比较不太好弄的知识点. 上代码: from sele ...

  5. mysql 5.7 修改root密码允许远程连接

    1.修改root密码(其他用户类似)  试过网上看的一些 在mysql数据库执行 update user set password='新密码'  where user='root' 执行说找不到字段, ...

  6. python基础知识(一)

    Python基础知识 计算基础知识 1.cpu 人类的大脑 运算和处理问题 2.内存 临时存储数据 断电就消失了 3.硬盘 永久存储数据 4.操作系统 调度硬件设备之间数据交互 python的应用和历 ...

  7. 禁用浏览器自动给input填充账号和密码

    如果input输入框type为text,设置autoComplete="off" <el-input v-model="ruleForm.loginId" ...

  8. swipe滑动操作

    1.swipe() 滑动用法 swipe(self, start_x, start_y, end_x, end_y, duration=None) :Args: - start_x - 开始滑动的x坐 ...

  9. mac php Swoole入门

    一. swoole 扩展安装 安装前必须保证系统已经安装了下列软件 php-7.0 或更高版本 gcc-4.8 或更高版本 make autoconf pcre (CentOS系统可以执行命令:yum ...

  10. kill 命令在Java应用中使用注意事项

    前言 我们都知道,kill在linux系统中是用于杀死进程. kill pid [..] kill命令可将指定的信号发送给相应的进程或工作. kill命令默认使用信号为15,用于结束进程或工作.如果进 ...