zznuli--1909

Description

小火山和他的朋友岩漿, 分別打算去買商店買一些東西。小火山的起始位置爲(X1, Y1),終止位置爲(X2, Y2), 速度爲U;
岩漿的起始位置爲(X3, Y3), 終止位置爲(X4, Y4), 速度爲V, 雙方一塊出發, 到達終止位置後, 都會在終止位置停留。 現在小火山想知道他和岩漿在此過程中的最小距離。

Input

輸入第一行是一個整數T(T <= 100), 表示一共有T組數據。
對於每組數據都有十個數字X1, Y1,X2, Y2, U, X3, Y3, X4, Y4, V。 分別代表小火山和岩漿的起始位置, 終止位置和速度。
(|X1, Y1,X2, Y2, X3, Y3, X4, Y4| <= 1000000, 1 <= U, V <= 1000000)

Output

對於每組數據輸出一個數字, 表示此過程中的最小距離, 最後結果保留四位小數。

Sample Input

2
1 0 2 2 5 0 0 -1 -1 4
0 0 2 0 3 1 1 2 1 2

Sample Output

1.0000
1.0000
對於兩個人,有起點座標,速度的向量, 那麼每個人每個時刻的位置是可以根據時間算出來的。


兩人的運動分爲兩段時間(0,t1), (t1, t2).

就是分兩種情況考慮

其中l表示的是該人所行走的路程,也就是起點和終點的距離

t表示行走所需要的時間,也就是爲了考慮情況

vx, vy分別表示在x, y分量座標的相對速度

c表示兩點相對的移動

ans分別進行保存


#include<stdio.h>

#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 5010;
#define INF 0x3f3f3f3f;


double dis(double x1, double y1, double x2, double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
    int T;
    double x1, y1, x2, y2, v1, x3, y3, x4, y4, v2;
    double l1, l2;
    double t1, t2, t;
    double vx1, vy1, vx2, vy2;
    double c1, c2, c3;
    double ans1, ans2, ans;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&v1,&x3,&y3,&x4,&y4,&v2);
        l1=dis(x1,y1,x2,y2);
        l2=dis(x3,y3,x4,y4);
        t1=l1/v1;
        t2=l2/v2;
        vx1=v1*(x2-x1)/l1;//就是表示在X軸上移動的距離
        vy1=v1*(y2-y1)/l1;//就是表示在y軸上移動的距離
        vx2=v2*(x4-x3)/l2;
        vy2=v2*(y4-y3)/l2;
        t=min(t1, t2);
        c1=pow(vx1-vx2, 2)+pow(vy1-vy2, 2);
        c2=2*((x1-x3)*(vx1-vx2)+(y1-y3)*(vy1-vy2));
        c3=pow(x1-x3, 2)+pow(y1-y3, 2);
        if(c2>=0)
            ans1=c3;
        else
        {
            double nice = -c2/(2*c1);
            if(nice<=t)
                ans1=c1*nice*nice+c2*nice+c3;
            else
                ans1=c1*t*t+c2*t+c3;
        }
        if(t1==t2)
            ans2=ans1;
        else if(t1<t2)
        {
            t=t2-t1;
            x3=x3+vx2*t1;
            y3=y3+vy2*t1;
            c1=vx2*vx2+vy2*vy2;
            c2=2*(vx2*(x3-x2)+vy2*(y3-y2));
            c3=pow(x3-x2, 2)+pow(y3-y2, 2);
            if(c2>=0)
                ans2=c3;
            else
            {
                double nice=-c2/(2*c1);
                if(nice<=t)
                    ans2=c1*nice*nice+c2*nice+c3;
                else
                    ans2=c1*t*t+c2*t+c3;
            }
        }
        else
        {
            t=t1-t2;
            x1=x1+vx1*t2;
            y1=y1+vy1*t2;
            c1=vx1*vx1+vy1*vy1;
            c2=2*(vx1*(x1-x4)+vy1*(y1-y4));
            c3=pow(x1-x4, 2)+pow(y1-y4, 2);
            if(c2>=0)
                ans2=c3;
            else
            {
                double nice=-c2/(2*c1);
                if(nice<=t)
                    ans2=c1*nice*nice+c2*nice+c3;
                else
                    ans2=c1*t*t+c2*t+c3;
            }
        }
        ans=sqrt(min(ans1, ans2));
        printf("%.4lf\n", ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章