0-1 BFS
To solve this problem, we modify the standard BFS algorithm using deques (
deque
): if the considered edge has a weight of 0, then we will add a vertex to the beginning, otherwise to the end.
Thus, at the beginning of the deque there will always be a vertex, the distance to which is less than or equal to the distance to the other vertices of the deque, and the requirement to arrange elements in the deque in non-decreasing order is preserved.
For the implementation of the
0-1 BFS
algorithm, see the problem itself.