【模板】SPFA
作者:互联网
#include<iostream> #include<sstream> #include<cstring> #include<cstdio> #include<queue> using namespace std; const long long INF=2147483647; int s,N,n,last[500005],dis[500005],vis[500005]; struct node{ int to,val,next; }a[500005]; int add(int U,int V,int Edge){ a[++n].to=V; a[n].val=Edge; a[n].next=last[U]; last[U]=n; return 0; } int SPFA(){ queue<int> q; q.push(s); for(int i=1;i<=N;i++) dis[i]=INF; vis[s]=1; dis[s]=0; while(!q.empty()){ int u=q.front(); q.pop(); vis[u]=0; for(int i=last[u];i;i=a[i].next){ int v=a[i].to; if(dis[u]==INF||a[i].val==INF) continue; if(dis[v]>dis[u]+a[i].val){ dis[v]=dis[u]+a[i].val; if(vis[v]==1) continue; vis[v]=1; q.push(v); } } } return 0; } int main(){ int m; memset(dis,INF,sizeof(dis)); memset(vis,0,sizeof(vis)); scanf("%d%d%d",&N,&m,&s); for(int i=1;i<=m;i++){ int u,v,edge; scanf("%d%d%d",&u,&v,&edge); add(u,v,edge); } SPFA(); for(int i=1;i<=N;i++) printf("%d ",dis[i]); return 0; }
标签:val,int,vis,SPFA,include,500005,模板,dis 来源: https://www.cnblogs.com/latent-Lin/p/13927490.html