UVA11796 Dog Distance 计算几何

本文详细阐述了如何通过计算几何原理解决两狗沿不同折线路径移动时,它们之间的最短和最长距离计算问题,具体步骤包括处理线段连接的折线路径、利用速度和位置信息来确定距离变化,并通过迭代处理每段路径来求解整个行程中的最大最小距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


计算几何:

题解转自:点击打开链接

首先我们想一下如果甲乙都仅沿着两条线段向前跑,那么他们之间的最短和最长距离怎么算? 假设甲的速度向量为v1(速度向量指甲单位时间所走的位移向量),乙的速度向量为v2. 那么可以把甲看成是静止不动的,而让乙一个人以v2-v1的速度向量去运动(画图验证下).

       且假设甲和乙都运动了t秒时间,那么我们可以知道上面的过程类似于甲不动,乙从自己的起点运动了t*(v2-v1)的位移向量. 而乙已知起点和位移向量,那么它就是在一条线段上. 最终我们要求的就是甲这个不动的点到乙这条线段的最大和最小距离即可.(仔细体会下)

       下面我们回到原问题. 原问题是一段一段的线段连接起来的折线,但是在某一段时刻,甲乙必然都是同时在走直线段的.

       我们只要把握住甲乙的当前点和下一个拐点这两个信息即可.

       假设当前甲在Pa点,乙在Pb点.而他们的下一个拐点是Sa和Sb,(由于总距离已知且他们花的时间相同,所以他们的速度vavb已知).

       所以我们可以知道到底甲先到拐点还是乙先到拐点,那么他们在到达拐点的这段时间内就都是走的直线段. 然后我们处理这段的最大最小值.接着更新他们的当前位置点和下一个拐点即可接着循环处理,一直到终点.



Time Limit: 2000MSMemory Limit: Unknown64bit IO Format: %lld & %llu

Submit Status

Description

Download as PDF

 

C

Dog Distance

Input

Standard Input

Output

Standard Output

Two dogs, Ranga and Banga, are running randomly following two different paths. They both run for T seconds with different speeds. Ranga runs with a constant speed of R m/s, whereas Banga runs with a constant speed of S m/s. Both the dogs start and stop at the same time. Let D(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 using N points, (P1 P2 P3 ... PN). The dog following this path will start from P1 and follow the line joining withP2, and then it will follow the line joining P2-P3, then P3-P4 and so on until it reaches Pn.

Input

Input starts with an integer I(I≤1000), the number of test cases.

Each test case starts with 2 positive integers A(2≤A≤50),B(2≤B≤50). The next line contains the coordinates of A points with the format XYXY2 ...XA YA(0≤Xi,Yi≤1000). These points indicate the path taken by Ranga. The next line contains B points 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 values of TR and S are 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 ::  Sohel Hafiz
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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值