UVA_11796_Dog_Distance_(计算几何)

描述


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2896

两只狗分别沿着各自的折线匀速跑,同时出发,同时到达.求其间它们两的最大距离和最小距离之差.

11796
Dog Distance
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 jour-
ney.
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, (P 1 P 2 P 3 . . . P N ). The dog following this path will
start from P 1 and follow the line joining with P 2 , and then it will follow the line joining P 2 -P 3 , then
P 3 -P 4 and so on until it reaches P n .UVA_11796_Dog_Distance_(计算几何)
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 X 1 Y 1 X 2 Y 2 . . . X A Y A , (0 ≤ X i , Y i ≤ 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 T , R 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
2
2 2
0 0
0 1
3 2
635
117
10 0
10 1
187 241 269 308 254
663 760 413
Sample Output
Case 1: 0
Case 2: 404

分析


先考虑简单的情况:两条狗都在线段上跑.由于运动是相对的,可以以其中一条狗为参考系,那么另一条狗以v2-v1(矢量差)的速度相对于狗#1运动,那么最大最小距离就是点到线段的距离了.

再考虑复杂的情况:可以把复杂情况分解为多个上述的简单情况.用pa,pb记录两条狗当前的位置,sa,sb表示它们刚经过各自的第sa和第sb个折点,计算它们俩谁先到达下一个折点,从当前时间到它到达下一个折点的时间,在这段时间里就是简单情况,求解完之后再更新pa,pb,如果需要的话还要更新sa,sb.由于每次至少经过一个折点,所以最多进行A+B次处理.

 #include <bits/stdc++.h>
using namespace std; const int maxn=;
const double INF=1e10,eps=1e-; int n,A,B,kase;
double Max,Min;
struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){}
}a[maxn],b[maxn];
typedef Point Vector;
int dcmp(double x){
if(fabs(x)<eps) return ;
return x>?:-;
}
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 < (Vector a,Vector b){ return a.x<b.x||(a.x==b.x&&a.y<b.y); }
bool operator == (Vector a,Vector b){ return dcmp(a.x-b.x)==&&dcmp(a.y-b.y)==; }
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; }
double length(Vector a){ return sqrt(dot(a,a)); }
double distance_to_segment(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)<)) return length(v2);
if(dcmp(dot(v1,v3)>)) return length(v3);
return fabs(cross(v1,v2)/length(v1));
}
void update(Point p,Point a,Point b){//简单情况
Min=min(Min,distance_to_segment(p,a,b));
Max=max(Max,length(p-a));
Max=max(Max,length(p-b));
}
void solve(){
scanf("%d%d",&A,&B);
double lena=,lenb=;
for(int i=;i<=A;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
for(int i=;i<=B;i++) scanf("%lf%lf",&b[i].x,&b[i].y);
for(int i=;i<A;i++) lena+=length(a[i+]-a[i]);
for(int i=;i<B;i++) lenb+=length(b[i+]-b[i]);
Min=INF,Max=-INF;
int sa=,sb=;
Point pa=a[],pb=b[];
while(sa<A){
double la=length(a[sa+]-pa);
double lb=length(b[sb+]-pb);//用总长来代表速度
double t=min(la/lena,lb/lenb);
Vector va=(a[sa+]-pa)/la*t*lena;
Vector vb=(b[sb+]-pb)/lb*t*lenb;//两个位移向量
update(pa,pb,pb+vb-va);
pa=pa+va;
pb=pb+vb;
if(pa==a[sa+]) sa++;
if(pb==b[sb+]) sb++;
}
printf("Case %d: %.0lf\n",++kase,Max-Min);
}
int main(){
scanf("%d",&n);
while(n--) solve();
return ;
}
上一篇:Hadoop就业面试题


下一篇:Nuttx操作系统