Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

using i64 = int64_t;
constexpr const i64 INF = INT64_MAX / 2;

struct node {
  i64 min_dist;
  node() : min_dist(INF) {}
};

struct edge {
  int from;
  int to;
  i64 cost;
};

bool bellman_ford(vector<node> &nodes, vector<edge> const &edges) {
  int N = nodes.size();
  for (int i = 0; i < N - 1; i++) {
    for (auto const &e : edges) {
      nodes[e.to].min_dist =
          min(nodes[e.to].min_dist, nodes[e.from].min_dist + e.cost);
    }
  }

  for (auto const &e : edges) {
    if (nodes[e.to].min_dist > nodes[e.from].min_dist + e.cost) {
      return true;
    }
  }

  return false;
}

bool mark_reachable_to_n(vector<vector<int>> const &graph,
                         vector<bool> &unreachable, int from,
                         unordered_set<int> &visited) {
  if (from == graph.size() - 1)
    return true;
  if (visited.count(from))
    return false;
  visited.insert(from);
  bool res = false;
  for (auto to : graph[from]) {
    bool t = mark_reachable_to_n(graph, unreachable, to, visited);
    res = res || t;
  }
  visited.erase(from);
  if (res)
    unreachable[from] = false;
  return res;
}

void remove_edges_for_unreachable_vertices(vector<edge> &edges,
                                           vector<vector<int>> const &graph) {
  vector<bool> unreachable(graph.size(), true);
  unreachable[graph.size() - 1] = false;
  unordered_set<int> visited;
  assert(mark_reachable_to_n(graph, unreachable, 0, visited));
  assert(visited.size() == 0);

  for (int a = 0; a < graph.size(); a++) {
    if (!unreachable[a])
      continue;
    cerr << a << " is unreachable!" << endl;
    for (auto it = edges.begin(); it != edges.end(); ++it) {
      if (it->from == a || it->to == a) {
        it = edges.erase(it);
      }
      if (it == edges.end())
        break;
    }
  }
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<vector<int>> graph(N);
  vector<edge> edges;
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edges.push_back({a, b, -c});
    graph[a].push_back(b);
  }

  remove_edges_for_unreachable_vertices(edges, graph);
  vector<node> nodes(N);
  nodes[0].min_dist = 0;
  if (bellman_ford(nodes, edges)) {
    cout << "inf" << endl;
  } else {
    cout << -nodes[N - 1].min_dist << endl;
  }

  return 0;
}


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

using i64 = int64_t;
constexpr const i64 INF = INT64_MAX / 2;

struct node {
  i64 min_dist;
  node() : min_dist(INF) {}
};

struct edge {
  int from;
  int to;
  i64 cost;
};

bool bellman_ford(vector<node> &nodes, vector<edge> const &edges) {
  int N = nodes.size();
  for (int i = 0; i < N - 1; i++) {
    for (auto const &e : edges) {
      nodes[e.to].min_dist =
          min(nodes[e.to].min_dist, nodes[e.from].min_dist + e.cost);
    }
  }

  for (auto const &e : edges) {
    if (nodes[e.to].min_dist > nodes[e.from].min_dist + e.cost) {
      return true;
    }
  }

  return false;
}

bool mark_reachable_to_n(vector<vector<int>> const &graph,
                         vector<bool> &unreachable, int from,
                         unordered_set<int> &visited) {
  if (from == graph.size() - 1)
    return true;
  if (!unreachable[from])
    return false;
  if (visited.count(from))
    return false;
  visited.insert(from);
  bool res = false;
  for (auto to : graph[from]) {
    bool t = mark_reachable_to_n(graph, unreachable, to, visited);
    res = res || t;
  }
  visited.erase(from);
  if (res)
    unreachable[from] = false;
  return res;
}

void remove_edges_for_unreachable_vertices(vector<edge> &edges,
                                           vector<vector<int>> const &graph) {
  vector<bool> unreachable(graph.size(), true);
  unreachable[graph.size() - 1] = false;
  unordered_set<int> visited;
  assert(mark_reachable_to_n(graph, unreachable, 0, visited));
  assert(visited.size() == 0);

  for (int a = 0; a < graph.size(); a++) {
    if (!unreachable[a])
      continue;
    cerr << a << " is unreachable!" << endl;
    for (auto it = edges.begin(); it != edges.end(); ++it) {
      if (it->from == a || it->to == a) {
        it = edges.erase(it);
      }
      if (it == edges.end())
        break;
    }
  }
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<vector<int>> graph(N);
  vector<edge> edges;
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edges.push_back({a, b, -c});
    graph[a].push_back(b);
  }

  remove_edges_for_unreachable_vertices(edges, graph);
  vector<node> nodes(N);
  nodes[0].min_dist = 0;
  if (bellman_ford(nodes, edges)) {
    cout << "inf" << endl;
  } else {
    cout << -nodes[N - 1].min_dist << endl;
  }

  return 0;
}