HDU - 4717 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.

输入说明:

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

思路:

题目的意识是让我们两点之间最大距离的最小值。用二分去找那个最小值,提前写一个函数用来得到两个点之间的距离,注意二分的时候要保证精度足够小。

AC代码:

#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;
}


猜你喜欢

转载自blog.csdn.net/m0_51727949/article/details/115187650