Spfa模板

#include <queue>
#include <memory.h>
using namespace std;
struct Edge {
    int u,v,w,nx;
}g[M];
int cnt,list[N];
int d[N];
bool b[N];

void Spfa(int v0) {
    queue<int> q;
    while (!q.empty()) q.pop();
    q.push(v0);
    memset(d,0x3f,sizeof d);memset(b,0,sizeof b);
    b[v0]=1;d[v0]=0;
    while (!q.empty()) {
        int u=q.front();q.pop();
        for (int i=list[u];i;i=g[i].nx)
        if (d[u]+g[i].w<d[g[i].v]) {
            d[g[i].v]=d[u]+g[i].w;
            if (!b[g[i].v]) q.push(g[i].v);
            b[g[i].v]=1;
        }
        b[u]=0;
    }
}
View Code

 

posted @ 2018-08-05 14:44  Vagari  阅读(90)  评论(0编辑  收藏  举报