There are two points (x1, y1) and (x2, y2) on the plane. They move with the velocities (vx1, vy1) and (vx2, vy2). Find the minimal distance between them ever in future.
Input
The first line contains four space-separated integers x1, y1, x2, y2 ( - 104 ≤ x1, y1, x2, y2 ≤ 104) — the coordinates of the points.
The second line contains four space-separated integers vx1, vy1, vx2, vy2 ( - 104 ≤ vx1, vy1, vx2, vy2 ≤ 104) — the velocities of the points.
Output
Output a real number d — the minimal distance between the points. Absolute or relative error of the answer should be less than 10 - 6.
Examples
Input
1 1 2 2
0 0 -1 0
Output
1.000000000000000
Input
1 1 2 2
0 0 1 0
Output
1.414213562373095
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
double a, b, c, d;
double x, y, z, w;
double solve(double n) {
double ans;
double x1, x2, y1, y2;
x1 = a + c * n;
x2 = x + z * n;
y1 = b + d * n;
y2 = y + w * n;
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
scanf("%lf%lf%lf%lf", &a, &b, &x, &y);
scanf("%lf%lf%lf%lf", &c, &d, &z, &w);
double l = 0, r = 1e5, cnt = 500;
while (cnt--) {
double mid = (l + r) / 2;
double m = (mid + r) / 2;
if (solve(mid) < solve(m))
r = m;
else
l = mid;
}
printf("%.15f\n", solve(r));
}
该博客主要讨论如何计算在二维平面上具有给定速度的两个点在未来可能达到的最小距离。通过输入坐标和速度,程序使用牛顿迭代法求解,输出满足精度要求的最小距离值。

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



