The Moving Points
HDU - 4717
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.
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).
2 2 0 0 1 0 2 0 -1 0 2 0 0 1 0 2 1 -1 0
Case #1: 1.00 0.00 Case #2: 1.00 1.00
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-10;
const int M = 310;
int n;
struct node{
double x,y,vx,vy;
}p[M];
double dis(double x1,double y1,double x2,double y2){//计算两个点之间的距离的函数
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double cal(double t){//寻找经过t时间后,两点之间的最大距离
double ans = 0;
int i,j;
for(i = 1; i <= n; i++){
for(j = i+1; j <= n; j++){
double num = dis(p[i].x+t*p[i].vx, p[i].y+t*p[i].vy, p[j].x+t*p[j].vx, p[j].y+t*p[j].vy);
ans = max(num,ans);
}
}
return ans;
}
double ternarySearch(double l,double r){//三分查找
while(l+eps<=r){
double mid,midmid;
mid = (l+r)/2;
midmid = (mid+r)/2;
if(cal(mid)+eps<cal(midmid))
r = midmid;
else
l = mid;
}
return l;
}
int main(){
int t,cas = 0;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int i;
for(i = 1; i <= n; i++){
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].vx,&p[i].vy);
}
double ans = ternarySearch(0,1e8);
printf("Case #%d: ",++cas);
if(n == 1)printf("0.00 0.00\n");
else printf("%.2f %.2f\n",ans,cal(ans));
}
return 0;
}
311

被折叠的 条评论
为什么被折叠?



