题意:给你一个点A和一个距离d 再给你n个点 问你是否存在一个点 在满足到所有n个点距离小于等于d前提下 到A的距离尽量短
思路:刚开始我的思路是一n个点为圆心 以d为半径 二分答案 再判断所有圆是否相交 算了下模板的复杂度是O(n*n*logn) 感觉可以 于是交了一发 T了 然后就没招了。。查看题解 思路是首先判断n个圆是否相交 如果相交则一定存在一个最短距离 最短距离是多少呢 分两种情况 第一种是到某个交点的距离 第二种是点A与所有圆圆心连线的交点上 然后就是精度问题了 我用的LRJ的模板 1e-8各种WA 改成1e-5就过了 这题过得还算顺利 只交了28遍就过了~~
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;
const double eps = 1e-5;
const double INF = 1e20;
int dcmp(double x){
if(fabs(x) < eps) return 0;
return x > 0 ? 1 : -1;
}
struct Point{
double x, y;
Point(double x = 0, double y = 0) : x(x) , y(y) {}
bool operator == (const Point &a)const{
return dcmp(a.x-x) == 0 && dcmp(a.y-y) == 0;
}
Point operator - (const Point &a)const{
return Point(x-a.x, y-a.y);
}
Point operator + (const Point &a)const{
return Point(x+a.x, y+a.y);
}
Point operator *(const double &a) const{
return Point(x*a, y*a);
}
};
double Dot(Point a,Point b){
return a.x * b.x + a.y * b.y;
}
double dis2(Point a, Point b){
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}
double length(Point a){
return sqrt(Dot(a,a));
}
struct Circle{
Point c;
double r;
Circle(Point c, double r) : c(c), r(r) {}
Circle() {}
Point point(const double &a)const{
return Point(c.x + cos(a)*r, c.y+sin(a)*r);
}
};
typedef Point Vector;
struct Line{
Point p;
Vector v;
double ang;
Line() {}
Line(Point p, Vector v) : p(p), v(v) { ang = atan2(v.y, v.x); }
bool operator < (const Line &L) const {
return ang < L.ang;
}
Point point(double t){
return p + v*t;
}
};
double angle(const Point &v){
return atan2(v.y, v.x);
}
void getCircleCirclelntersection(Circle c1, Circle c2, vector<Point> &sol){
double d = length(c1.c-c2.c);
if(dcmp(d) == 0) return;
if(dcmp(c1.r + c2.r - d) < 0) return;
if(dcmp(fabs(c1.r-c2.r) - d) > 0) return;
double a = angle(c2.c - c1.c);
double da = acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));
Point p1 = c1.point(a-da), p2 = c1.point(a+da);
sol.push_back(p1);
if(p1 == p2) return;
sol.push_back(p2);
}
void getLineCircleIntersection(Line l, Circle C, double &t1, double &t2, vector<Point> &sol){
double a = l.v.x, b = l.p.x - C.c.x, c = l.v.y, d = l.p.y-C.c.y;
double e = a*a + c*c, f = 2*(a*b+c*d), g = b*b+d*d - C.r*C.r;
double delta = f*f-4*e*g;
if(dcmp(delta) < 0)return;
if(dcmp(delta) == 0){
t1 = t2 = -f/(2*e);
sol.push_back(l.point(t1));
return;
}
t1 = (-f-sqrt(delta))/(2*e);
sol.push_back(l.point(t1));
t2 = (-f+sqrt(delta))/(2*e);
sol.push_back(l.point(t2));
}
Point boss;
double d;
vector<Point> work;
vector<Point> ans;
vector<Circle> cir;
bool in(Point p){
for(int i = 0; i < cir.size(); i++){
if(dcmp(dis2(cir[i].c, p) - d * d) > 0){
return false;
}
}
return true;
}
bool in_all_circle(Point a){
for(int i = 0; i < cir.size(); i++)
if(dcmp(dis2(a, cir[i].c) - cir[i].r * cir[i].r) > 0) return false;
return true;
}
void solve(){
int n;
scanf("%d", &n);
cir.clear();
work.clear();
ans.clear();
for(int i = 0; i < n; i++){
double x, y;
scanf("%lf%lf", &x, &y);
cir.push_back(Circle(Point(x, y), d));
}
Point a, b;
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++){
getCircleCirclelntersection(cir[i], cir[j], work);
}
for(int i = 0; i < n; i++){
double s, b;
getLineCircleIntersection(Line(boss, boss-cir[i].c), cir[i], s, b, work);
}
for(int i = 0; i < work.size(); i++)
if(in(work[i]))
ans.push_back(work[i]);
if(ans.size() == 0 ){
printf("X\n");
return;
}
double res = INF;
if(in_all_circle(boss)){
printf("0.00\n");
return;
}
for(int i = 0; i < ans.size(); i++){
res = min(res, length(ans[i]- boss));
}
printf("%.2lf\n", res);
}
int main()
{
// freopen("in.txt", "r", stdin);
while(~scanf("%lf%lf%lf", &boss.x, &boss.y, &d)) solve();
return 0;
}
/*
0 0 5
2
5 3
5 -3
0 0 1
2
5 3
5 -3
0 100 1
3
1 0
0 -1
-1 0
-100001 -100001 1973
2
100031 100101
100067 100022
0 0 1
2
3 0
3 0
0 0 1
3
3 0
3 0
4 0
0 0 100
1
1 1
-100000 -100000 100000
10
0 0
9999 866
345 56756
543 333
-98 876
6789 89
9876 -9
1 1234
45643 775
9 5236
*/