ContinueLib

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Continue765/ContinueLib

:heavy_check_mark: bfs_01
(graph/shortest_path/bfs_01.hpp)

bfs_01

定义于头文件 bfs_01.hpp

template <typename T, bool dir>
pair<vector<T>, vector<int>> bfs_01(const Graph<T, dir>& g, int s);  // (1)

template <typename T, bool dir>
tuple<vector<T>, vector<int>, vector<int>> bfs_01(const Graph<T, dir>& g, vector<int>& s);  // (2)

求边权为 $0$ 或 $1$ 时的最短路。第一个重载返回 {dist, pre},第二个重载返回 {dist, pre, root}。

参数

参数 说明
g 有向或无向图,边权为 $0$ 或 $1$
s 源点;第二个重载为源点集合

返回值

返回 {dist, pre} 或 {dist, pre, root}:dist[v] 为到最近源点的距离,不可达为 inf<T>;pre[v] 为最短路树上 v 的前驱,v 为源点或不可达时为 -1;root[v] 为 v 所属的最近源点,不可达时为 -1。

复杂度

$O(n + m)$。

示例

Digraph<int> g(n);
g.add(0, 1, 0);
g.add(1, 2, 1);

auto [dist, pre] = bfs_01(g, 0);

vector<int> s = {0, 2};
auto [dist2, pre2, root] = bfs_01(g, s);

Depends on

Verified with

Code

#pragma once
#include <graph/graph.hpp>

template <typename T, bool dir>
pair<vector<T>, vector<int>> bfs_01(const Graph<T, dir>& g, int s) {
    int n = g.n;
    vector<T> dist(n, inf<T>);
    vector<int> pre(n, -1);
    deque<int> que;

    dist[s] = 0;
    que.push_front(s);

    while (!que.empty()) {
        auto u = que.front();
        que.pop_front();
        for (auto [v, id] : g[u]) {
            T w = g.edges[id].w;
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                pre[v] = u;
                if (w == 0) {
                    que.push_front(v);
                } else {
                    que.push_back(v);
                }
            }
        }
    }

    return {dist, pre};
}

template <typename T, bool dir>
tuple<vector<T>, vector<int>, vector<int>> bfs_01(const Graph<T, dir>& g, vector<int>& s) {
    int n = g.n;
    vector<T> dist(n, inf<T>);
    vector<int> pre(n, -1), root(n, -1);
    deque<int> que;

    for (auto& u : s) {
        dist[u] = 0;
        root[u] = u;
        que.push_front(u);
    }

    while (!que.empty()) {
        auto u = que.front();
        que.pop_front();
        for (auto [v, id] : g[u]) {
            T w = g.edges[id].w;
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                pre[v] = u;
                root[v] = root[u];
                if (w == 0) {
                    que.push_front(v);
                } else {
                    que.push_back(v);
                }
            }
        }
    }

    return {dist, pre, root};
}
Back to top page