题解 P4682 【[ZJOI2007]粒子运动】
GoPoux4
·
·
题解
刚学计算几何,来写一篇题解。
先考虑一个粒子的运动情况。
若不考虑碰撞,一个粒子将会沿着一条射线运动,于是可以记录两个量来表示一个粒子:起始点 p 和速度 \vec{v}。这样表示有一个非常好用的东西:t 时刻粒子所在的位置 p' 所表示的向量 \vec{p'}=\vec{p}+t\vec{v}。
当发生碰撞时,有下面这张图:
此时 p 在圆上,碰撞前的速度为 v ,碰撞后的速度为 v_1 ,v_2 由 v_1 平移而来。我们需要将 v 旋转 2\alpha 至 v_2。\alpha 不难由 v,p 算出:
{\rm{sin}}\ \alpha={\rm{cos}}\ \beta=\frac{\vec{p} * \vec{v}}{|\vec{p}||\vec{v}|}
{\rm{cos}}\ \alpha={\rm{sin}}\ \beta=\frac{\vec{p} \times \vec{v}}{|\vec{p}||\vec{v}|}
由三角恒等变换得:
{\rm{sin}}\ 2\alpha=2\ {\rm{sin}}\ \alpha\ {\rm{cos}}\ \alpha
{\rm{cos}}\ 2\alpha={\rm{cos}}^2\ \alpha-{\rm{sin}}^2\ \alpha
向量旋转有公式,直接贴上:
---
不难发现,最近距离**只与两个粒子**有关,于是可以枚举两个粒子 $a,b$,单独考虑它们的运动情况。
设粒子 $a$ 的起始点为 $p_a$,速度为 $\vec{v_a}$ ,粒子 $b$ 的起始点为 $p_b$,速度为 $\vec{v_b}$ ,运动时间为 $t$,粒子间距离表示为矢量 $\vec{dis}$,则有:
$$
\vec{dis}=(\vec{p_a}+t\vec{v_a})-(\vec{p_b}+t\vec{v_b})=(\vec{p_a}-\vec{p_b})+t(\vec{v_a}-\vec{v_b})=(x_p+tx_v,y_p+ty_v)
$$
粒子间距离即为 $|\vec{dis}|$,有:
$$
|\vec{dis}|=\sqrt{(x_p+tx_v)^2+(y_p+ty_v)^2}=\sqrt{(x_v^2+y_v^2)t^2+2t(x_px_v+y_py_v)+x_p^2+y_p^2}
$$
这个二次函数在对称轴取到最小值,即:
$$
t=-\frac{x_px_v+y_py_v}{x_v^2+y_v^2}
$$
代入原方程:
$$
|\vec{dis}|=\sqrt{\frac{(x_px_v+y_py_v)^2}{x_v^2+y_v^2}-\frac{2(x_px_v+y_py_v)^2}{x_v^2+y_v^2}+x_p^2+y_p^2}
$$
$$
=\frac{\sqrt{(x_p^2+y_p^2)(x_v^2+y_v^2)-(x_px_v+y_py_v)^2}}{\sqrt{x_v^2+y_v^2}}
$$
$$
=\frac{\sqrt{x_p^2y_v^2+y_p^2x_v^2-2x_py_px_vy_v}}{\sqrt{x_v^2+y_v^2}}
$$
$$
=\frac{x_py_v-y_px_v}{\sqrt{x_v^2+y_v^2}}
$$
$$
=\frac{\vec{p}\times\vec{v}}{len(\vec{v})}
$$
这样就能够轻松地求出两个粒子的最小距离。
但是这样算还有一个小问题:$t$ 有取值范围,不一定能取到对称轴,即二次函数在 $t$ 的取值范围上是单调的。这个问题会在 $a,b$ 的运动轨迹不相交时出现,特判一下这种情况即可。
---
Code:
```cpp
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define maxn 105
#define Rint register int
#define INF 0x3f3f3f3f
using namespace std;
typedef long long lxl;
const double eps=1e-9;
template <typename T>
inline T read()
{
T x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
inline bool zero(double x) {return fabs(x)<eps;}
struct Point
{
double x,y;
inline Point(double x=0,double y=0):x(x),y(y){}
inline Point operator + (const Point &T)const// 加
{
return Point(x+T.x,y+T.y);
}
inline Point operator - ()const// 取负
{
return Point(-x,-y);
}
inline Point operator - (const Point &T)const// 减
{
return *this+(-T);
}
inline Point operator * (const double &a)const// 数乘
{
return Point(x*a,y*a);
}
inline Point operator / (const double &a)const// 数除
{
return Point(x/a,y/a);
}
inline double operator * (const Point &T)const// 点乘
{
return x*T.x+y*T.y;
}
inline double operator % (const Point &T)const// 叉乘
{
return x*T.y-y*T.x;
}
inline double len() {return sqrt((*this) * (*this));}// 向量长度
inline void rotate(double cosa,double sina)// 向量旋转 a 角度
{
(*this)=Point(x*cosa-y*sina,x*sina+y*cosa);
}
inline Point unit()// 同方向的单位向量
{
return (*this)/(*this).len();
}
inline void read()
{
scanf("%lf%lf",&x,&y);
}
};
typedef Point Vec;// 向量的坐标表示
struct node
{
Point p;// 起始点
Vec v;// 速度
double t,T;// t:还需多久发生碰撞 T:走过该方向的割线所需时间
int cnt;// 碰撞次数
}A[maxn];
Point cir;
double R,ans=INF;
int n,k;
inline void calcu(node &a)
{
double v=a.v.len();// 速率
double d=fabs(a.p%a.v)/v;// 圆心到直线的距离
double m=sqrt(R*R-d*d);// 割线长度的一半
a.T=2*m/v;
a.t=(m+a.v.unit()*(-a.p))/v;
}
inline void flip(node &a)// 碰撞
{
double len=a.p.len()*a.v.len();
double sn=a.p*a.v/len,cs=(a.p%a.v)/len;
double sina=2*sn*cs,cosa=cs*cs-sn*sn;
a.v.rotate(cosa,sina);
}
inline void update(Point pa,Point pb,Vec va,Vec vb)
{
if((pa-pb)*(vb-va)>0&&(pa+va-pb-vb)*(va-vb)>0)// 特判不相交
ans=min(ans,fabs((pa-pb)%(vb-va))/(vb-va).len());
else ans=min(ans,min((pa-pb).len(),(pa+va-pb-vb).len()));
}
inline void solve(node a,node b)
{
double t;
Vec v1,v2;
while(a.cnt<k&&b.cnt<k)
{
t=min(a.t,b.t);
a.t-=t;b.t-=t;
v1=a.v*t,v2=b.v*t;
update(a.p,b.p,v1,v2);
a.p=a.p+v1,b.p=b.p+v2;
if(zero(a.t)) flip(a),a.t=a.T,++a.cnt;
if(zero(b.t)) flip(b),b.t=b.T,++b.cnt;
}
}
int main()
{
// freopen("P4682.in","r",stdin);
cir.read();scanf("%lf",&R);
n=read<int >(),k=read<int >();
for(int i=1;i<=n;++i)
{
A[i].p.read(),A[i].v.read();
A[i].p=A[i].p-cir;
calcu(A[i]);
}
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j)
solve(A[i],A[j]);
printf("%.3lf\n",ans);
return 0;
}
```