UVA 11796 - Dog Distance 向量的应用
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2896
题目大意:
甲和乙两条狗分别沿着一条折线跑,它们速度未知,但同时出发并且同时到达终点,并且都是匀速奔跑。求奔跑过程中两只狗的最大距离与最小距离之差。
思路:
因为运动是相对的,我们可以认为甲静止不动,乙沿着直线走。则问题就转化为点到线段的最小距离。
那么,我们对于每段分析,看谁先到达该线段的终点,则该段可以用上面的方法求。
把a看成不动的,则有b运动到了cb+vb-va(其中va,vb分别为a和b的位移向量,ca,cb分别为a和b初始位置)
那么就转换为sa到线段cb+vb-va的距离。(最小距离为点到直线,而最大距离一定在线段两边)
至于速度的表示,设1S到达终点,那么速度就是总长。
PS:直接用模版就是爽。哈哈
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=50+10;
const int INF=99999999;
double ans_max,ans_min;
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y){ }
};
typedef Point Vector;
const double eps = 1e-8;
int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }
Vector operator + (const Vector& A, const Vector& B) { return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (const Point& A, const Point& B) { return Vector(A.x-B.x, A.y-B.y); }
Vector operator * (const Vector& A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (const Vector& A, double p) { return Vector(A.x/p, A.y/p); }
bool operator == (const Point& a, const Point &b) {
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
} double Dot(const Vector& A, const Vector& B) { return A.x*B.x + A.y*B.y; }
double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; }
double Length(const Vector& A) { return sqrt(Dot(A, A)); } double DistanceToSegment(const Point& P, const Point& A, const 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);
} void update(Point P,Point A,Point B)
{
ans_max=max(ans_max,Length(P-A));//最大的一定在端点
ans_max=max(ans_max,Length(P-B));
ans_min=min(ans_min,DistanceToSegment(P,A,B));//最小的为点到直线的距离
} Point a[MAXN],b[MAXN]; int main()
{
int T,kase=1;
scanf("%d",&T);
while(T--)
{
int A,B;
scanf("%d%d",&A,&B);
for(int i=0;i<A;i++)
scanf("%lf%lf",&a[i].x,&a[i].y); for(int i=0;i<B;i++)
scanf("%lf%lf",&b[i].x,&b[i].y); double lena=0,lenb=0; //总长度
for(int i=0;i<A-1;i++)
lena+=Length(a[i+1]-a[i]);
for(int i=0;i<B-1;i++)
lenb+=Length(b[i+1]-b[i]); int ia=0,ib=0; //index
Point ca=a[0],cb=b[0]; //current point
ans_max=-INF,ans_min=INF;
while(ia<A-1 && ib<B-1)
{
double sa=Length(a[ia+1]-ca); //到下一个拐点的距离
double sb=Length(b[ib+1]-cb);
double ta=sa/lena; //设到终点都为1S,那么每一段时间就是除以总长。
double tb=sb/lenb;
double t=min(ta,tb);
Vector va=(a[ia+1]-ca)/sa*t*lena;
Vector vb=(b[ib+1]-cb)/sb*t*lenb;
update(ca,cb,cb+vb-va);//把a看成静止的,则b相对从cb运动到了vb-va处。
ca=ca+va;
cb=cb+vb;
if(ca==a[ia+1]) ia++;
if(cb==b[ib+1]) ib++;
}
printf("Case %d: %.0lf\n",kase++,ans_max-ans_min);
} return 0;
}
UVA 11796 - Dog Distance 向量的应用的更多相关文章
- UVA 11796 Dog Distance(几何)
		Dog Distance [题目链接]Dog Distance [题目类型]几何 &题解: 蓝书的题,刘汝佳的代码,学习一下 &代码: // UVa11796 Dog Distance ... 
- UVA 11796 Dog Distance(向量)
		题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31962 [代码] #include<cstdio> # ... 
- ●UVA 11796 Dog Distance
		题链: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ... 
- UVA   11796 - Dog Distance
		题意 两条狗啊,同时跑,,同时结束,各自跑各自的道路,问跑的过程中,他们最大距离和最小距离的差: 方法 恶心一点就是,最大最小距离的求解方法,假设两只狗都只有一条线段要跑,则可以判定在端点处有最大 ... 
- 简单几何(相对运动距离最值) UVA 11796 Dog Distance
		题目传送门 题意:两只狗在折线上跑,速度未知,同时出发,同时达到.问跑的过程中,两狗的最大距离和最小距离的差 分析:训练指南P261,考虑相对运动,设A静止不动,B相对A运动,相对的运动向量:Vb - ... 
- UVa 11796 计算几何
		题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ... 
- UVA 10140 - Prime Distance(数论)
		10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ... 
- UVA 11437 - Triangle Fun 向量几何
		http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ... 
- UVA 11796
		题意: 有两个狗, 按照 多边形跑,不知道两条狗的速度,但是狗是同时出发,同时到达终点的 输出两条狗的 最大相距距离 - 最小相距距离: 思路 : 用物理的相对运动来计算, 每次只计算 两条狗的直线 ... 
随机推荐
- 洛谷 P2080 增进感情
			P2080 增进感情 题目背景 小明和小红的感情,是慢慢发展起来的. 题目描述 他们对对方分别有一个好感值.定义两人的亲密程度为两人的好感值之和. 如果他们的亲密程度达到V,则他们将走到一起.他们以后 ... 
- Junit4.x高级使用方法具体解释(一)
			近期整理代码的时候,总习惯把一些经常使用的工具类和方法等都写在junit中,这样能够方便于在想用的时候直接copy,在用junit的时候学到了一些比較实用的东西.记录例如以下: 1.使用junit进行 ... 
- poj 1087 A Plug for UNIX(字符串编号建图)
			A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14862 Accepted: 5026 ... 
- Android ScrollView滚动实现大众点评、网易云音乐评论悬停效果
			今天听着网易云音乐,写着代码,真是爽翻了. http://blog.csdn.net/linshijun33/article/details/47910833 网易云音乐这个产品亮点应该在评论这一模块 ... 
- sdut 2805(最小生成树)
			大家快来A水题 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 (1<= N <=2000)(1<= M <= N*(N-1)/2 ... 
- Eclipse中JDK的配置
			window -> preference -> java -> install jres -> add -> standard vm -> 设置好相应的jre ho ... 
- 60.浅谈nodejs中的Crypto模块
			转自:https://www.cnblogs.com/c-and-unity/articles/4552059.html node.js的crypto在0.8版本并没有改版多少,这个模块的主要功能是加 ... 
- ElasticSearch 架构图
			ElasticSearch 架构图 从下往上来分析ElasticSearch 架构图 Gateway代表ElasticSearch索引的持久化存储方式. 在Gateway中,ElasticSearch ... 
- C#List<string>和string[]之间的相互转换
			一.LIST概述 所属命名空间:System.Collections.Generic public class List<T> : IList<T>, IColle ... 
- Kinect 开发 —— 保持视频影像
			相比直接将影像显示出来,如果能将录制到的影像保存到硬盘上就好了.但是,影像录制,是需要一定的技巧,在网上可以看到很多例子演示如何将Kinect获取到的影像以图片的形式保存到本地,前面的博文也介绍了这一 ... 
