链接:戳这里
F. Two Points
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll a1,a2,b1,b2,c1,c2,d1,d2;
const long double eps=1e-8;
int dcmp(long double x)
{
if(fabs(x)<=eps) return 0;
return x<0?-1:1;
}
int main()
{
cin>>a1>>a2>>b1>>b2;
cin>>c1>>c2>>d1>>d2;
ll e1=b1-a1;
ll e2=d1-c1;
ll f1=b2-a2;
ll f2=d2-c2;
ll A=e2*e2+f2*f2;
ll B=2*e1*e2+2*f1*f2;
ll C=e1*e1+f1*f1;
double l=0.0,r=1e8*1.0,mid;
while(r-l>1e-10){
double m1=l+(r-l)/3;
double m2=r-(r-l)/3;
double calc1=A*m1*m1+B*m1+C;
double calc2=A*m2*m2+B*m2+C;
if(calc1<calc2) r=m2;
else l=m1;
}
double ans=A*l*l+B*l+C;
if(ans>=0.00) printf("%.12f\n",sqrt(ans));
else printf("%.12f\n",sqrt(C*1.0));
return 0;
}
/*
0 0 0 3
0 1 0 -2
*/