Because a and B start at the same time, but their speed is not necessarily the same, we can set their speed to LA, lb (La, LB is, total route of B dog)
So how can a and B be the shortest distance between them when moving along a straight line?
Because the motion is relative, we can think of a as something we don't understand, and the movement direction of B relative to a is VB-va, therefore, we can see the relationship between A and B + (Vb-Va ).
As for the direction VA and how to find the VB vector, we can use the unit vector x moving time x moving speed to get it.
By the way, the bug of a template is fixed:
14128183 |
11796 |
Dog distance |
Accepted |
C ++ |
0.039 |
09:14:46 |
# Include <iostream> # include <cstdlib> # include <cstdio> # include <string> # include <cstring> # include <cmath> # include <vector> # include <queue> # include <stack> # include <algorithm> using namespace STD; const double EPS = 1e-10; # define maxd 50 + 10 # DEFINE _ pI ACOs (-1.0) struct point {Double X, Y; point (double x = 0, double Y = 0): x (x), y (y) {} bool operator <(const point & A) const {if (. x! = X) return x <. x; return Y <. Y ;}}; typedef point vector; vector operator + (vector A, vector B) {return vector (. X + B. x,. Y + B. y);} vector operator-(point a, point B) {return vector (. x-B.x,. y-B.y);} vector operator * (vector A, Double P) {return vector (. x * P,. y * P);} vector operator/(vector A, Double P) {return vector (. x/P,. y/P);} int DCMP (Double X) {If (FABS (x) <EPS) return 0; else Return x <0? -1: 1;} bool operator = (const point & A, const point & B) {return DCMP (. x-b.x) = 0 & DCMP (. y-b.y) = 0;} double dot (vector A, vector B) {return. x * B. X +. y * B. y;} double dist (point a, point B) {return SQRT (. x-B. x) * (. x-B. x) + (. y-B. y) * (. y-B. y);} double length (vector A) {return SQRT (dot (A, A);} double angle (vector A, vector B) {return ACOs (dot (A, B)/length (a)/length (B);} double cross (vector A, vector B) {return. x * B. y-. y * B. x;} double area2 (point a, point B, point C) {return cross (B-A, C-A);} vector rotate (vector A, double rad) {return vector (. x * Cos (RAD)-. y * sin (RAD),. x * sin (RAD) +. y * Cos (RAD);} double disttosegment (point P, point A, point B) {if (a = B) Return length (p-); vector V1 = B-a, V2 = P-A, V3 = p-B; If (DCMP (dot (V1, V2) <0) return length (V2 ); if (DCMP (dot (V1, V3)> 0) return length (V3); Return FABS (Cross (V1, V2)/length (V1 ));} point getintersection (point P, vector V, point Q, vector W) {vector u = P-Q; Double T = cross (W, u)/Cross (V, W ); return P + V * t;} bool segmentproperintersection (Point A1, point A2, point B1, point B2) {double C1 = cross (a2-a1, b1-a1), C2 = cross (a2-a1, b2-a1); double C3 = cross (b2-b1, a1-b1), C4 = cross (b2-b1, a2-b1); Return DCMP (C1) * DCMP (C2) <0 & DCMP (C3) * DCMP (C4) <0;} double polygonarea (point * P, int N) {double area = 0; For (INT I = 1; I <n-1; I ++) area + = cross (P [I]-P [0], p [I + 1]-P [0]); return area;} bool onsegment (point P, point A1, point A2) {return DCMP (a1-p, a2-p) = 0 & DCMP (dot (a1-p, a2-p) <0 ;} point pa [maxd], Pb [maxd]; int Na, NB; double la, Lb; double min, Max; void Init () {scanf ("% d ", & Na, & nb); La = 0; lB = 0; min = 1e20; max = 0; For (INT I = 0; I <Na; I ++) {scanf ("% lf", & pa [I]. x, & pa [I]. y); if (I> 0) {LA + = length (PA [I]-pa [I-1]); // La + = dist (PA [I], pa [I-1]) ;}}for (INT I = 0; I <NB; I ++) {scanf ("% lf ", & Pb [I]. x, & Pb [I]. y); if (I> 0) {LB + = length (Pb [I]-Pb [I-1]); // LB + = dist (Pb [I], pb [I-1]) ;}} void Update (point P, point A, point B) {min = min (Min, disttosegment (P, a, B )); max = max (max, length (p-A); max = max (max, length (p-B);} double solve () {// assume that the speeds of dog a and dog B are La and lb point pos_a = pa [0]. // the current position of dog a is point pos_ B = Pb [0]. // The current position of B Dog for (INT next_a = 0, next_ B = 0; next_a <Na-1 & next_ B <Nb-1 ;) {double TA = dist (PA [next_a + 1], pos_a); // The distance from A to the next inflection point is double TB = dist (Pb [next_ B + 1], pos_ B ); // The distance from B to the next inflection point is double T = min (Ta/La, TB/lb); // the shortest time is vector Va = (PA [next_a + 1]-pos_a) /TA * T * la; vector VB = (Pb [next_ B + 1]-pos_ B)/TB * T * Lb; Update (pos_a, pos_ B, pos_ B + VB-Va ); pos_a = pos_a + Va; pos_ B = pos_ B + VB; If (pos_a = pa [next_a + 1]) next_a ++; If (pos_ B = Pb [next_ B + 1]) next_ B ++;} // printf ("% lf \ n", Max, min); Return (max-min);} int main () {int t, case = 1; scanf ("% d", & T); While (t --) {Init (); double Dist = solve (); printf ("case % d: %. 0f \ n ", Case ++, DIST);} return 0 ;}
[Ultraviolet A] 11796-dog distance (relative motion)