serna37's Library

Logo

C++ アルゴリズムとデータ構造のライブラリ

View the Project on GitHub serna37/library-cpp

:heavy_check_mark: Bellman Ford
(library/graph/shortest_path/bellman_ford.hpp)

Bellman Ford

できること

計算量

$O(VE)$

使い方

auto [dis, negativeCycle, route] = bellman_ford(G, s);

Depends on

Verified with

Code

#pragma once
#include "library/graph/base/graph.hpp"
tuple<vector<long long>, bool, vector<int>> bellman_ford(const Graph &G,
                                                         int s = 0) {
    int N = G.size(), loop = 0;
    vector<long long> dis(N, INF);
    vector<int> route(N, -1);
    dis[s] = 0;
    while (1) {
        ++loop;
        bool upd = 0;
        for (int v = 0; v < N; ++v) {
            if (dis[v] == INF) continue;
            for (auto &&[from, to, cost, idx] : G[v]) {
                long long asis = dis[to], tobe = dis[v] + cost;
                if (dis[v] == -INF) tobe = -INF;
                tobe = max(tobe, -INF);
                if (asis <= tobe) continue;
                if (loop >= N) tobe = -INF;
                dis[to] = tobe;
                route[to] = v;
                upd = 1;
            }
        }
        if (!upd) break;
    }
    bool negativeCycle = false;
    for (auto &&v : dis) {
        if (v == -INF) {
            negativeCycle = true;
            break;
        }
    }
    return {dis, negativeCycle, route};
}
#line 2 "library/graph/base/edge.hpp"
struct Edge {
    int from, to;
    long long cost;
    int idx;
    Edge(int from, int to, long long cost = 1, int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
};
#line 3 "library/graph/base/graph.hpp"
struct Graph {
    int N;
    vector<vector<Edge>> G;
    int es;
    Graph() = default;
    Graph(int N) : N(N), G(N), es(0) {}
    const vector<Edge> &operator[](int v) const { return G[v]; }
    int size() const { return N; }
    void add(int from, int to, long long cost = 1) {
        G[from].push_back(Edge(from, to, cost, es++));
    }
    void add_both(int from, int to, long long cost = 1) {
        G[from].push_back(Edge(from, to, cost, es));
        G[to].push_back(Edge(to, from, cost, es++));
    }
    void read(int M, int padding = -1, bool weighted = false,
              bool directed = false) {
        for (int i = 0; i < M; i++) {
            int u, v;
            cin >> u >> v;
            u += padding, v += padding;
            long long cost = 1ll;
            if (weighted) cin >> cost;
            if (directed) {
                add(u, v, cost);
            } else {
                add_both(u, v, cost);
            }
        }
    }
};
#line 3 "library/graph/shortest_path/bellman_ford.hpp"
tuple<vector<long long>, bool, vector<int>> bellman_ford(const Graph &G,
                                                         int s = 0) {
    int N = G.size(), loop = 0;
    vector<long long> dis(N, INF);
    vector<int> route(N, -1);
    dis[s] = 0;
    while (1) {
        ++loop;
        bool upd = 0;
        for (int v = 0; v < N; ++v) {
            if (dis[v] == INF) continue;
            for (auto &&[from, to, cost, idx] : G[v]) {
                long long asis = dis[to], tobe = dis[v] + cost;
                if (dis[v] == -INF) tobe = -INF;
                tobe = max(tobe, -INF);
                if (asis <= tobe) continue;
                if (loop >= N) tobe = -INF;
                dis[to] = tobe;
                route[to] = v;
                upd = 1;
            }
        }
        if (!upd) break;
    }
    bool negativeCycle = false;
    for (auto &&v : dis) {
        if (v == -INF) {
            negativeCycle = true;
            break;
        }
    }
    return {dis, negativeCycle, route};
}
Back to top page