P16599 [SYSUCPC 2025] Arc Path 题解
Bingxiu2
·
·
题解
在工单被骂复杂度错误骂无语了所以来发篇题解。
首先说明一下本做法时间复杂度为 O(n^2+Kn \log nV),其中 K=30。\log V 是因为计算 \gcd。不需要哈希。
考虑把所有交点和 S,T 作为顶点建图,在同一个圆上相邻的两个点连边,边权为走过这段弧的耗时。
这样只要建出这幅图然后在上面跑 S 到 T 的最短路就好了。
然后容易发现如果直接把圆两两间的切点都存起来那么空间会炸到 O(n^2),时间会炸到 O(n^2 \log n),显然是过不了的。
那么我们考虑先 O(n^2) 计算出每个圆被其他圆共覆盖了多少次,这是容易计算的(因为保证任意两圆要么相切要么无交点)。记第 i 个圆被覆盖了 \mathrm{cov}_i 次。
为了方便,下面记 \boxed{i} 为第 i 个圆。
考虑在判断交点时,只判断所有满足 |\mathrm{cov}_i-\mathrm{cov}_j| \le 1 的圆的交点。我们来证明这一点。
- 若在此交点处 \boxed i 与另一个圆内切,则必有与 \boxed i 半径最接近且与 \boxed i 在此处内切的圆 \boxed j,则满足 |\mathrm{cov}_i-\mathrm{cov}_j|=1,则该交点会被放入 \boxed i 的集合内;
- 否则在此交点处 \boxed i 与一些圆外切,且不与任何圆在此内切,则取所有与 \boxed i 在此外切的圆中半径最大的圆 \boxed j,则满足 \mathrm{cov}_i=\mathrm{cov}_j。
于是这种做法不会漏掉任何交点或在任何圆上漏掉其经过的交点。
接下来我们归纳证明,n 个两两相切或不相交的圆的所有切点 P 的经过次数减一之和小于 6n,也就是:
当 $n=1$ 的时候 $0<6$ 显然成立。\
假设 $n=n_0-1(n_0 \in \mathbb{Z_{\ge 2}})$ 成立,则 $n=n_0$ 时:\
考虑半径最小的圆,不妨设 $\boxed 1$ 半径最小。\
则显然没有圆在 $\boxed 1$ 内部。分类讨论。下面的外切在同切点时只看半径最大的(因为只在数 $\boxed i$ 经过的切点个数)。
- 若存在 $2 \le i \le n_0$,使得 $\boxed 1$ 与 $\boxed i$ 内切,则:

由于 $\boxed 1$ 半径最小,故 $\angle CO_1D \ge \angle CO_1G+\angle DO_1H \ge \frac{\pi}{6}+\frac{\pi}{6}=\frac{\pi}{3}$。而由于在 $\boxed i$ 内部,则所有相邻圆的 $\angle CO_1D$ 之和小于 $\pi$,因此至多 $3$ 个圆与 $\boxed i$ 这样相切,则 $\boxed i$ 经过的切点个数不超过 $4$;
- 否则由于所有与 $\boxed i$ 相切的圆中相邻圆的 $\angle CO_1D$ 之和不超过 $2\pi$,则 $\boxed i$ 经过的切点个数不超过 $6$。
于是根据归纳假设,除了 $\boxed i$ 外其他圆的 $S'<6(n_0-1)$,则 $S=S'+(\boxed i\ 经过的切点数)<6(n_0-1)+6=6n_0$,得证。
于是切点的个数不超过 $6n$,且所有圆经过的切点个数之和不超过 $2S$,也就是小于 $12n$(别问我为什么 $x \ge 2$ 时 $x \le 2(-1+x)$)。
然后我们再证明,对于固定的 $\boxed i$ 和一个切点 $P$,满足 $|\mathrm{cov}_i-\mathrm{cov}_j| \le 1$ 且 $\boxed i$ 与 $\boxed j$ 相切于 $P$ 的不同 $\boxed j$ 个数不超过 $5$。

如图,与圆 $D$ 在 $P$ 处相切且满足对 $\rm cov$ 的限制的圆分别为 $5$ 个紫色的圆 $C,E,I,J,K$。
证明很容易,内切至多 $2$ 个,外切至多 $3$ 个,故总共至多 $5$ 个。
于是该做法至多判断条件成立 $\frac{12n \times 5}{2}=30n$ 次,复杂度正确。
时间复杂度在开头写了,为 $O(n^2+Kn\log nV)$,其中 $K=30$。
不要再说这题没正解只能靠卡常了。
:::info[[AC 记录](https://www.luogu.com.cn/record/279477423)]
AC Code:
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
struct pt{ll x,y;int m;inline bool operator<(const pt &other) const{return x!=other.x?x<other.x:y!=other.y?y<other.y:m<other.m;};inline bool operator==(const pt &other) const{return x==other.x&&y==other.y&&m==other.m;}};
struct pt2{int id;ld a;inline bool operator<(const pt2 &other) const{return a<other.a;}};
struct Node{int f;ld e;inline bool operator<(const Node &other) const{return e>other.e;}};
const int N=60009;
ull HSH;
int n,s,t,A,B,C,D,g,cnt,x[10009],y[10009],r[10009],v[10009];
const ld pi=acos(-1);
pt U,V,e,w[N];
pt2 o[10009];
vector<pt> a[10009];
set<pt> M,P[10009];
map<pt,int> m;
ld d[N];
int cov[10009];
vector<Node> b[N];
priority_queue<Node> q;
bool vst[N];
inline void chk(pt X,int i){if((X.x-x[i])*(ll)(X.x-x[i])+(X.y-y[i])*(ll)(X.y-y[i])==r[i]*(ll)r[i]) a[i].push_back(X);}
inline void link(int u,int v,ld w){b[u].push_back((Node){v,w}),b[v].push_back((Node){u,w});}
inline void add(int i,pt X){if(P[i].find(X)==P[i].end()) P[i].insert(X),a[i].push_back(X);}
inline void add2(pt X){if(M.find(X)==M.end()) M.insert(X),++cnt,w[cnt]=X;}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>A>>B>>C>>D,cnt=2,U.x=A,U.y=B,U.m=1,V.x=C,V.y=D,V.m=1,w[1]=U,w[2]=V;
for(int i=1;i<=n;++i) cin>>x[i]>>y[i]>>r[i]>>v[i];
for(int i=1;i<=n;++i) chk(U,i),chk(V,i);
for(int i=1;i<n;++i) for(int j=i+1;j<=n;++j) if((x[j]-x[i])*(ll)(x[j]-x[i])+(y[j]-y[i])*(ll)(y[j]-y[i])<=(r[i]-r[j])*(ll)(r[i]-r[j])) r[i]<r[j]?++cov[i]:++cov[j];
for(int i=1;i<n;++i) for(int j=i+1;j<=n;++j) if(abs(cov[i]-cov[j])<=1){if((x[j]-x[i])*(ll)(x[j]-x[i])+(y[j]-y[i])*(ll)(y[j]-y[i])==(r[i]+r[j])*(ll)(r[i]+r[j])) e.x=r[i]*(ll)x[j]+r[j]*(ll)x[i],e.y=r[i]*(ll)y[j]+r[j]*(ll)y[i],e.m=r[i]+r[j],g=__gcd(__gcd(e.x,e.y),(ll)e.m),e.x/=g,e.y/=g,e.m/=g,add2(e),add(i,e),add(j,e);else if((x[j]-x[i])*(ll)(x[j]-x[i])+(y[j]-y[i])*(ll)(y[j]-y[i])==(r[i]-r[j])*(ll)(r[i]-r[j])) e.x=r[i]*(ll)x[j]-r[j]*(ll)x[i],e.y=r[i]*(ll)y[j]-r[j]*(ll)y[i],e.m=r[i]-r[j],e.m<0&&(e.m=-e.m,e.x=-e.x,e.y=-e.y),g=__gcd(__gcd(e.x,e.y),(ll)e.m),e.x/=g,e.y/=g,e.m/=g,add2(e),add(i,e),add(j,e);}
sort(w+1,w+cnt+1),cnt=unique(w+1,w+cnt+1)-w-1;
for(int i=1;i<=cnt;++i) m[w[i]]=i;
s=m[U],t=m[V];
for(int i=1;i<=n;++i) if(a[i].size()>=2){
sort(a[i].begin(),a[i].end());
int l=unique(a[i].begin(),a[i].end())-a[i].begin();
if(l<2) continue;
for(int j=0;j<l;++j) o[j].id=m[a[i][j]],o[j].a=atan2(a[i][j].y/(ld)a[i][j].m-y[i],a[i][j].x/(ld)a[i][j].m-x[i]);
sort(o,o+l);
o[l].id=o[0].id,o[l].a=o[0].a+2*pi;
for(int j=0;j<l;++j) link(o[j].id,o[j+1].id,(o[j+1].a-o[j].a)*r[i]*v[i]);
}
q.push((Node){s,0});
while(!q.empty()&&!vst[t]){
Node f=q.top();
q.pop();
if(vst[f.f]) continue;
vst[f.f]=1,d[f.f]=f.e;
for(Node &x:b[f.f]) if(!vst[x.f]) q.push((Node){x.f,f.e+x.e});
}
if(!vst[t]) cout<<"-1";
else cout<<fixed<<setprecision(15)<<d[t];
return 0;
}
```
:::