Problem H : The Moving Points
From: HDU, 4717
ime Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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 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
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
Source
2013 ACM/ICPC Asia Regional Online —— Warmup2
Recommend
zhuyuanchen520
题目大意:
给几个点,他们会按照他们自己的速度移动,看他们在每一时刻两两间距离最大是多少,求不同时刻这个最大距离的最小值
解题思路:
利用三分法,先设定两个缎端点,一个为0,一个取一个比较大的数比如9999,之后求每次时间为1/3处和2/3处的两点间最大距离 看哪个小,如果1/3处的小就让右端点编导原2/3处,否则2/3处小就让左端点变到原1/3处,不断缩小范围最终两个端点几乎成为一个点,那个点就是最小值点。
AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int max_num = 301;
const double EPS=1e-9;
struct point{
double x,y;
double vx,vy;
}p[max_num],temp[max_num];
double distance(point a,point b)
{
return (sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
}
double s_dis(double m,int num)
{
for(int i=0;i<num;i++)
{
temp[i].x = p[i].x + p[i].vx * m;
temp[i].y = p[i].y + p[i].vy * m;
}
double max_dis = -1000000;
double x;
for(int i=0;i<num;i++)
{
for(int j=i+1;j<num;j++)
{
x = distance(temp[i],temp[j]);
if (x > max_dis)
max_dis = x;
}
}
return max_dis;
}
int main()
{
int n;
cin>>n;
int number = 0;
while(n--)
{
int t;
cin>>t;
if(t==1) //特殊的情况
{
printf("Case #%d: 0.00 0.00\n",++number);
continue;
}
for(int i=0;i<t;i++)
{
cin>>p[i].x>>p[i].y>>p[i].vx>>p[i].vy;
}
double l = 0.0,r = 9999.9;
while(r-l>EPS) //三分法
{
double m1=l+(r-l)/3; //三分靠左的点
double m2=r-(r-l)/3; //三分靠右的点
if(s_dis(m1,t) < s_dis(m2,t))
r = m2; //左边点的值小 就将右端点移到右边的点处
else
l = m1; //右边点的值小 就将左端点移到左边的点处
}
printf("Case #%d: %.2lf %.2lf\n",++number,l,s_dis(l,t));
}
}
参考:https://blog.csdn.net/honeyaya/article/details/11573821