C |
Dog distance |
Input |
Standard Input |
Output |
Standard output |
Two dogs, Ranga and Banga, are running randomly following two different paths. They both runTSeconds with different speeds. Ranga runs with a constant speedRM/s, whereas Banga runs with a constant speedSM/S. Both the dogs start and stop at the same time. LetD (t)Be the distance between the two dogs at time t.
The dog distance is equal to the difference between the maximum and the minimum distance between the two dogs in their whole journey.
Mathematically,
Dog distance={Max (D (A) 0 <= A <= t}-{min (D (B) 0 <= B <= t}
Given the paths of the two dogs, your job is to find the dog distance.
Each path will be represented usingNPoints ,(P1 P2 P3... PN). The dog following this path will start fromP1And follow the line joiningP2, And then it will follow the line joiningP2-P3, ThenP3-P4And so on until it reachesPn.
Input
Input starts with an integerI(I ≤1000), The number of test cases.
Each test case starts with 2 positive integersA(2 ≤A ≤50 ),B(2 ≤B ≤50). The next line contains the coordinatesAPoints with the formatX1 Y1 X2 Y2... XA ya,(0 ≤XI, Yi≤ 1000). These points indicate the path taken by Ranga. The next line containsBPoints in the same format. These points indicate the path taken by Banga. All distance units are given in meters and consecutive points are distinct. All the given coordinates are integers.
Note that the valuesT,RAndSAre unknown to us.
Output
For each case, output the case number first. Then output the dog distance rounded to the nearest integer. Look at the samples for exact format.
Sample Input |
Sample output |
2 2 2 0 0 10 0 0 1 10 1 3 2 635 187 241 269 308 254 117 663 760 413 |
Case 1: 0 Case 2: 404 |
Question:
The two dogs run along the broken line at a constant speed. If they are known to start at the same time and arrive at the same time, ask the difference between the maximum difference distance and the minimum difference distance.
Solution:
If both dogs take one line segment, one of them can be regarded as static based on relative motion theory, and the other is relatively moving, and the line is a line segment, the distance from a point to a line segment is immediately converted.
Solution code:
# Include <iostream> # include <cstdio> # include <cmath> # include <algorithm> using namespace STD; const double EPS = 1e-7; struct point {Double X, Y; point (double X0 = 0, double Y0 = 0) {x = x0, y = y0;} friend bool operator <(point a, point B) {if (. y! = B. y) return. Y <B. y; else return. x <B. X ;}}; typedef point vector; vector operator + (vector A, vector B) {return vector (. X + B. x,. Y + B. y);} vector operator-(vector A, vector 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; els E 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 length (vector A) {return SQRT (dot (A, A);} double angle (vector A, vector B) {return ACOs (dot (, b)/length (a)/length (B);} double cross (vector A, vector B) {return. x * B. y-A.y * B. x;} vector rotate (vector A, double rad) {return vector (. x * Cos (RAD)-. y * sin (RAD),. x * sin (RAD) +. y * Cos (RAD);} // rotate the rad radian double torad (double ANG) {return ANG/180.0 * ACOs (-1.0);} double distancetosegment (point P, point A, point B) {if (a = B) Return length (P-A); vector V1 = B-A, V2 = P-A, V3 = P-B; if (DCMP (dot (V1, V2) <0) return length (V2); else if (DCMP (dot (V1, V3)> 0) return length (V3); else return FABS (Cross (V1, V2)/length (V1);} const int maxn = 1100; point P [maxn], Q [maxn]; int Na, NB; double minans, maxans; void input () {minans = 1e9, maxans =-1e-9; scanf ("% d ", & Na, & nb); For (INT I = 0; I <Na; I ++) scanf ("% lf", & P [I]. x, & P [I]. y); For (INT I = 0; I <NB; I ++) scanf ("% lf", & Q [I]. x, & Q [I]. y);} void Update (point X, point A, point B) {minans = min (minans, distancetosegment (X, a, B); maxans = max (maxans, length (X-A); maxans = max (maxans, length (X-B);} void solve () {double Lena = 0, lenb = 0; for (INT I = 1; I <Na; I ++) Lena + = length (P [I]-P [I-1]); For (INT I = 1; I <NB; I ++) lenb + = length (Q [I]-Q [I-1]); int LA = 0, lB = 0; point Pa = P [0], Pb = Q [0]; while (La <NA-1 & Lb <nb-1) {double Disa = length (P [La + 1]-PA); double disb = length (Q [LB + 1]-PB); double T0 = min (DISA/Lena, disb/lenb); vector offa = (P [La + 1]-PA)/disa * t0 * Lena; vector offb = (Q [LB + 1]-PB) /disb * t0 * lenb; Update (Pa, Pb, Pb + offb-offa); Pa = pa + offa; Pb = Pb + offb; if (Pa = P [La + 1]) La ++; If (PB = Q [LB + 1]) LB ++;} printf ("%. 0lf \ n ", maxans-minans);} int main () {int t; scanf (" % d ", & T); For (INT I = 1; I <= T; I ++) {input (); printf ("case % d:", I); solve ();} return 0 ;}