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, thenP3-P4 and so on until it reaches Pn.

Input

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

Each test case starts with 2 positive integers A(2A50), B(2B50). 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

题目大意:两条狗按各自的折线线路奔跑,它们同时出发同事到达终点。求奔跑过程中两只狗的最大距离与最小距离之差。

标记为红色的均为向量,其他的则为点。

分析:整个过程可以按折点细化成多个它们沿着各自的线段奔跑同时出发同时到达终点,假设它们的起点为sa,sb终点为ea,eb,位移为Sa,Sb。运动是相对的所以可以认为一只狗不动待在sa,另一只口沿着直线跑。以a为参考系,b相对a的速度为Vb-Va。由于它们的运动时间相等s=vt,所以这个过程等同与b从sb移动到(sb+(Sb-Sa)),即转化为求点sa到线段(sb --- sb+(Sb-Sa))的最大距离与最小距离。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std; struct Point
{
double x,y;
Point(double x=,double y=):x(x),y(y) {}
};
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);}
bool operator < (const Point &a,const Point &b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
const double eps=1e-;
int dcmp(double x)
{
if(fabs(x)<eps) return ;
else return x<?-:;
}
bool operator == (const Point &a,const Point &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 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));
} Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)//两直线的交点
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
} double DistanceToLine(Point P,Point A,Point B)//点到直线的距离
{
Vector v1=B-A,v2=P-A;
return fabs(Cross(v1,v2))/Length(v1);
} 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)) < ) return Length(v2);
else if(dcmp(Dot(v1,v3)) > ) return Length(v3);
else return fabs(Cross(v1,v2))/Length(v1);
} Point GetLineProjection(Point P,Point A,Point B)//P在直线上的投影点
{
Vector v=B-A;
return A+v*(Dot(v,P-A)/Dot(v,v));
} bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)//两线段规范相交(不包括端点)
{
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)< && dcmp(c3)*dcmp(c4)<;
} bool OnSegment(Point p,Point a1,Point a2)//点是否在线段上
{
return dcmp(Cross(a1-p,a2-p))== && dcmp(Dot(a1-p,a2-p))<;
} double PolygonArea(Point *p,int n)//多边形的有向面积
{
double area=;
for(int i=;i<n-;i++)
area+=Cross(p[i]-p[],p[i+]-p[]);
return area/;
} const int maxn=;
int T,A,B,Icase;
Point P[maxn],Q[maxn];
double Min,Max;
double LenA,LenB,La,Lb,t;
int i,sa,sb;
Point Pa,Pb;
Vector Va,Vb; Point read_point()
{
Point p;
scanf("%lf %lf",&p.x,&p.y);
return p;
} double min(double a,double b)
{
if(a-b>0.00000001) return b;
else return a;
} double max(double a,double b)
{
if(a-b>0.00000001) return a;
else return b;
} void update(Point P,Point A,Point B)
{
Min=min(Min,DistanceToSegment(P,A,B));
Max=max(Max,Length(P-A));
Max=max(Max,Length(P-B));
//printf("%.lf %.lf %.lf %.lf %.lf %.lf\n",P.x,P.y,A.x,A.y,B.x,B.y);
} int main()
{
Icase=;
scanf("%d",&T);
while(T--)
{
Icase++;
Min=1e9,Max=-1e9;
//printf("%.lf %.lf\n",Min,Max);
scanf("%d %d",&A,&B);
for(i=;i<A;i++) P[i]=read_point();
for(i=;i<B;i++) Q[i]=read_point();
LenA=LenB=;
for(i=;i<A;i++) LenA+=Length(P[i]-P[i-]);
for(i=;i<B;i++) LenB+=Length(Q[i]-Q[i-]);
sa=sb=;
Pa=P[],Pb=Q[];
while(sa<A- && sb<B-)
{
La=Length(P[sa+]-Pa);
Lb=Length(Q[sb+]-Pb);
t=min(La/LenA,Lb/LenB);
Va=(P[sa+]-Pa)/La*t*LenA;
Vb=(Q[sb+]-Pb)/Lb*t*LenB;
update(Pa,Pb,Pb+Vb-Va);
Pa=Pa+Va;
Pb=Pb+Vb;
if(Pa==P[sa+]) sa++;
if(Pb==Q[sb+]) sb++;
}
//printf("%lf %lf\n",Max,Min);
printf("Case %d: %.0lf\n",Icase,Max-Min);
}
return ;
}

uva 11798 相对运动的最小最大距离的更多相关文章

  1. 训练指南 UVA - 11419(二分图最小覆盖数)

    layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...

  2. UVa 1658 - Admiral(最小费用最大流 + 拆点)

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

  3. UVA 11354 Bond(最小瓶颈路+倍增)

    题意:问图上任意两点(u,v)之间的路径上,所经过的最大边权最小为多少? 求最小瓶颈路,既是求最小生成树.因为要处理多组询问,所以需要用倍增加速. 先处理出最小生成树,prim的时间复杂度为O(n*n ...

  4. UVa 10806 Dijkstra,Dijkstra(最小费用最大流)

    裸的费用流.往返就相当于从起点走两条路到终点. 按题意建图,将距离设为费用,流量设为1.然后增加2个点,一个连向节点1,流量=2,费用=0;结点n连一条同样的弧,然后求解最小费用最大流.当且仅当最大流 ...

  5. UVA - 10480 Sabotage【最小割最大流定理】

    题意: 把一个图分成两部分,要把点1和点2分开.隔断每条边都有一个花费,求最小花费的情况下,应该切断那些边.这题很明显是最小割,也就是最大流.把1当成源点,2当成汇点,问题是要求最小割应该隔断那条边. ...

  6. UVa 1331 最大面积最小的三角剖分

    https://vjudge.net/problem/UVA-1331 题意:输入一个多边形,找一个最大三角形面积最小的三角剖分,输出最大三角形的面积. 思路: 最优三角剖分. dp[i][j]表示从 ...

  7. UVa 1658 海军上将(最小费用最大流)

    https://vjudge.net/problem/UVA-1658 题意: 给出一个v个点e条边的有向加权图,求1~v的两条不相交(除了起点和终点外公共点)的路径,使得权和最小. 思路:把2到v- ...

  8. UVa 10791 最小公倍数的最小和(唯一分解定理)

    https://vjudge.net/problem/UVA-10791 题意: 输入整数n,求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小. 思路: 首先对n进行质因数分解,举个例子 ...

  9. Uva 10791 最小公倍数的最小和 唯一分解定理

    题目链接:https://vjudge.net/contest/156903#problem/C 题意:给一个数 n ,求至少 2个正整数,使得他们的最小公倍数为 n ,而且这些数之和最小. 分析: ...

随机推荐

  1. 转向ARC的说明

    转自hherima的博客原文:Transitioning to ARC Release Notes(苹果官方文档) ARC是一个编译器特征,它提供了对OC对象自动管理内存.ARC让开发者专注于感兴趣的 ...

  2. 讲课笔记1——meta标签、表格标签

    图片属性:src(source): 图片的来源(路径),可以放置本地图片,也可以放网上的图片的url地址 [相对路径:        ./:当前目录            ../:跳出当前目录,到上一 ...

  3. C#背景图片自适应

    1.选中窗体修改属性 2.在load添加代码 private void Form1_Load(object sender, EventArgs e) { this.BackgroundImageLay ...

  4. OpenWrite方法打开现有文件并进行写入

    实现效果: 知识运用: File类的OpenWrite方法 //实现打开现有文件以进行写入 public static FileStream OpenWrite (string path) Encod ...

  5. 题解 P5082 【成绩】

    随机跳题跳到了这一题,一看是个红题,本蒟蒻就 艰难地思考起来 高兴地写起来 这题实在不能用数组,用了数组就RE 一开始就卡在这上面了 说实话,这道题真的 很难 不算很难,只要照着公式往上面套就行了 废 ...

  6. non-JRMP server at remote endpoint

    #在相应的domain的domain.xml文件添加下面红色设置,并重启domain <admin-service system-jmx-connector-name="system& ...

  7. ios之UISlider

    初始化一个Slider   UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 400,320 , 20)]; 滑块是一个标 ...

  8. [SDOi2012]Longge的问题 (数论)

    Luogu2303 [SDOi2012]Longge的问题 题目 题目背景 SDOi2012 题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N, ...

  9. UVa-101-木块问题

    这题用vector比较好写,我们设置对应的几个函数,然后进行相应的操作来简化代码,这样才不易出错. 对于输入和操作来说我们经分析之后,可以看到最后一个操作时最原始的操作也就是不需要还原任意一个堆任意高 ...

  10. (41)zabbix监控api接口性能及可用性 天气预报api为例

    现在各种应用都走api,例如淘宝,天气预报等手机.pad客户端都是走api的,那么平时也得对这些api做监控了.怎么做呢?zabbix的web监控是不二选择了.今天就以天气预报api作为一个例子. 天 ...