跳到正文

题解

题解 P4682 【[ZJOI2007]粒子运动】

GoPoux4约 9 分钟
原文
文章信息与版本

文章信息

发布
2020年8月14日
收录
2026年10月10日
更新
2026年11月11日
篇幅
约 9 分钟
获赞
2
题目
P4682

版本

  1. v1当前

刚学计算几何,来写一篇题解。


先考虑一个粒子的运动情况。

若不考虑碰撞,一个粒子将会沿着一条射线运动,于是可以记录两个量来表示一个粒子:起始点 pp 和速度 v⃗\vec{v}。这样表示有一个非常好用的东西:tt 时刻粒子所在的位置 p′p' 所表示的向量 p′⃗=p⃗+tv⃗\vec{p'}=\vec{p}+t\vec{v}。

当发生碰撞时,有下面这张图:

dPwEwV.jpg

此时 pp 在圆上,碰撞前的速度为 vv ,碰撞后的速度为 v1v_1 ,v2v_2 由 v1v_1 平移而来。我们需要将 vv 旋转 2α2\alpha 至 v2v_2。α\alpha 不难由 v,pv,p 算出:

sin α=cos β=p⃗∗v⃗∣p⃗∣∣v⃗∣{\rm{sin}}\ \alpha={\rm{cos}}\ \beta=\frac{\vec{p} * \vec{v}}{|\vec{p}||\vec{v}|} cos α=sin β=p⃗×v⃗∣p⃗∣∣v⃗∣{\rm{cos}}\ \alpha={\rm{sin}}\ \beta=\frac{\vec{p} \times \vec{v}}{|\vec{p}||\vec{v}|}

由三角恒等变换得:

sin 2α=2 sin α cos α{\rm{sin}}\ 2\alpha=2\ {\rm{sin}}\ \alpha\ {\rm{cos}}\ \alpha cos 2α=cos2 α−sin2 α{\rm{cos}}\ 2\alpha={\rm{cos}}^2\ \alpha-{\rm{sin}}^2\ \alpha

向量旋转有公式,直接贴上:

v=(x,y)v=(x,y) 逆时针旋转 α\alpha 得到 v′=(x cos α−y sin α,y cos α+x sin α)v'=(x\ {\rm{cos}}\ \alpha-y\ {\rm{sin}}\ \alpha,y\ {\rm{cos}}\ \alpha+x\ {\rm{sin}}\ \alpha)。


不难发现,最近距离只与两个粒子有关,于是可以枚举两个粒子 a,ba,b,单独考虑它们的运动情况。

设粒子 aa 的起始点为 pap_a,速度为 va⃗\vec{v_a} ,粒子 bb 的起始点为 pbp_b,速度为 vb⃗\vec{v_b} ,运动时间为 tt,粒子间距离表示为矢量 dis⃗\vec{dis},则有:

dis⃗=(pa⃗+tva⃗)−(pb⃗+tvb⃗)=(pa⃗−pb⃗)+t(va⃗−vb⃗)=(xp+txv,yp+tyv)\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)

粒子间距离即为 ∣dis⃗∣|\vec{dis}|,有:

∣dis⃗∣=(xp+txv)2+(yp+tyv)2=(xv2+yv2)t2+2t(xpxv+ypyv)+xp2+yp2|\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=−xpxv+ypyvxv2+yv2t=-\frac{x_px_v+y_py_v}{x_v^2+y_v^2}

代入原方程:

∣dis⃗∣=(xpxv+ypyv)2xv2+yv2−2(xpxv+ypyv)2xv2+yv2+xp2+yp2|\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} =(xp2+yp2)(xv2+yv2)−(xpxv+ypyv)2xv2+yv2=\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}} =xp2yv2+yp2xv2−2xpypxvyvxv2+yv2=\frac{\sqrt{x_p^2y_v^2+y_p^2x_v^2-2x_py_px_vy_v}}{\sqrt{x_v^2+y_v^2}} =xpyv−ypxvxv2+yv2=\frac{x_py_v-y_px_v}{\sqrt{x_v^2+y_v^2}} =p⃗×v⃗len(v⃗)=\frac{\vec{p}\times\vec{v}}{len(\vec{v})}

这样就能够轻松地求出两个粒子的最小距离。

但是这样算还有一个小问题:tt 有取值范围,不一定能取到对称轴,即二次函数在 tt 的取值范围上是单调的。这个问题会在 a,ba,b 的运动轨迹不相交时出现,特判一下这种情况即可。


Code:

C++
#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;
}
原文
存档评论2 条抓取于