TOP > グラフ 単一始点最短路
単一始点最短路(SPFA)
説明
単一始点全点間最短路を求めるアルゴリズム。単一始点全点間最短路を求めるアルゴリズム。負辺があっても動作する。また負閉路も検出する。
計算量
- $O(VE)$
実用上はBellman-Fordよりも高速に動作する。
実装例
- shortest_path_faster_algorithm($g$, $s$):= 重み付きグラフ $g$ 上で、頂点 $s$ から全点間の最短コストを求める。到達できないとき, 型の最大値が格納される。負閉路を検出した時空列を返す。
template< typename T >
vector< T > shortest_path_faster_algorithm(WeightedGraph< T > &g, int s) {
const auto INF = numeric_limits< T >::max();
vector< T > dist(g.size(), INF);
vector< int > pending(g.size(), 0), times(g.size(), 0);
queue< int > que;
que.emplace(s);
pending[s] = true;
++times[s];
dist[s] = 0;
while(!que.empty()) {
int p = que.front();
que.pop();
pending[p] = false;
for(auto &e : g[p]) {
T next_cost = dist[p] + e.cost;
if(next_cost >= dist[e.to]) continue;
dist[e.to] = next_cost;
if(!pending[e.to]) {
if(++times[e.to] >= g.size()) return vector< T >();
pending[e.to] = true;
que.emplace(e.to);
}
}
}
return dist;
}
検証
AOJ gRL_1_B 単一始点最短経路(負の重みをもつ辺を含む
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B"
#include "../../template/template.cpp"
#include "../template.cpp"
#include "../shortest-path-faster-algorithm.cpp"
int main() {
int V, E, R;
scanf("%d %d %d", &V, &E, &R);
WeightedGraph< int > g(V);
for(int i = 0; i < E; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
g[a].emplace_back(b, c);
}
auto dists = shortest_path_faster_algorithm(g, R);
if(dists.empty()) puts("NEGATIVE CYCLE");
for(auto &dist : dists) {
if(dist == numeric_limits< int >::max()) puts("INF");
else printf("%d\n", dist);
}
}