跳到正文

个人记录

SPFA又被我救活了

pengyulun约 6 分钟
原文

这是 2025年10月5日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年2月21日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

书接上回

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define getchar_fread() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++)
#define flush() (fwrite(buffer,1,q1+1,stdout),q1=-1)
const int MAXN=1<<15,N=1e6+8,M=3e6+8;
const ll C=0x3f3f3f3f3f3f3f3f;
char *p1,*p2,buf[MAXN],buffer[MAXN];
int q1=-1,q2=MAXN-1,j=0;
inline void putchar_fwrite(char x){if (q1==q2) flush();buffer[++q1]=x;}
template<typename T>
inline void read(T &x){x=0;
    T c=getchar_fread(),f=0;
    while(!isdigit(c)){if(c=='-') f=1;c=(T)getchar_fread();}
    while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=(T)getchar_fread();
    if (f) x=~x+1;
}
template<typename T>
inline void write(T x){
	if (x<0) putchar_fwrite('-'),x=~x+1;
	T print[65];
	while (x) print[++j]=x%10,x/=10;
	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;
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];
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){
        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[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;}
};
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(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();
		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});
			}
		}
	}
	memset(w,0,sizeof(w));
	l=0,tarjan(s);
	memset(w,0,sizeof(w));
	w[s]=c[s]=1;
}
struct spf{int x,f;};
inline bool spfa(int s){
    queue <spf> sta;
    sta.push({s,1});init(s);
    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];
        }
    }return 1;
}
signed main(){
    int s;
	read(n),read(m),read(s);
	for (int i=1,x,y,z;i<=m;++i){
	    read(x),read(y),read(z);
	    if (x==y) continue;
	    a[++cnt]={y,h[x],0,z};h[x]=cnt;
	}
	if (!spfa(s)) return cout <<"No answer.",0;
	for (int i=1;i<=n;++i) write(ans[i]),putchar_fwrite(' ');
	flush();
	return 0;
}
//拓扑排序与最小生成树思想优化spfa