The Moving Points

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).
 

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
 

Sample Output
Case #1: 1.00 0.00 Case #2: 1.00 1.00
题意:给定n个点,以及每个点在某个方向的移动速度,求一个时刻是的n个点中任意两个点的距离最大的最小。
题解:直接对时间进行三分即可
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
struct lmx{
 double x;
 double y;
 double vx;
    double vy;
};
lmx lm[300];
double cnt(double x1,double y1,double x2,double y2)
{
 return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double maxi(double a,double b)
{
 return a>b?a:b;
}
int main()
{
     int test,i,ca=0,n,j;
  scanf("%d",&test);
  while(test--)
  {
   ca++;
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
            scanf("%lf %lf %lf %lf",&lm[i].x,&lm[i].y,&lm[i].vx,&lm[i].vy);
   }
   double l,r,mid,midmid,sx,sy,ex,ey;
   double mx,my,nx,ny;
   double ma=0,p1,p2;
   printf("Case #%d: ",ca);
                l=0,r=1e10;
    while(r-l>1e-6)
    {
     mid=(l+r)/2;
     midmid=(mid+r)/2;
     p1=0;p2=0;
     for(i=0;i<n-1;i++)
     {
             for(j=i+1;j<n;j++)
       {
      sx=lm[i].x+lm[i].vx*mid;
      sy=lm[i].y+lm[i].vy*mid;
      ex=lm[j].x+lm[j].vx*mid;
      ey=lm[j].y+lm[j].vy*mid;
      mx=lm[i].x+lm[i].vx*midmid;
      my=lm[i].y+lm[i].vy*midmid;
      nx=lm[j].x+lm[j].vx*midmid;
      ny=lm[j].y+lm[j].vy*midmid;
                        p1=maxi(p1,cnt(sx,sy,ex,ey));
      p2=maxi(p2,cnt(mx,my,nx,ny));
       }
     }
     if(p1>p2) l=mid;
     else r=midmid;
    }
    for(i=0;i<n-1;i++)
    {
     for(j=i+1;j<n;j++)
     {
      sx=lm[i].x+lm[i].vx*l;
      sy=lm[i].y+lm[i].vy*l;
      ex=lm[j].x+lm[j].vx*l;
      ey=lm[j].y+lm[j].vy*l;
      if(cnt(sx,sy,ex,ey)>ma) ma=cnt(sx,sy,ex,ey);
     }
    }
   printf("%.2lf %.2lf\n",l,ma);
  }
 return 0;
}
 
posted @ 2013-09-13 13:25  forevermemory  阅读(280)  评论(0编辑  收藏  举报