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 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 with P2, 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

题目大意:

两条狗匀速分别沿着折线跑,已知同时出发,同时到达,问你求相差最大的距离 与相差的最小的距离之间的差值。

解题思路:

如果两只狗都走1条线段的话,根据相对运动的理论,可以把其中一只狗看成静止不动,另一只狗相对运动,且线路为线段,那么立刻转化为点到线段的距离的问题。

解题代码:

#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(a.y!=b.y) return a.y<b.y;
        else return a.x<b.x;
    }
};

typedef Point Vector;

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); }
int dcmp(double x) { if(fabs(x)<eps) return 0; else return x<0?-1:1; }
bool operator == (const Point &a,const Point &b) { return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0 ; }

double Dot(Vector A,Vector B){ return A.x*B.x+A.y*B.y; }
double Length(Vector A){ return sqrt(Dot(A,A)); }
double Angle(Vector A,Vector B){ return acos(Dot(A,B)/Length(A)/Length(B)); }
double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x; }
Vector Rotate(Vector A,double rad){ return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad)); }//逆时针旋转rad弧度
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%d",&na,&nb);
    for(int i=0;i<na;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
    for(int i=0;i<nb;i++) scanf("%lf%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;
}

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

时间: 2024-11-23 03:22:34

uva 11796 Dog Distance (计算几何-点和直线)的相关文章

相对运动模拟:UVa 11796 Dog Distance

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

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

●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(相对运动)

因为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

UVA11796 Dog Distance 计算几何

计算几何: 题解转自:点击打开链接 首先我们想一下如果甲乙都仅沿着两条线段向前跑,那么他们之间的最短和最长距离怎么算? 假设甲的速度向量为v1(速度向量指甲单位时间所走的位移向量),乙的速度向量为v2. 那么可以把甲看成是静止不动的,而让乙一个人以v2-v1的速度向量去运动(画图验证下). 且假设甲和乙都运动了t秒时间,那么我们可以知道上面的过程类似于甲不动,乙从自己的起点运动了t*(v2-v1)的位移向量. 而乙已知起点和位移向量,那么它就是在一条线段上. 最终我们要求的就是甲这个不动的点到乙

uva 11178 Morley&amp;#39;s Theorem(计算几何-点和直线)

Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states that that the lines trisecting the angles of an arbitrary plane triangle meet at the vertices of an equilateral triangle. For example in the figure below

uva 11178 Morley&#39;s Theorem(计算几何-点和直线)

Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states that that the lines trisecting the angles of an arbitrary plane triangle meet at the vertices of an equilateral triangle. For example in the figure below