SPFA
bool SPFA() { memset(inq, false, sizeof(inq)); for(int i = 0; i <= n; i++) d[i] = INF; d[0] = 0; queue <int> Q; Q.push(0); inq[0] = true; while(!Q.empty()) { int u = Q.front(); Q.pop(); inq[u] = false; for(int i = 0; i < G[u].size(); i++) { int v = G[u][i].v; int w = G[u][i].w; if(d[v] < d[u] + w) { d[v] = d[u] + w; if(!inq[v]) { inq[v] = true; Q.push(v); } } } } }
原文:http://blog.csdn.net/u011686226/article/details/24180227