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 .
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_(计算几何)的更多相关文章
- ACM/ICPC 之 计算几何入门-叉积-to left test(POJ2318-POJ2398)
POJ2318 本题需要运用to left test不断判断点处于哪个分区,并统计分区的点个数(保证点不在边界和界外),用来做叉积入门题很合适 //计算几何-叉积入门题 //Time:157Ms Me ...
- HDU 2202 计算几何
最大三角形 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- ACM 计算几何中的精度问题(转)
http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...
- hdu 2393:Higher Math(计算几何,水题)
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
- [知识点]计算几何I——基础知识与多边形面积
// 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vxaq.html 1.前言 ...
- POJ 1106 Transmitters(计算几何)
题目链接 切计算几何,感觉计算几何的算法还不熟.此题,枚举线段和圆点的直线,平分一个圆 #include <iostream> #include <cstring> #incl ...
- TYVJ计算几何
今天讲了计算几何,发几道水水的tyvj上的题解... 计算几何好难啊!@Mrs.General....怎么办.... 这几道题都是在省选之前做的,所以前面的Point运算啊,dcmp啊,什么什么的,基 ...
- 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点
平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...
随机推荐
- EasyUI兼容IE问题
上网搜了下,发现说明白的解决方案不多,于是记录了一下: 根本原因是JQuery的版本造成IE8及以下兼容的问题,(由于EasyUI是基于JQuery框架的,而JQuery貌似从2013年为了简洁代码, ...
- ios隐藏导航栏底线条和导航、状态栏浙变色
方法一遍历法: 在你需要隐藏的地方调用如下代码 [self findlineviw:self.navigationBar].hidden = YES; -(UIImageView*)findlinev ...
- react native android 开发,基础配置笔记。
一.React-native-device-info https://github.com/rebeccahughes/react-native-device-info 二.修改App名称 三.定位权 ...
- Codevs 4600 [NOI2015]程序自动分析
4600 [NOI2015]程序自动分析 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 在实现程序自动分析的过程中,常常需 ...
- js清空前后空格
function trim(sValue){ var lastValue=this.replace(/(^\s*)|(\s*$)/g,""); ...
- nodejs -formidable模块实现图片上传。
var form = new formidable.IncomingForm(); form.uploadDir="/localnonobank/test/images/"; ...
- input表单
submit:点击submit按钮表单就会被提交给服务器,中文IE下默认按钮文本为“提交查询”,可以设置value属性修改按钮的显示文本 text:size属性为宽度,value为值,maxlengt ...
- js实现小数点后保留N位并可以四舍五入——js对float数据的处理
曾经遇到的两次的问题,关于前台接受后台传过来的float数据,一显示就是老长的小数点后缀,很烦人,后来想着用js把其进行四舍五入处理下,经网上查找,一哥们的代码如下:(很好用,感谢下!) functi ...
- centos VM 识别U盘
在VM设置选项中,选择 USB Controller 选项,设置相关参数即可
- php类的属性
属性声明是由关键字 public,protected 或者 private 开头,后面跟一个普通的变量声明来组成.属性的变量可以设置初始化的默认值,默认值必须是常量. class Car { //定义 ...