HDU - 4717 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 description:

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 description:

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

SAMPLEOUTPUT:

Case #1: 1.00 0.00
Case #2: 1.00 1.00

Ideas:

The consciousness of the subject is the minimum value of the maximum distance between us two points. Use dichotomy to find the minimum value, write a function in advance to get the distance between two points, pay attention to ensure that the accuracy is small enough when dichotomy.

AC code:

#include <bits/stdc++.h>
using namespace std;
int n;
double a[1000],b[1000],c[1000],d[1000];
double distance(int m,int n,double t)
{
    
    
    double x1=a[m]+t*c[m],x2=a[n]+t*c[n];
    double y1=b[m]+t*d[m],y2=b[n]+t*d[n];
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

double check(double t)
{
    
    
    double ans=0.0;
    for(int i=1;i<=n;i++)
    {
    
    
        for(int j=i+1;j<=n;j++)
        {
    
    
            ans=max(ans,distance(i,j,t));
        }
    }
    return ans;
}

int main()
{
    
    
    int T;
    cin>>T;
    int cas=0;
    while(T--)
    {
    
    
        cin>>n;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        for(int i=1;i<=n;i++)
        {
    
    
            scanf("%lf%lf%lf%lf",&a[i],&b[i],&c[i],&d[i]);
        }
        double l=0.0,r=100000000000.0,time=0.0,path=10000000000000.0;
        while(l+1e-11<=r)
        {
    
    
            double middle1=(l+r)/2;
            double middle2=(middle1+r)/2;
            if(check(middle1)<=check(middle2))
                r=middle2;
            else l=middle1;
            if(path>check(l))
            {
    
    
                time=l;
                path=check(l);
            }
        }
        cout<<"Case #"<<++cas<<": "<<fixed<<setprecision(2)<<time<<' '<<path<<endl;
    }
    return 0;
}


Guess you like

Origin blog.csdn.net/m0_51727949/article/details/115187650