With the help of the Ares L, the Emperor of Law and some stars of the future, General M succeeded in setting foot on the mountain, and had set up a kingdom, known as ACMSYSU. In order to reward these founding heroes, General M decided to share her network.
General M has manufactured a router. One can use this network if the Euclidean distance between his/her house and the router is not greater than d. Because of General M's excellent technology, the distance between her house and the router can be infinite.
There are n heroes in the kingdom besides General M. General M is not an eccentric person, so she hopes everyone can use the network. Because the farther the router is away from her house, the worse the signal, General M wants the router being as close to her house as possible. Please help General M place the router so that the distance between her house and the router is minimized, and every one can use the router. You just need to tell General M the minimum distance.
Input
The input will consist of multiple test cases.
For each case, the first line contains 3 integers x, y and d, which represent that the General M's house locates at the point (x, y) and one can use the network if his/her distance to the router is not greater than d.
The second line is a positive integer n, the number of heroes of the kingdom.
Then n lines follow. Each line has 2 integers x_i and y_i, indicating that the i-th hero locates at point (x_i, y_i)。
All integers in the input satisfy that their absolute values are less than or equal to 10^5, and 1<=n<=10^3.
Output
For each case, output a real number which indicates the minimum distance between the General M's house and the router, to two decimal places.
If it is impossible to share a network which can cover everyone, output an 'X' (without the single quote).
Sample Input
0 0 5 2 5 3 5 -3 0 0 1 2 5 3 5 -3
Sample Output
1.00 X
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn=1e3+9;
const double inf=1e11,epx=0.00001;
int n;
double rx,ry,d;
double x[maxn],y[maxn];
double cal(double x1,double y1,double x2,double y2)
{
double a=(x1-x2)*(x1-x2);
double b=(y1-y2)*(y1-y2);
return sqrt(a+b);
}
bool chk(double xx,double yy)
{
for(int i=1;i<=n;i++)
if(cal(xx,yy,x[i],y[i])>d+epx) return false;
return true;
}
int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%lf %lf",&rx,&ry)!=EOF)
{
scanf("%lf",&d);
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf %lf",&x[i],&y[i]);
bool flag=false;
double ans=inf;
if(chk(rx,ry))
{
ans=0;
flag=true;
}
for(int i=1;i<=n;i++)
{
double tmp=cal(x[i],y[i],rx,ry);
double xx=x[i]+(rx-x[i])*(d/tmp);
double yy=y[i]+(ry-y[i])*(d/tmp);
if(ans>cal(xx,yy,rx,ry))
if(chk(xx,yy))
{
ans=cal(xx,yy,rx,ry);
flag=true;
}
}
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
if(cal(x[i],y[i],x[j],y[j])>2*d+epx) continue;
if(fabs(x[i]-x[j])<epx&&fabs(y[i]-y[j])<epx) continue;
double x1=x[j]-x[i];
double y1=y[j]-y[i];
double xx=x[i]+x1/2,yy=y[i]+y1/2;
double tmp=cal(xx,yy,x[i],y[i]);
double txt=sqrt(d*d-tmp*tmp);
double ex,ey;
if(fabs(x1-0)>epx) ey=1,ex=-y1/x1;
else ex=1,ey=-x1/y1;
double tt=sqrt(ex*ex+ey*ey);
xx+=txt*ex/tt;
yy+=txt*ey/tt;
if(cal(xx,yy,rx,ry)<ans)
if(chk(xx,yy))
{
ans=cal(xx,yy,rx,ry);
flag=true;
}
xx-=txt*ex*2/tt;
yy-=txt*ey*2/tt;
if(cal(xx,yy,rx,ry)<ans)
if(chk(xx,yy))
{
ans=cal(xx,yy,rx,ry);
flag=true;
}
}
if(!flag) printf("X\n");
else printf("%.2lf\n",ans);
}
return 0;
}