F. Two Points
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputThere 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
题意:给你两个点的位置与x轴方向的速度,y轴方向的速度,求两个点最近的距离;
思路:根据题意:得到两个相差x的距离的平方+y轴相差距离的平方得到一元二次方程;
利用公式得到最小值;
#include<bits/stdc++.h> using namespace std; #define ll __int64 #define mod 100000007 #define esp 0.00000000001 const int N=2e5+10,M=1e6+10,inf=1e9; int main() { double x,y,a,b,z,i,t; double vx,vy,va,vb; cin>>x>>y>>a>>b; cin>>vx>>vy>>va>>vb; double fa,fb,fc; fa=(vx-va)*(vx-va)+(vy-vb)*(vy-vb); fb=2.0*((vb-vy)*(b-y)+(va-vx)*(a-x)); fc=(a-x)*(a-x)+(b-y)*(b-y); double ans=(4*fa*fc-fb*fb)/(4*fa); double zuo=-fb/(2.0*fa); if(zuo>=0.0) printf("%.6f ",sqrt(ans)); else printf("%.6f ",sqrt(fc)); return 0; }