Problem p02949 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <limits>
#include <vector>
#include <unordered_set>

using namespace std;

struct arc {
  int to, cost;
};

struct node {
  int value;
  vector<arc> arcs;
  node(int v) : value(v) {}
};

class graph {
protected:
  vector<node> nodes;

public:
  graph(int N, int value) : nodes(N, value){};

  void addArc(int from, int to, int cost) {
    nodes[from].arcs.push_back({to, cost});
  }
  void addEdge(int a, int b, int cost) {
    addArc(a, b, cost);
    addArc(b, a, cost);
  }
  node &operator[](int index) { return nodes[index]; }
};

class bellmanford : public graph {
  int N;
  int changed;

  void r(unordered_set<int> froms, int termination) {

    bool c = false;
    unordered_set<int> f;
    for (auto &from : froms) {
      for (auto &i : nodes[from].arcs) {
        if (nodes[i.to].value > i.cost + nodes[from].value) {
          nodes[i.to].value = i.cost + nodes[from].value;
          f.insert(i.to);
          c = true;
        }
      }
    }

    if (!c)
      return;
    if (!termination) {
      changed = c;
      return;
    }
    r(move(f), termination - 1);
  }

public:
  bellmanford(int N)
      : graph::graph(N, std::numeric_limits<int>::max()), N(N), changed(false) {
  }
  bool run(int start) {
    nodes[start].value = 0;
    r({start}, N - 1);
    return changed;
  }
  int getValue(int index) { return nodes[index].value; }
};

int main(void) {

  int N, M, P;

  cin >> N >> M >> P;

  graph visit(N, 0);

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    visit.addArc(b - 1, a - 1, c);
  }

  visit[N - 1].value = 1;

  vector<int> f = {N - 1};

  for (int i = 0; i < N - 1; ++i) {
    vector<int> tmp;
    for (auto &n : f) {
      for (auto &a : visit[n].arcs) {
        visit[a.to].value = 1;
        tmp.push_back(a.to);
      }
    }
    f = move(tmp);
  }

  bellmanford g(N);

  for (int i = 0; i < N; ++i) {
    if (visit[i].value == 1) {
      for (auto &a : visit[i].arcs) {
        g.addArc(a.to, i, P - a.cost);
      }
    }
  }

  cout << ((g.run(0)) ? -1 : max(0, -g.getValue(N - 1)));

  return 0;
}

Fixed submission:
#include <iostream>
#include <limits>
#include <vector>
#include <unordered_set>

using namespace std;

struct arc {
  int to, cost;
};

struct node {
  int value;
  vector<arc> arcs;
  node(int v) : value(v) {}
};

class graph {
protected:
  vector<node> nodes;

public:
  graph(int N, int value) : nodes(N, value){};

  void addArc(int from, int to, int cost) {
    nodes[from].arcs.push_back({to, cost});
  }
  void addEdge(int a, int b, int cost) {
    addArc(a, b, cost);
    addArc(b, a, cost);
  }
  node &operator[](int index) { return nodes[index]; }
};

class bellmanford : public graph {
  int N;
  int changed;

  void r(unordered_set<int> froms, int termination) {

    bool c = false;
    unordered_set<int> f;
    for (auto &from : froms) {
      for (auto &i : nodes[from].arcs) {
        if (nodes[i.to].value > i.cost + nodes[from].value) {
          nodes[i.to].value = i.cost + nodes[from].value;
          f.insert(i.to);
          c = true;
        }
      }
    }

    if (!c)
      return;
    if (!termination) {
      changed = c;
      return;
    }
    r(move(f), termination - 1);
  }

public:
  bellmanford(int N)
      : graph::graph(N, std::numeric_limits<int>::max()), N(N), changed(false) {
  }
  bool run(int start) {
    nodes[start].value = 0;
    r({start}, N - 1);
    return changed;
  }
  int getValue(int index) { return nodes[index].value; }
};

int main(void) {

  int N, M, P;

  cin >> N >> M >> P;

  graph visit(N, 0);

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    visit.addArc(b - 1, a - 1, c);
  }

  visit[N - 1].value = 1;

  vector<int> f = {N - 1};

  for (int i = 0; i < N - 1; ++i) {
    vector<int> tmp;
    for (auto &n : f) {
      for (auto &a : visit[n].arcs) {
        if (visit[a.to].value)
          continue;
        visit[a.to].value = 1;
        tmp.push_back(a.to);
      }
    }
    f = move(tmp);
  }

  bellmanford g(N);

  for (int i = 0; i < N; ++i) {
    if (visit[i].value == 1) {
      for (auto &a : visit[i].arcs) {
        g.addArc(a.to, i, P - a.cost);
      }
    }
  }

  cout << ((g.run(0)) ? -1 : max(0, -g.getValue(N - 1)));

  return 0;
}