在工单被骂复杂度错误骂无语了所以来发篇题解。
首先说明一下本做法时间复杂度为 ,其中 。 是因为计算 。不需要哈希。
考虑把所有交点和 作为顶点建图,在同一个圆上相邻的两个点连边,边权为走过这段弧的耗时。
这样只要建出这幅图然后在上面跑 到 的最短路就好了。
然后容易发现如果直接把圆两两间的切点都存起来那么空间会炸到 ,时间会炸到 ,显然是过不了的。
那么我们考虑先 计算出每个圆被其他圆共覆盖了多少次,这是容易计算的(因为保证任意两圆要么相切要么无交点)。记第 个圆被覆盖了 次。
为了方便,下面记 为第 个圆。
考虑在判断交点时,只判断所有满足 的圆的交点。我们来证明这一点。
- 若在此交点处 与另一个圆内切,则必有与 半径最接近且与 在此处内切的圆 ,则满足 ,则该交点会被放入 的集合内;
- 否则在此交点处 与一些圆外切,且不与任何圆在此内切,则取所有与 在此外切的圆中半径最大的圆 ,则满足 。
于是这种做法不会漏掉任何交点或在任何圆上漏掉其经过的交点。
接下来我们归纳证明, 个两两相切或不相交的圆的所有切点 的经过次数减一之和小于 ,也就是:
。
当 的时候 显然成立。
假设 成立,则 时:
考虑半径最小的圆,不妨设 半径最小。
则显然没有圆在 内部。分类讨论。下面的外切在同切点时只看半径最大的(因为只在数 经过的切点个数)。
- 若存在 ,使得 与 内切,则:
由于 半径最小,故 。而由于在 内部,则所有相邻圆的 之和小于 ,因此至多 个圆与 这样相切,则 经过的切点个数不超过 ; - 否则由于所有与 相切的圆中相邻圆的 之和不超过 ,则 经过的切点个数不超过 。
于是根据归纳假设,除了 外其他圆的 ,则 ,得证。
于是切点的个数不超过 ,且所有圆经过的切点个数之和不超过 ,也就是小于 (别问我为什么 时 )。
然后我们再证明,对于固定的 和一个切点 ,满足 且 与 相切于 的不同 个数不超过 。
如图,与圆 在 处相切且满足对 的限制的圆分别为 个紫色的圆 。
证明很容易,内切至多 个,外切至多 个,故总共至多 个。
于是该做法至多判断条件成立 次,复杂度正确。
时间复杂度在开头写了,为 ,其中 。
不要再说这题没正解只能靠卡常了。
AC 记录
AC Code:
#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;
}