This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub morioprog/cpplib
#include "graph/shortestpath/01_bfs.hpp"
単一始点最短路. 辺の重みが$0$または$1$のみである必要がある.
$O(E + V)$
bfs_01(g, frm)
frm
vector<T>
/** * @brief 01-BFS * @docs docs/graph/shortestpath/01_bfs.md */ template <typename T> vector<T> bfs_01(const Graph<T>& g, int frm) { vector<T> ret(g.V, GINF<T>); ret[frm] = 0; deque<int> dq; dq.emplace_back(frm); while (not dq.empty()) { int idx = dq.front(); dq.pop_front(); for (auto& e : g.mat[idx]) { T nxt_cst = ret[idx] + e.cst; if (ret[e.to] <= nxt_cst) continue; ret[e.to] = nxt_cst; if (e.cst == 0) dq.emplace_front(e.to); else dq.emplace_back(e.to); } } return ret; }
#line 1 "graph/shortestpath/01_bfs.hpp" /** * @brief 01-BFS * @docs docs/graph/shortestpath/01_bfs.md */ template <typename T> vector<T> bfs_01(const Graph<T>& g, int frm) { vector<T> ret(g.V, GINF<T>); ret[frm] = 0; deque<int> dq; dq.emplace_back(frm); while (not dq.empty()) { int idx = dq.front(); dq.pop_front(); for (auto& e : g.mat[idx]) { T nxt_cst = ret[idx] + e.cst; if (ret[e.to] <= nxt_cst) continue; ret[e.to] = nxt_cst; if (e.cst == 0) dq.emplace_front(e.to); else dq.emplace_back(e.to); } } return ret; }