天天看點

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.

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 

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

求的是所有點之間的最大距離最小,考慮對于任意兩個點來說,

它們之間的距離可以通過三分時間來确定最小值。

那麼所有點之間的最大值也會呈現抛物線的形狀。

#include<map>   
#include<set>  
#include<ctime>    
#include<cmath>   
#include<stack>
#include<queue>     
#include<string>    
#include<vector>    
#include<cstdio>        
#include<cstring>      
#include<iostream>    
#include<algorithm>        
#include<functional>    
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))        
#define rep(i,j,k) for(int i=j;i<=k;i++)        
#define per(i,j,k) for(int i=j;i>=k;i--)        
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])        
#define inone(x) scanf("%d",&x)        
#define intwo(x,y) scanf("%d%d",&x,&y)        
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)      
#define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)     
#define lson x<<1,l,mid    
#define rson x<<1|1,mid+1,r    
#define mp(i,j) make_pair(i,j)    
#define ff first    
#define ss second    
typedef long long LL;
typedef pair<int, int> pii;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const double eps = 1e-8;
int T, n, cas = 1;
int x[N], y[N], vx[N], vy[N];

double get(int i, double a, int j)
{
  double xi = x[i] + a*vx[i], yi = y[i] + a*vy[i];
  double xj = x[j] + a*vx[j], yj = y[j] + a*vy[j];
  return sqrt((xi - xj)*(xi - xj) + (yi - yj)*(yi - yj));
}

int main()
{
  for (inone(T); T--; cas++)
  {
    inone(n);
    rep(i, 1, n) infou(x[i], y[i], vx[i], vy[i]);
    double l = 0, r = 1e6, d = INF;
    while (r - l > eps)
    {
      double a = (l + l + r) / 3, b = (l + r + r) / 3;
      double da = 0, db = 0;
      rep(i, 1, n) rep(j, i + 1, n)
      {
        double d1 = get(i, a, j), d2 = get(i, b, j);
        da = max(da, d1); db = max(db, d2);
      }
      if (da > db) l = a; else r = b;
      d = min(da, db);
    }
    printf("Case #%d: %.2lf %.2lf\n",cas, r, d);
  }
  return 0;
}