UVA11796 Dog Distance 计算几何

计算几何:

题解转自:点击打开链接

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

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

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

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

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

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


Dog Distance

Time Limit: 2000MS 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 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;
}
时间: 2024-08-05 02:59:02

UVA11796 Dog Distance 计算几何的相关文章

uva 11796 Dog Distance (计算几何-点和直线)

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

相对运动模拟:UVa 11796 Dog Distance

11796 - Dog Distance 题目大意 甲和乙两条狗分别沿着给定的折线匀速奔跑,同时出发.同时到达,试计算甲和乙在奔跑过程中的最远距离和最近距离的差. 解题思路 模拟整个过程,把总过程分割成一个个子过程:甲和乙的路线都是一条线段,因为运动是相对的,因此也可以认为甲静止不动,乙自己沿着某个相对运动的直线(用相对位移向量表示)走,因此问题转化为点到线段的最小最大距离,注意子过程的最小最大值不代表整个过程的最小最大值,所以每处理完一个子过程就要不断更新. 假设现在甲的位置是Pa,刚经过编号

【UVA】11796 - Dog Distance(相对运动)

因为a,b是同时出发,同时达到,但是他们的速度不一定一样,所以我们可以设他们的速度为La,Lb(La,Lb为a,b狗的总路程) 那么,如何a,b都是沿着直线运动的时候如何求他们之间的最短最长距离呢? 因为运动都是相对的,所以我们可以把a看成不懂的,而b相对于a的移动方向就是Vb - Va,因此就可以看成a和线段 b + (Vb -Va)之间的关系了 至于方向Va,Vb向量怎么求,我们可以利用单位向量 X 移动时间 X 移动速度 得到. 经典的一道题,顺便还修复了一个模板的BUG: 1412818

UVA - 11796 - Dog Distance (计算几何~)

不得不感叹,计算几何真是太美丽了!! AC代码: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> using namespace std; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x) , y(y) { } }; t

Dog Distance(UVA11796)

[分析] 先看一种简单的情况:甲和乙的路线都是一条线段.因为运动是相对的,因此也可以认为甲静止不动,乙沿着自己的直线走,因此问题转化为求点到线段的最小距离和最大距离: 有了简化版的分析,只需要模拟整个过程,设现在甲的位置在Pa,刚刚经过编号为Sa的拐点:乙的位置是Pb,刚刚经过编号为Sb的拐点,则我们只需要计算他们俩谁先到拐点,那么在这个时间点之前的问题就是我们刚刚讨论过的"简化版".求解完毕之后,需要更新甲乙的位置,如果正好到达下一个拐点,还需要更新Sa和/或Sb,然后继续模拟.因为

●UVA 11796 Dog Distance

题链: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2896 题解: 计算几何,骚操作 对于简单情况,即两只狗的路线均为一条线段, 可以从相对运动的角度出发,考虑一直狗不动,另一只狗在运动. 而由于两只狗此时都在做匀速直线运动,所以在那只不懂的狗看来,另一直狗也在匀速直线前行.(物理老师:速度的矢量和!) 所以这个简单情况下,问题就变为

UVa 11796 Dog Distance

题意: 有甲乙两条狗分别沿着一条折线奔跑,已知它们同时从各自的起点出发,同时到达各自的终点.求整个过程中两条狗的最大距离Max与最小距离Min的差值. 分析: 假设甲乙的路线都是一条线段的简单情况.运动是相对的,我们假定甲不动,乙相对甲的运动也是匀速直线运动.所以就将问题转化成了点到直线的最小和最大距离. 甲或乙每到达一个拐点所对应的时刻称作“关键点”,那么没两个关键点之间的运动都可看做上面分析的简单情况.我们只要及时更新甲乙的位置即可. LenA和LenB分别是两条路线的总长,因为运动时间相同

简单几何(相对运动距离最值) UVA 11796 Dog Distance

题目传送门 题意:两只狗在折线上跑,速度未知,同时出发,同时达到.问跑的过程中,两狗的最大距离和最小距离的差 分析:训练指南P261,考虑相对运动,设A静止不动,B相对A运动,相对的运动向量:Vb - Va(可以理解为速度矢量),那么就是pa到线段pb-pb+Vb-Va的距离最值 /************************************************ * Author :Running_Time * Created Time :2015/10/22 星期四 10:21

UVA 11796- Dog Distance(计算几何_求最大距离和最小距离之差)

题意:甲乙两条狗分别沿着一条折线奔跑,两只狗的速度未知,但已知他们同时出发,同时到达,并且都是匀速奔跑,试求甲和乙在奔跑过程中最远距离和最近距离之差. 思路:因为运动是相对的,因此也可以认为甲静止不动,乙自己沿着直线走,因此问题转化为求点到线段的最小或最大距离.然后模拟求解.大白P262 #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include