The Moving Points
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 964 Accepted Submission(s): 393
Problem Description
There are N points in total. Every point moves in certain direction and certain speed. We want to know at what time that the largest distance between any two points would be minimum. And also, we require you to calculate that minimum distance. We guarantee that no two points will move in exactly same speed and direction.
Input
The rst line has a number T (T <= 10) , indicating the number of test cases.
For each test case, first line has a single number N (N <= 300), which is the number of points.
For next N lines, each come with four integers Xi, Yi, VXi and VYi (-106 <= Xi, Yi <= 106, -102 <= VXi , VYi <= 102), (Xi, Yi) is the position of the ith point, and (VXi , VYi) is its speed with direction. That is to say, after 1 second, this point will move to (Xi + VXi , Yi + VYi).
For each test case, first line has a single number N (N <= 300), which is the number of points.
For next N lines, each come with four integers Xi, Yi, VXi and VYi (-106 <= Xi, Yi <= 106, -102 <= VXi , VYi <= 102), (Xi, Yi) is the position of the ith point, and (VXi , VYi) is its speed with direction. That is to say, after 1 second, this point will move to (Xi + VXi , Yi + VYi).
Output
For test case X, output "Case #X: " first, then output two numbers, rounded to 0.01, as the answer of time and distance.
Sample Input
2
2
0 0 1 0
2 0 -1 0
2
0 0 1 0
2 1 -1 0
2
0 0 1 0
2 0 -1 0
2
0 0 1 0
2 1 -1 0
Sample Output
Case #1: 1.00 0.00
Case #2: 1.00 1.00
Case #2: 1.00 1.00
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define eps 0.000001
typedef struct abcd
{
double x,y,vx,vy;
} point;
point p[];
int n;
double dis(double tt,int x,int y)
{
double xx=p[x].x+tt*p[x].vx;
double yy=p[x].y+tt*p[x].vy;
double xx1=p[y].x+tt*p[y].vx;
double yy1=p[y].y+tt*p[y].vy;
return sqrt((xx-xx1)*(xx-xx1)+(yy-yy1)*(yy-yy1));
}
double funa(double tt)
{
int i,j;
double maxa=;
for(i=;i<n;i++)
{
for(j=i+;j<n;j++)
{
maxa=max(maxa,dis(tt,i,j));
}
}
//cout<<tt<<endl;
return maxa;
}
double fun()
{
double l=,r=,m1,m2;
while(r-l>eps)
{
m1=l+(r-l)/;
m2=r-(r-l)/;
if(funa(m1)<funa(m2))
{
r=m2;
}
else l=m1;
}
return l;
}
int main()
{
int t,i,j,k;
scanf("%d",&t);
for(i=; i<=t; i++)
{
scanf("%d",&n);
for(j=; j<n; j++)
{
scanf("%lf%lf%lf%lf",&p[j].x,&p[j].y,&p[j].vx,&p[j].vy);
}
double yyy=fun();
printf("Case #%d: %.2lf %.2lf\n",i,yyy,funa(yyy));
}
}