描述


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 .
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 ;
}

UVA_11796_Dog_Distance_(计算几何)的更多相关文章

  1. ACM/ICPC 之 计算几何入门-叉积-to left test(POJ2318-POJ2398)

    POJ2318 本题需要运用to left test不断判断点处于哪个分区,并统计分区的点个数(保证点不在边界和界外),用来做叉积入门题很合适 //计算几何-叉积入门题 //Time:157Ms Me ...

  2. HDU 2202 计算几何

    最大三角形 Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. ACM 计算几何中的精度问题(转)

    http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...

  4. hdu 2393:Higher Math(计算几何,水题)

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)

    Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a b ...

  6. [知识点]计算几何I——基础知识与多边形面积

    // 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vxaq.html 1.前言 ...

  7. POJ 1106 Transmitters(计算几何)

    题目链接 切计算几何,感觉计算几何的算法还不熟.此题,枚举线段和圆点的直线,平分一个圆 #include <iostream> #include <cstring> #incl ...

  8. TYVJ计算几何

    今天讲了计算几何,发几道水水的tyvj上的题解... 计算几何好难啊!@Mrs.General....怎么办.... 这几道题都是在省选之前做的,所以前面的Point运算啊,dcmp啊,什么什么的,基 ...

  9. 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点

    平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...

随机推荐

  1. (转)C#中的Dictionary字典类介绍

    关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionar ...

  2. row_number() OVER (PARTITION BY COL1 ORDER BY COL2)

    select *,ROW_NUMBER() over(partition by deviceID order by RecordDate desc row_number() OVER (PARTITI ...

  3. tar 命令基本使用(加密)

    本文讲述tar命令的基本使用,special: 使用tar命令对文件加密. 假定在当前目录下有一个文件夹/stuff. 1.将/stuff目录下的所有文件打包成为.tar 文件. $ tar -cvf ...

  4. node.js 小爬虫 imooc 2016.03.06

    爬虫目标:获取http://www.imooc.com/learn/348网页中的章节标题和视频信息. var http = require('http'); var cheerio = requir ...

  5. PAT_1002 写出这个数

    宝宝不开心了.自从回家开始百练就上不去POJ也上不去,今天突然HDU也上不去了,PAT25分的题目都快更新完了.我就按顺序往下面更新了.回学校之后题目质量能高出不少= =. 问题描述: 读入一个自然数 ...

  6. 处理safari缓存的办法

    window.onpageshow = function(event) {        if (event.persisted) {             alert("From bac ...

  7. [JS]Cookie精通之路

    [JS]Cookie精通之路 转http://blog.163.com/neu_pdh1983/blog/static/572407020077310528915/ 发布:Cary 媒体:www.Ju ...

  8. CI的面向切面的普通权限验证

    第一步:开启CI的钩子配置,此次不多说看CI手册即可. 第二步:在cofig/hooks.php中进行钩子配置,CI手册中有记载 <?php defined('BASEPATH') OR exi ...

  9. 【 java版坦克大战--事件处理】 坦克动起来了

    折腾了这么久,坦克总算能动了.只贴代码编辑不给上首页,花了半个小时的时间写了n多注释. 再顺便把绘图的原理发在这里: 绘图原理 Component类提供了两个和绘图有关的重要方法: ①   paint ...

  10. ios8.1.2耗电情况严重的解决方法

    打开cydia,搜索ifile(威锋源,版本2.1.0-1).打开ifile,进入路径/Applications.里面有许多程序文件,选择适当的进行禁用(ifile可以禁用程序的活动而不完全删除它,这 ...