题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717点击打开链接
The Moving Points
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2214 Accepted Submission(s): 929
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 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).
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
移动时点的距离成抛物线 因此用三分
注意最后用%f输出
#include <bits/stdc++.h>
using namespace std;
#define eps 0.0000001
struct xjy
{
double x;
double y;
double vx;
double vy;
};
vector<xjy > point;
int n;
double dis(double t,int now1,int now2)
{
return sqrt(((point[now1].x+point[now1].vx*t)-(point[now2].x+point[now2].vx*t))*
((point[now1].x+point[now1].vx*t)-(point[now2].x+point[now2].vx*t))+
((point[now1].y+point[now1].vy*t)-(point[now2].y+point[now2].vy*t))*
((point[now1].y+point[now1].vy*t)-(point[now2].y+point[now2].vy*t)));
}
double cal(double t)
{
double ans=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
ans=max(ans,dis(t,i,j));
}
return ans;
}
double sf()
{
double l=0;
double r=1e8;
while((r-l)>=eps)
{
double mid1=(l*2+r)/3;
double mid2=(l+r*2)/3;
//cout << cal(mid2)-cal(mid1) <<endl;
if(cal(mid2)-cal(mid1)>eps)
r=mid2;
else
l=mid1;
}
return r;
}
int main()
{
int t;
int cas=0;
cin >>t;
while(t--)
{
point.clear();
cin >> n;
for(int i=0;i<n;i++)
{
xjy mid;
scanf("%lf%lf%lf%lf",&mid.x,&mid.y,&mid.vx,&mid.vy);
point.push_back(mid);
}
printf("Case #%d: ", ++cas);
if (n==1)
printf("0.00 0.00\n");
else
{
double time=sf();
printf("%.2f %.2f\n",time,cal(time));
}
}
}