Simple geometry (relative motion distance max) UVA 11796 Dog Distance

Source: Internet
Author: User
Tags cas

Topic Portal

Test instructions: Two dogs running on a polyline, the speed is unknown, at the same time to go, at the same time. The difference between the maximum distance and the minimum distance between two dogs during a run

Analysis: Training Guide P261, consider the relative motion, set a stationary, B relative a motion, the relative motion vector: vb-va (can be understood as the speed vector), then is the PA to the line segment Pb-pb+vb-va distance maximum

/************************************************* author:running_time* Created time:2015/10/22 Thursday 10:21:19* Fi Le Name:UVA_11796.cpp ************************************************/#include <cstdio> #include < algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath># Include <string> #include <vector> #include <queue> #include <deque> #include <stack># Include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long Ll;const int N = + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const Double EPS = 1e-10;st    Ruct point {double x, y; Point (Double x=0, double y=0): X (x), Y (y) {}};typedef point vector;double dot (vector A, vector b) {return a.x * b . x + a.y * B.Y;} Double Cross (Vector A, VecTor B) {return a.x * b.y-a.y * b.x;}    int dcmp (double x) {if (Fabs (x) < EPS) return 0; else return x < 0? -1:1;} Vector operator + (vector A, vector B) {return vector (a.x + b.x, A.Y + b.y);} Vector operator-(vector A, vector B) {return vector (a.x-b.x, a.y-b.y);} Vector operator * (vector A, double p) {return vector (a.x * p, A.Y * p);} Vector operator/(vector A, double p) {return vector (a.x/p, a.y/p);} BOOL operator < (const point &a, const point &b) {return a.x < b.x | | (a.x = = b.x && a.y < B.Y);} BOOL operator = = (Const point &a, const point &b) {return dcmp (a.x-b.x) = = 0 && dcmp (a.y-b.y) = = 0;} Double Length (Vector a) {return sqrt (dot (A, a));}    Double dis_to_seg (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);}    Point A[n], b[n];d ouble mx, mn;void updata (point P, point A, point B) {mn = min (mn, dis_to_seg (P, A, B)); mx = max (mx, max (Length (p-a), Length (p-b)));}    int main (void) {int T, cas = 0; scanf ("%d", &t);   while (t--) {int na, NB;        scanf ("%d%d", &na, &AMP;NB);        for (int i=0; i<na; ++i) scanf ("%lf%lf", &a[i].x, &AMP;A[I].Y);        for (int i=0; i<nb; ++i) scanf ("%lf%lf", &b[i].x, &AMP;B[I].Y);        Double Lena = 0, LenB = 0;        for (int i=0; i<na-1; ++i) Lena + = Length (a[i+1]-a[i]);        for (int i=0; i<nb-1; ++i) LenB + = Length (b[i+1]-b[i]);        int sa = 0, sb = 0;   mx = -1e9;        MN = 1e9;        Point pa = a[0], PB = B[0];            while (SA < na-1 && SB < nb-1) {double la = length (a[sa+1]-PA);            Double lb = length (b[sb+1]-Pb); Double tim = min (LA/Lena, LB/LENB);            Vector Va = (a[sa+1]-PA)/La * Tim * LENA;            Vector Vb = (b[sb+1]-Pb)/lb * Tim * LENB;            Updata (PA, Pb, Pb + Vb-va);            PA = pa + Va;            PB = Pb + Vb;            if (PA = = a[sa+1]) sa++;        if (Pb = = b[sb+1]) sb++;    } printf ("Case%d:%.0f\n", ++cas, MX-MN); } return 0;}

  

Simple geometry (relative motion distance max) UVA 11796 Dog Distance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.