This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ruthen71/rcpl
#include "graph/bellman_ford.hpp"
INF
-INF
s
auto [dist, par, root] = bellman_ford(G, s, INF);
#pragma once #include "graph/graph_template.hpp" template <class T> std::tuple<std::vector<T>, std::vector<int>, std::vector<int>> // bellman_ford(Graph<T> &G, std::vector<int> &s, const T INF) { int N = (int)G.size(); std::vector<T> dist(N, INF); std::vector<int> par(N, -1), root(N, -1); for (auto &v : s) { dist[v] = 0; root[v] = v; } int loop_count = 0; while (true) { loop_count++; bool update = false; for (int cur = 0; cur < N; cur++) { if (dist[cur] == INF) continue; for (auto &e : G[cur]) { T nd = std::max(-INF, dist[cur] + e.cost); if (dist[e.to] > nd) { par[e.to] = cur; root[e.to] = root[cur]; update = true; if (loop_count >= N) nd = -INF; dist[e.to] = nd; } } } if (!update) break; } return {dist, par, root}; }
#line 2 "graph/bellman_ford.hpp" #line 2 "graph/graph_template.hpp" #include <vector> template <class T> struct Edge { int from, to; T cost; int id; Edge() = default; Edge(int from, int to, T cost = 1, int id = -1) : from(from), to(to), cost(cost), id(id) {} friend std::ostream &operator<<(std::ostream &os, const Edge<T> &e) { // output format: "{ id : from -> to, cost }" return os << "{ " << e.id << " : " << e.from << " -> " << e.to << ", " << e.cost << " }"; } }; template <class T> using Edges = std::vector<Edge<T>>; template <class T> using Graph = std::vector<std::vector<Edge<T>>>; #line 4 "graph/bellman_ford.hpp" template <class T> std::tuple<std::vector<T>, std::vector<int>, std::vector<int>> // bellman_ford(Graph<T> &G, std::vector<int> &s, const T INF) { int N = (int)G.size(); std::vector<T> dist(N, INF); std::vector<int> par(N, -1), root(N, -1); for (auto &v : s) { dist[v] = 0; root[v] = v; } int loop_count = 0; while (true) { loop_count++; bool update = false; for (int cur = 0; cur < N; cur++) { if (dist[cur] == INF) continue; for (auto &e : G[cur]) { T nd = std::max(-INF, dist[cur] + e.cost); if (dist[e.to] > nd) { par[e.to] = cur; root[e.to] = root[cur]; update = true; if (loop_count >= N) nd = -INF; dist[e.to] = nd; } } } if (!update) break; } return {dist, par, root}; }