Uva11796 dog distance calculation ry

Source: Internet
Author: User
Tags x2 y2


Computational ry:

Question resolved from: Click to open the link

First, let's think about how to calculate the shortest distance and longest distance between them if both A and B are running only along two lines? Assume that the velocity vector of A is V1 (Speed vector the offset vector taken by the nail unit time), The velocity vector of B is V2. so we can regard a as static, and let B a person to the speed vector of v2-v1 to motion (Drawing Verification).

And assume that A and B are moving t seconds, then we can know that the above process is similar to a does not move, B from their own starting point of movement T * (v2-v1) of the displacement vector. B knows the starting point and displacement vector, so it is on a line segment. the final requirement is the maximum and minimum distance between A and B. (Try it out)

Next we will go back to the original problem. The original problem is the line connecting a line segment, but at some point in time, A and B must be at the same time.

We only need to grasp the current and next inflection points of Party A and Party B.

Assume that A is at PA and B is at Pb. Their next inflection point is SA and SB ,(Because the total distance is known and they spend the same time, their velocity VA and VB are known.).

So we can know whether a is at the inflection point first, or B is at the inflection point first, so they will all be straight lines during the time they reach the inflection point. then we process the maximum and minimum values of this section. update their current position and next inflection point and then process them cyclically until the end point.



Dog distance
Time limit:2000 ms Memory limit:Unknown 64bit Io format:% LLD & % LlU

Submit status

Description

 

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

 

 

 

Source


Root: prominent problemsetters: sohelhafiz
Root: aoapc I: Beginning algorithm contests -- Training Guide (rujia Liu): Chapter 4. Geometry: geometric computations in 2D: Examples

Submit status





#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const double eps=1e-8;int dcmp(double x){  if(fabs(x)<eps) return 0;  return (x<0)?-1:1;}struct Point{  double x,y;  Point(double _x=0,double _y=0):x(_x),y(_y){}};Point operator+(Point A,Point B){return Point(A.x+B.x,A.y+B.y);}Point operator-(Point A,Point B){return Point(A.x-B.x,A.y-B.y);}Point operator*(Point A,double p){return Point(A.x*p,A.y*p);}Point operator/(Point A,double p){return Point(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 Dot(Point A,Point B) {return A.x*B.x+A.y*B.y;}double Lenth(Point A) {return sqrt(Dot(A,A));}double Angle(Point A,Point B) {return acos(Dot(A,B)/Lenth(A)/Lenth(B));}double Angle(Point v) {return atan2(v.y,v.x);}double Cross(Point A,Point B){return A.x*B.y-A.y*B.x;}double DistanceToSegment(Point P,Point A,Point B){  if(A==B) return Lenth(P-A);  Point v1=B-A,v2=P-A,v3=P-B;  if(dcmp(Dot(v1,v2))<0) return Lenth(v2);  else if(dcmp(Dot(v1,v3))>0) return Lenth(v3);  else return fabs(Cross(v1,v2))/Lenth(v1);}int n,m;Point aa[55],bb[55];double LenA,LenB;double MX,MI;void update(Point P,Point A,Point B){  MI=min(MI,DistanceToSegment(P,A,B));  MX=max(MX,max(Lenth(P-A),Lenth(P-B)));}int main(){  int T_T,cas=1;  scanf("%d",&T_T);  while(T_T--)    {      LenA=0,LenB=0;      scanf("%d%d",&n,&m);      for(int i=0;i<n;i++)        {          scanf("%lf%lf",&aa[i].x,&aa[i].y);          if(i) LenA+=Lenth(aa[i]-aa[i-1]);        }      for(int i=0;i<m;i++)        {          scanf("%lf%lf",&bb[i].x,&bb[i].y);          if(i) LenB+=Lenth(bb[i]-bb[i-1]);        }      MI=1e9,MX=-1e9;      int sa=0,sb=0;      Point pa=aa[0],pb=bb[0];      while(sa<n-1&&sb<m-1)        {          double la=Lenth(aa[sa+1]-pa);          double lb=Lenth(bb[sb+1]-pb);          double t=min(la/LenA,lb/LenB);          Point va=(aa[sa+1]-pa)/la*t*LenA;          Point vb=(bb[sb+1]-pb)/lb*t*LenB;          update(pa,pb,pb+vb-va);          pa=pa+va; pb=pb+vb;          if(pa==aa[sa+1]) sa++;          if(pb==bb[sb+1]) sb++;        }      printf("Case %d: %.0lf\n",cas++,MX-MI);    }  return 0;}


Uva11796 dog distance calculation ry

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.