版本比较
SPFA又被我救活了
正文
增加 33 行删除 28 行
24 行未改动2526272829293031323233343536363738394038394041424344454647484950515253545556575859606162636465665967686970647172736768697071727374757677787980818283798485818683848788899086878889909192939192939495969798991001011028 行未改动11111211311411011511624 行未改动 if (j==0) print[++j]=0; while (j) putchar_fwrite(print[j--]^48);}struct G{int x,t,qq;ll u;} a[M];int n,m,s,h[N],cnt=0;int n,m,h[N],cnt=0;struct Tar{int dfn,low;} g[N];ll ans[N],z;int p[N],c[N],l,stk[N],top=0,w[N],k[N];int p[N],c[N],l,stk[N],top=0,w[N],k[N],t[N],u,v;void tarjan(int x){ g[x].dfn=g[x].low=++l; stk[++top]=x;k[x]=w[x]=1; for (int i=h[x];i;i=a[i].t){ for (int i=h[x];i;i=a[i].t){v=a[i].x; if (a[i].qq==0) continue; if (g[a[i].x].dfn&&k[a[i].x]) --p[a[i].x]; if (!g[a[i].x].dfn) tarjan(a[i].x); if (w[a[i].x]) g[x].low=min(g[x].low,g[a[i].x].low); if (g[v].dfn&&k[v]) --p[v]; if (!g[v].dfn) tarjan(v); if (w[v]) g[x].low=min(g[x].low,g[v].low); }if (g[x].dfn==g[x].low){ int y=0; while (x!=y) y=stk[top--],w[y]=0; }k[x]=0;}struct pri{ int x;ll u; inline bool operator<(const pri&other)const{return u>other.u;}};struct spf{ int x,f,t,dep; inline bool operator<(const spf&other)const{ if (t!=other.t) return t<other.t; return dep>other.dep; }};template <typename T>class Priority_Queue:public priority_queue<T>{ public:void reserve(size_t n){this->c.reserve(n);}};inline void init(int s){ memset(g,0,sizeof(g)); memset(p,0,sizeof(p)); memset(w,0,sizeof(w)); memset(t,0,sizeof(t)); memset(ans,C,sizeof(ans)); memset(c,0,sizeof(c)); Priority_Queue <pri> q; q.reserve(n);ans[s]=0; w[s]=s;q.push({s,0});l=0; while (!q.empty()){ int u=q.top().x;q.pop(); u=q.top().x;q.pop(); if (c[u]) continue; c[u]=1;++l;if (l==n) break; for (int i=h[u];i;i=a[i].t){ if (a[i].x==w[u]) continue; ++p[a[i].x];a[i].qq=1; if (ans[a[i].x]>ans[u]+a[i].u){ ans[a[i].x]=ans[u]+a[i].u,w[a[i].x]=u; q.push({a[i].x,a[i].u}); } for (int i=h[u];i;i=a[i].t){v=a[i].x; if (v==w[u]) continue; ++p[v];a[i].qq=1; if (ans[v]>ans[u]+a[i].u) ans[v]=ans[u]+a[i].u,w[v]=u,q.push({v,a[i].u}); } } memset(w,0,sizeof(w)); l=0,tarjan(s); memset(w,0,sizeof(w)); w[s]=c[s]=1; w[s]=c[s]=t[s]=1;}struct spf{int x,f;};inline bool spfa(int s){ queue <spf> sta; sta.push({s,1});init(s); Priority_Queue <spf> sta; init(s);sta.reserve(n); sta.push({s,1,1,1}); while (!sta.empty()){ spf u=sta.front(); sta.pop();w[u.x]=0; if (c[u.x]>n) return 0; for (int i=h[u.x];i;i=a[i].t){l=0; if (ans[a[i].x]>ans[u.x]+a[i].u) c[a[i].x]=c[u.x]+1,ans[a[i].x]=ans[u.x]+a[i].u,l=1; if (!w[a[i].x]&&(p[a[i].x]==1&&u.f||l&&p[a[i].x]==0)) w[a[i].x]=1,sta.push({a[i].x,p[a[i].x]}); if (u.f&&p[a[i].x]>0) --p[a[i].x]; spf tp=sta.top();u=tp.x; sta.pop();w[u]=0; if (c[u]>n) return 0; for (int i=h[u];i;i=a[i].t){l=0,v=a[i].x; if (ans[v]>ans[u]+a[i].u) c[v]=c[u]+1,ans[v]=ans[u]+a[i].u,l=1; if (!w[v]&&(p[v]==1&&tp.f||l&&p[v]==0)) ++t[v],w[v]=1,sta.push({v,p[v],t[v],c[v]}); if (tp.f&&p[a[i].x]>0) --p[a[i].x]; } }return 1;}signed main(){8 行未改动 for (int i=1;i<=n;++i) write(ans[i]),putchar_fwrite(' '); flush(); return 0;}//拓扑排序与最小生成树思想优化spfa//拓扑排序、最小生成树思想与加速路径传播优化spfa```