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
题意: 有两个狗, 按照 多边形跑,不知道两条狗的速度,但是狗是同时出发,同时到达终点的 输出两条狗的 最大相距距离 - 最小相距距离: 思路 : 用物理的相对运动来计算, 每次只计算 两条狗的直线 ...
随机推荐
- 树莓派 使用python来操作GPIO 控制LED灯
一.创建python驱动和控制GPIO 先新建一个文件夹用于放置脚本 mkdir python_gpio 进入文件夹内新建一个gpio_blink.py的脚本 cd python_gpio touch ...
- Android学习总结(2)——App客户端与服务器交互中的token
学习Token Token是什么? Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便将此Token返回给客户端,以后客户端只需带上这个Tok ...
- 【Mysql】经常使用指令之——忘记password
上一篇文章基本总结了下myql下通过指令怎么创建用户.详见:[Mysql]经常使用指令之--用户操作(创建,授权,改动.删除) 今天说下特殊情况,忘记password了怎么办??? 重装.删除配置等能 ...
- java对象和json数据转换实现方式1-使用json-lib实现
測试代码: package com.yanek.util.json; import java.util.ArrayList; import java.util.List; import net.sf. ...
- 基于SVM的数据分类预測——意大利葡萄酒种类识别
update:把程序源代码和数据集也附上http://download.csdn.net/detail/zjccoder/8832699 2015.6.24 --------------------- ...
- POJ 3170 线段树优化DP
题意: 思路: 先搞一个vector 存以T2结尾的结构体 (结构体里面有开始工作的时间和花费) f[i]表示取区间[M,i)的代价 易得f[i]=min(f[k]+w,f[i]);T1<=k ...
- POJ 2433 枚举
题意: 思路: 每回枚举去哪个山包 枚举的姿势很重要 //By SiriusRen #include <cstdio> #include <algorithm> using n ...
- Kinect 开发 —— 录音
不涉及语音识别~~ <Window x:Class="KinectRecordAudio.MainWindow" xmlns="http://schemas.mic ...
- at&&atq&&atrm---定时任务
at放在 ls /var/spool/at/ 目录下 At的配置文件/etc/at.deny和/etc/at.allow 如果deny单独存在,则是deny以为的所有用户都可以使用at命令 如果all ...
- newgrp---将当前登录用户临时加入到已有的组中
Linux中的newgrp命令主要是将当前登录用户临时加入到已有的组中,用法如下: [linuxidc@localhost etc]$ newgrp grptest 上面命令的含义是将用户linuxi ...