Problem p00299 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

template <typename T> vector<T> bellman_ford(Edges<T> &edges, int V, int s) {
  const auto INF = numeric_limits<T>::max();
  vector<T> dist(V, INF);
  dist[s] = 0;
  for (int i = 0; i < V - 1; i++) {
    for (auto &e : edges) {
      if (dist[e.src] == INF)
        continue;
      dist[e.to] = min(dist[e.to], dist[e.src] + e.cost);
    }
  }
  for (auto &e : edges) {
    if (dist[e.src] == INF)
      continue;
    if (dist[e.src] + e.cost < dist[e.to])
      return vector<T>();
  }
  return dist;
}

int main() {
  int N, C;
  int A[200], O[200], B[200], S[200], D[200];

  cin >> N >> C;
  vector<int> undefined;
  for (int i = 0; i < C; i++) {
    string str;
    cin >> str;
    int tail = 0, a = 0, b = 0, o, s, d = 0;
    while (isdigit(str[tail]))
      a = a * 10 + str[tail++] - '0';
    if (str[tail] == '*')
      o = 2;
    else if (str[tail] == '<')
      o = 0, ++tail;
    else
      o = 1, ++tail;
    ++tail;
    while (isdigit(str[tail]))
      b = b * 10 + str[tail++] - '0';
    if (str[tail] == '+')
      s = 0;
    else
      s = 1;
    ++tail;
    while (tail < str.size())
      d = d * 10 + str[tail++] - '0';
    A[i] = --a, B[i] = --b, O[i] = o, S[i] = s, D[i] = d;
    if (o == 2)
      undefined.push_back(i);
  }
  int ret = -1;
  for (int i = 0; i < (1 << undefined.size()); i++) {
    for (int j = 0; j < undefined.size(); j++)
      O[undefined[j]] = (i >> j) & 1;

    Edges<int> es;
    for (int k = 0; k < C; k++) {
      if (O[k] == 1)
        swap(A[k], B[k]);
      es.emplace_back(B[k], A[k], 0);
      if (S[k] == 0)
        es.emplace_back(B[k], A[k], -D[k]);
      else
        es.emplace_back(A[k], B[k], D[k]);
      if (O[k] == 1)
        swap(A[k], B[k]);
    }
    auto dists = bellman_ford(es, N, 0);
    ret = max(ret, *max_element(begin(dists), end(dists)));
  }

  if (ret >= numeric_limits<int>::max())
    cout << "inf" << endl;
  else
    cout << ret << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

template <typename T> vector<T> bellman_ford(Edges<T> &edges, int V, int s) {
  const auto INF = numeric_limits<T>::max();
  vector<T> dist(V, INF);
  dist[s] = 0;
  for (int i = 0; i < V - 1; i++) {
    for (auto &e : edges) {
      if (dist[e.src] == INF)
        continue;
      dist[e.to] = min(dist[e.to], dist[e.src] + e.cost);
    }
  }
  for (auto &e : edges) {
    if (dist[e.src] == INF)
      continue;
    if (dist[e.src] + e.cost < dist[e.to])
      return vector<T>();
  }
  return dist;
}

int main() {
  int N, C;
  int A[200], O[200], B[200], S[200], D[200];

  cin >> N >> C;
  vector<int> undefined;
  for (int i = 0; i < C; i++) {
    string str;
    cin >> str;
    int tail = 0, a = 0, b = 0, o, s, d = 0;
    while (isdigit(str[tail]))
      a = a * 10 + str[tail++] - '0';
    if (str[tail] == '*')
      o = 2;
    else if (str[tail] == '<')
      o = 0, ++tail;
    else
      o = 1, ++tail;
    ++tail;
    while (isdigit(str[tail]))
      b = b * 10 + str[tail++] - '0';
    if (str[tail] == '+')
      s = 0;
    else
      s = 1;
    ++tail;
    while (tail < str.size())
      d = d * 10 + str[tail++] - '0';
    A[i] = --a, B[i] = --b, O[i] = o, S[i] = s, D[i] = d;
    if (o == 2)
      undefined.push_back(i);
  }
  int ret = -1;
  for (int i = 0; i < (1 << undefined.size()); i++) {
    for (int j = 0; j < undefined.size(); j++)
      O[undefined[j]] = (i >> j) & 1;

    Edges<int> es;
    for (int k = 0; k < C; k++) {
      if (O[k] == 1)
        swap(A[k], B[k]);
      es.emplace_back(B[k], A[k], 0);
      if (S[k] == 0)
        es.emplace_back(B[k], A[k], -D[k]);
      else
        es.emplace_back(A[k], B[k], D[k]);
      if (O[k] == 1)
        swap(A[k], B[k]);
    }
    auto dists = bellman_ford(es, N, 0);
    if (dists.empty() || *min_element(begin(dists), end(dists)) < 0)
      continue;
    ret = max(ret, *max_element(begin(dists), end(dists)));
  }

  if (ret >= numeric_limits<int>::max())
    cout << "inf" << endl;
  else
    cout << ret << endl;
}