圆覆盖的问题,也可以用模拟退火做
题意:求最小圆覆盖
https://blog.csdn.net/wu_tongtong/article/details/79362339 写得不错,有个别地方有手误
这两题一样的代码交 0ms
#include<cstdio>
#include<vector>
#include<cmath>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=505;
const int MOD=1e9+7;
const double eps=1e-10;
template <class T>
bool sf(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
int sign(double x) { //三态函数,减少精度问题
return abs(x) < 1e-10 ? 0 : x < 0 ? -1 : 1;
}
struct Point { //点的定义
double x, y;
Point(double x=0.0, double y=0.0) : x(x), y(y) {}
Point operator + (const Point &rhs) const { //向量加法
return Point(x + rhs.x, y + rhs.y);
}
Point operator - (const Point &rhs) const { //向量减法
return Point(x - rhs.x, y - rhs.y);
}
Point operator * (double p) const { //向量乘以标量
return Point(x * p, y * p);
}
Point operator / (double p) const { //向量除以标量
return Point(x / p, y / p);
}
bool operator < (const Point &rhs) const { //点的坐标排序
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
bool operator == (const Point &rhs) const { //判断同一个点
return sign(x - rhs.x) == 0 && sign(y - rhs.y) == 0;
}
};
typedef Point Vector;
double polar_angle(Vector A) { //向量极角
return atan2(A.y, A.x);
}
double cross(Vector A, Vector B) { //向量叉积,两向量组成的三角形的有向面积的两倍,可判断两向量的方向
return A.x * B.y - A.y * B.x;
}
Point line_line_inter(Point p, Vector V, Point q, Vector W) { //两直线交点,参数方程
Vector U = p - q;
double t = cross(W, U) / cross(V, W); //两直线有唯一交点的前提:cross(V, W)非0
return p + V * t;
}
double dot(Vector A,Vector B){
return A.x*B.x+A.y*B.y;
}
double length(Vector A){
return sqrt(dot(A,A));
}
Vector rotate(Vector A, double rad) {
return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}
Point p[N];
int n;
Point get_c(Point a,Point b,Point c){
Vector u=rotate(b-a,PI/2);
Vector v=rotate(c-b,PI/2);
if(sign(cross(a-c,a-b))==0){
double ac=length(a-c);
double bc=length(b-c);
double ab=length(a-b);
if(sign(ac+ab-bc)==0) return (b+c)/2;
if(sign(ac+bc-ab)==0) return (a+b)/2;
if(sign(ab+bc-ac)==0) return (a+c)/2;
}
return line_line_inter((a+b)/2,u,(b+c)/2,v);
}
void Min_Cover_Circle(Point &c,double &r){
random_shuffle(p,p+n);
c=p[0],r=0.0;
for(int i=1;i<n;i++){
if(sign(length(p[i]-c)-r)>0){
c=p[i],r=0.0;
for(int j=0;j<i;++j){
if(sign(length(p[j]-c)-r)>0){
c=(p[i]+p[j])/2.0;
r=length(c-p[i]);
for(int k=0;k<j;++k){
if(sign(length(p[k]-c)-r)>0){
c=get_c(p[i],p[j],p[k]);
r=length(c-p[i]);
}
}
}
}
}
}
}
int main(void){
while(scanf("%d",&n)==1){
if(!n) break;
for(int i=0;i<n;i++){
double x,y;
scanf("%lf%lf",&x,&y);
p[i]={x,y};
}
Point c;
double r;
Min_Cover_Circle(c,r);
printf("%.2f %.2f %.2f\n",c.x,c.y,r);
}
return 0;
}