library

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

View the Project on GitHub matumoto1234/library

:x: test/aoj/grl/1_B.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/1/GRL_1_B"

#include <bits/stdc++.h>
using namespace std;

#include "graph/bellman-ford.hpp"
using namespace matumoto;

// {{{ Templates

// clang-format off

// Macros
#define over_load_(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) over_load_(__VA_ARGS__, rep4, rep3, rep2)(__VA_ARGS__)
#define rep2(i, r) for ( int i = 0; i < static_cast<int>(r); (i) += 1)
#define rep3(i, l, r) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += 1)
#define rep4(i, l, r, stride) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += (stride))
#define rrep(...) over_load_(__VA_ARGS__, rrep4, rrep3, rrep2)(__VA_ARGS__)
#define rrep2(i, r) for ( int i = static_cast<int>(r) - 1; i >= 0; (i) -= 1)
#define rrep3(i, l, r) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= 1)
#define rrep4(i, l, r, stride) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= (stride))
#define len(x) (static_cast<int>((x).size()))
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(...) debug_function(#__VA_ARGS__, __VA_ARGS__)

// Operators
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { bool is_first = true; for (auto x: v) { os << (is_first ? "" : " ") << x; is_first = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> v) { bool is_first = true; while (!v.empty()) { os << (is_first?"":" ")<<v.front(); v.pop(); is_first = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> v) { bool is_first = true; while (!v.empty()) { os << (is_first?"":" ") << v.top(); v.pop(); is_first=false; } return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? "" : " "); return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { for (const auto &vec: v) { os << vec << '\n'; } return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? "" : " "); return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { bool is_first = true; for (T x: v) { os << (is_first ? "" : " ") << x; is_first = false; } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in: v) { is >> in; } return is; }

// For debug macro
int find_comma_not_bracketed(string_view s){ stack<char> bs; string lbs = "({[", rbs = ")}]"; for (size_t i = 0; i < s.size(); i++) { if (lbs.find(s[i]) != string::npos) bs.push(s[i]); if (rbs.find(s[i]) != string::npos and !bs.empty()) bs.pop(); if (s[i] == ',' and bs.empty()) return i; } return s.size(); }
template <typename T, typename... Ts> void debug_function(string_view name, const T &a, Ts &&...rest) { int end = find_comma_not_bracketed(name); cerr << name.substr(0, end) << ":" << a; if constexpr (sizeof...(rest) == 0) { cerr << '\n'; } else { cerr << ' '; debug_function(name.substr(name.find_first_not_of(' ', end + 1)), forward<Ts>(rest)...); } }

// Functions
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b and (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b and (a = b, true); }

// Structs
struct IoSetup { IoSetup(int x = 15) { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

// Type aliases
using ull = unsigned long long;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;

// Literals
constexpr ll INF64 = INT64_MAX / 2;
constexpr int INF32 = INT32_MAX / 2;
constexpr int dy[] = { 0, 1, -1, 0, -1, 1, -1, 1 };
constexpr int dx[] = { 1, 0, 0, -1, -1, -1, 1, 1 };
constexpr int mod998244353 = 998244353;
constexpr int mod1000000007 = static_cast<int>(1e9) + 7;
constexpr char newl = '\n';

// clang-format on

// }}} Templates



int main() {
  int V, E, r;
  cin >> V >> E >> r;

  using Edge = WeightedEdge<ll>;
  WeightedGraph<ll> G(V);
  rep(i, E) {
    int s, t, d;
    cin >> s >> t >> d;

    G.add_edge(Edge(s, t, d));
  }

  BellmanFord<ll> bf(G, r);

  if (bf.has_negative_cycle()) {
    cout << "NEGATIVE CYCLE" << newl;
    return 0;
  }

  rep(i, V) {
    if (bf[i] == BellmanFord<ll>::inf()) {
      cout << "INF" << newl;
      continue;
    }
    cout << bf[i] << newl;
  }
}
#line 1 "test/aoj/grl/1_B.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/1/GRL_1_B"

#include <bits/stdc++.h>
using namespace std;

#line 2 "graph/bellman-ford.hpp"

#line 2 "graph/base.hpp"

namespace matumoto {
  using namespace std;
}
#line 2 "graph/adjacency-list-to-edges.hpp"

#line 5 "graph/adjacency-list-to-edges.hpp"

namespace matumoto {
  vector<pair<int, int>> adjacency_list_to_edges(const vector<vector<int>> &adj_list) {
    vector<pair<int, int>> edges;
    for (int v = 0; v < (int)adj_list.size(); v++) {
      for (const auto &to: adj_list[v]) {
        edges.emplace_back(v, to);
      }
    }
    return edges;
  }

  template <typename Cost>
  vector<tuple<int, int, Cost>> adjacency_list_to_edges(const vector<vector<pair<Cost, int>>> &adj_list) {
    vector<tuple<int, int, Cost>> edges;
    for (int v = 0; v < (int)adj_list.size(); v++) {
      for (const auto &[cost, to]: adj_list[v]) {
        edges.emplace_back(v, to, cost);
      }
    }
    return edges;
  }
} // namespace matumoto
#line 2 "tools/assert-msg.hpp"

#line 2 "tools/base.hpp"

namespace matumoto {
  using namespace std;
}
#line 4 "tools/assert-msg.hpp"

#line 8 "tools/assert-msg.hpp"

namespace matumoto {
#define assert_msg(expr, msg) (static_cast<bool>(expr) ? void(0) : assert_fail(__FILE__, __LINE__, #expr, msg))

  namespace {
    void assert_fail(const char *file, int line, const char expr_str[], string msg) {
      cerr << "File: " << file << "\n"
           << "Line: " << line << "\n"
           << "Assertion '" << expr_str << "' failed.\n"
           << "Message: " << msg << "\n";
      abort();
    }
  } // namespace
} // namespace matumoto
#line 6 "graph/bellman-ford.hpp"

#line 11 "graph/bellman-ford.hpp"

namespace matumoto {
  template <typename Cost>
  class BellmanFord {
    // adjacency list
    vector<vector<pair<Cost, int>>> adj_list_;
    vector<tuple<int, int, Cost>> edges_;
    vector<Cost> dists_;
    vector<int> prevs_;
    int start_, goal_;
    bool has_neg_cycle_, has_neg_cycle_to_goal_;

  public:
    BellmanFord(const vector<vector<pair<Cost, int>>> &adj_list, int start, int goal = -1)
      : adj_list_(adj_list),
        dists_(adj_list_.size(), inf()),
        prevs_(adj_list_.size(), -1),
        start_(start),
        goal_(goal),
        has_neg_cycle_(false),
        has_neg_cycle_to_goal_(false) {
      int n = adj_list_.size();
      if (goal_ == -1) {
        goal_ = n - 1;
      }
      assert_msg(0 <= start and start < n, "start:" + to_string(start) + " n:" + to_string(n));
      assert_msg(0 <= goal and goal < n, "goal:" + to_string(goal) + " n:" + to_string(n));

      edges_ = adjacency_list_to_edges(adj_list_);

      dists_[start] = 0;

      for (int i = 0; i < 2 * n; i++) {
        for (const auto &[from, to, cost]: edges_) {
          assert_msg(0 <= from and from < n, "from:" + to_string(from) + " n:" + to_string(n));
          assert_msg(0 <= to and to < n, "to:" + to_string(to) + " n:" + to_string(n));

          if (dists_[from] >= inf() or dists_[from] + cost >= dists_[to]) {
            continue;
          }

          if (i >= n - 1 and to == goal) {
            has_neg_cycle_ = true;
            has_neg_cycle_to_goal_ = true;
            return;
          } else if (i >= n - 1) {
            has_neg_cycle_ = true;
            dists_[to] = -inf();
          } else {
            dists_[to] = dists_[from] + cost;
            prevs_[to] = from;
          }
        }
      }
    }

    static constexpr Cost inf() {
      return numeric_limits<Cost>::max() / 2;
    }

    bool has_negative_cycle() const {
      return has_neg_cycle_;
    }

    bool has_negative_cycle_to_goal() const {
      return has_neg_cycle_to_goal_;
    }

    Cost &operator[](int k) {
      assert_msg(0 <= k and k < (int)dists_.size(), "k:" + to_string(k) + " size:" + to_string(dists_.size()));
      return dists_[k];
    }

    vector<int> restore(int to) {
      assert_msg(0 <= to and to < (int)dists_.size(), "to:" + to_string(to) + " size:" + to_string(prevs_.size()));
      vector<int> path;
      if (prevs_[to] == -1) {
        path.emplace_back(to);
        return path;
      }

      while (prevs_[to] != -1) {
        path.emplace_back(to);
        to = prevs_.at(to);
      }
      reverse(path.begin(), path.end());
      return path;
    }
  };
} // namespace matumoto
#line 7 "test/aoj/grl/1_B.test.cpp"
using namespace matumoto;

// {{{ Templates

// clang-format off

// Macros
#define over_load_(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) over_load_(__VA_ARGS__, rep4, rep3, rep2)(__VA_ARGS__)
#define rep2(i, r) for ( int i = 0; i < static_cast<int>(r); (i) += 1)
#define rep3(i, l, r) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += 1)
#define rep4(i, l, r, stride) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += (stride))
#define rrep(...) over_load_(__VA_ARGS__, rrep4, rrep3, rrep2)(__VA_ARGS__)
#define rrep2(i, r) for ( int i = static_cast<int>(r) - 1; i >= 0; (i) -= 1)
#define rrep3(i, l, r) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= 1)
#define rrep4(i, l, r, stride) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= (stride))
#define len(x) (static_cast<int>((x).size()))
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(...) debug_function(#__VA_ARGS__, __VA_ARGS__)

// Operators
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { bool is_first = true; for (auto x: v) { os << (is_first ? "" : " ") << x; is_first = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> v) { bool is_first = true; while (!v.empty()) { os << (is_first?"":" ")<<v.front(); v.pop(); is_first = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> v) { bool is_first = true; while (!v.empty()) { os << (is_first?"":" ") << v.top(); v.pop(); is_first=false; } return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? "" : " "); return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { for (const auto &vec: v) { os << vec << '\n'; } return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? "" : " "); return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { bool is_first = true; for (T x: v) { os << (is_first ? "" : " ") << x; is_first = false; } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in: v) { is >> in; } return is; }

// For debug macro
int find_comma_not_bracketed(string_view s){ stack<char> bs; string lbs = "({[", rbs = ")}]"; for (size_t i = 0; i < s.size(); i++) { if (lbs.find(s[i]) != string::npos) bs.push(s[i]); if (rbs.find(s[i]) != string::npos and !bs.empty()) bs.pop(); if (s[i] == ',' and bs.empty()) return i; } return s.size(); }
template <typename T, typename... Ts> void debug_function(string_view name, const T &a, Ts &&...rest) { int end = find_comma_not_bracketed(name); cerr << name.substr(0, end) << ":" << a; if constexpr (sizeof...(rest) == 0) { cerr << '\n'; } else { cerr << ' '; debug_function(name.substr(name.find_first_not_of(' ', end + 1)), forward<Ts>(rest)...); } }

// Functions
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b and (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b and (a = b, true); }

// Structs
struct IoSetup { IoSetup(int x = 15) { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

// Type aliases
using ull = unsigned long long;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;

// Literals
constexpr ll INF64 = INT64_MAX / 2;
constexpr int INF32 = INT32_MAX / 2;
constexpr int dy[] = { 0, 1, -1, 0, -1, 1, -1, 1 };
constexpr int dx[] = { 1, 0, 0, -1, -1, -1, 1, 1 };
constexpr int mod998244353 = 998244353;
constexpr int mod1000000007 = static_cast<int>(1e9) + 7;
constexpr char newl = '\n';

// clang-format on

// }}} Templates



int main() {
  int V, E, r;
  cin >> V >> E >> r;

  using Edge = WeightedEdge<ll>;
  WeightedGraph<ll> G(V);
  rep(i, E) {
    int s, t, d;
    cin >> s >> t >> d;

    G.add_edge(Edge(s, t, d));
  }

  BellmanFord<ll> bf(G, r);

  if (bf.has_negative_cycle()) {
    cout << "NEGATIVE CYCLE" << newl;
    return 0;
  }

  rep(i, V) {
    if (bf[i] == BellmanFord<ll>::inf()) {
      cout << "INF" << newl;
      continue;
    }
    cout << bf[i] << newl;
  }
}
Back to top page