The Moving Points

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 X i, Y i, VX i and VY i (-10 6 <= X i, Y i <= 10 6, -10 2 <= VX i , VY i <= 10 2), (X i, Y i) is the position of the i th point, and (VX i , VY i) is its speed with direction. That is to say, after 1 second, this point will move to (X i + VX i , Y i + VY i).

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个点,每个点按特定方向移动,题目要我们求出在t时刻的点之间最大距离的最小值(每个时刻点之间的距离不一样,n个点就有(n-1)!个线段,找出其中最大的记做(st i),t在变,每个时刻的最大值(st n)也在变,找出最小值即可)。

点之间的距离函数我们不难想到是个二次函数,且开口肯定是向上的,最低点就是我们要找的最小值。因此我们可以对时间进行三分操作(每个时刻点的距离是随时间变化的),找到最小值。

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#define  LL long long
#define  ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
using namespace std;
const int MAXN = 2005;
const int N = 15;
const double eps=1e-6;
double x[MAXN];
double y[MAXN];
double xv[MAXN];
double yv[MAXN];
int n;
double cal(double t)//这个函数是算在t时刻,所有点中距离最大的值
{
    double sum=0;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            double x1=x[i]+xv[i]*t;
            double y1=y[i]+yv[i]*t;
            double x2=x[j]+xv[j]*t;
            double y2=y[j]+yv[j]*t;
            double cot=sqrt(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
            sum=max(sum,cot);
        }
    }
    return sum;
}
double thr()//三分时间找出最小值
{
    double l=0,r=1e10;
    while(r-l>=eps){
    double mid=(l+r)/2;
    double midmid=(r+mid)/2;
    double cot1=cal(mid);
    double cot2=cal(midmid);
    if(cot1<cot2) r=midmid-eps;
    else l=mid+eps;
   }
   return l;
}
int main()
{

    int T;
    int cot=1;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);

        int i;
        for(i=0;i<n;i++) scanf("%lf%lf%lf%lf",&x[i],&y[i],&xv[i],&yv[i]);
        double h=thr();
        printf("Case #%d: %.2lf %.2lf\n",cot++,h,cal(h));
    }
    return 0;
}




猜你喜欢

转载自blog.csdn.net/qq_40620465/article/details/82934785