Problem p03722 (C++)

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

using namespace std;

#define _for(i, j, N) for (int i = (j); i < (N); i++)
#define _rep(i, j, N) for (int i = (j); i <= (N); i++)
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define mk make_pair

typedef long long LL;
typedef pair<int, int> Interval;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  _for(i, 0, v.size()) os << v[i] << " ";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (typename set<T>::iterator it = v.begin(); it != v.end(); it++)
    os << *it << " ";
  return os;
}

const int maxn = 1005;
LL INF = (1LL) << 60;
int N, M;

struct Bellman_ford {
  int n, m;
  struct Edge {
    int from, to, dist;
    Edge(int from, int to, int dist) : from(from), to(to), dist(dist) { ; }
  };
  vector<int> G[maxn];
  // 判断进行松弛操作的次数
  int cnt[maxn];
  LL d[maxn];
  int p[maxn];
  // 判断是否在queue里
  bool inq[maxn];
  vector<Edge> edges;

  void init(int n) {
    this->n = n;
    edges.clear();
    _for(i, 0, n) G[i].clear();
  }

  void add_edge(int from, int to, int dist) {
    edges.push_back(Edge(from, to, dist));
    m = edges.size();
    G[from].push_back(m - 1);
  }

  bool bellman_ford(int s) {
    queue<int> Q;
    memset(inq, 0, sizeof(inq));
    memset(cnt, 0, sizeof(cnt));
    _for(i, 0, n) d[i] = INF;
    d[s] = 0;
    inq[s] = true;
    Q.push(s);

    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      inq[u] = false;
      for (int i = 0; i < G[u].size(); i++) {
        Edge &e = edges[G[u][i]];
        if (d[u] < INF && d[e.to] > d[u] + e.dist) {
          d[e.to] = d[u] + e.dist;
          p[e.to] = G[u][i];
          if (!inq[e.to]) {
            Q.push(e.to);
            inq[e.to] = true;
            if (e.to == n - 1 && ++cnt[e.to] > n)
              return false;
          }
        }
      }
    }
    return true;
  }
};
int main() {
  cin >> N >> M;
  Bellman_ford bf;
  bf.init(N);

  _for(i, 0, M) {
    int a, b;
    LL c;
    scanf("%d %d %lld", &a, &b, &c);
    bf.add_edge(a - 1, b - 1, -c);
  }

  if (bf.bellman_ford(0)) {
    printf("%lld\n", -bf.d[N - 1]);
  } else {
    printf("inf\n");
  }

  return 0;
}


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

using namespace std;

#define _for(i, j, N) for (int i = (j); i < (N); i++)
#define _rep(i, j, N) for (int i = (j); i <= (N); i++)
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define mk make_pair

typedef long long LL;
typedef pair<int, int> Interval;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  _for(i, 0, v.size()) os << v[i] << " ";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (typename set<T>::iterator it = v.begin(); it != v.end(); it++)
    os << *it << " ";
  return os;
}

const int maxn = 1005;
LL INF = (1LL) << 60;
int N, M;

struct Bellman_ford {
  int n, m;
  struct Edge {
    int from, to, dist;
    Edge(int from, int to, int dist) : from(from), to(to), dist(dist) { ; }
  };
  vector<int> G[maxn];
  // 判断进行松弛操作的次数
  int cnt[maxn];
  LL d[maxn];
  int p[maxn];
  // 判断是否在queue里
  bool inq[maxn];
  vector<Edge> edges;

  void init(int n) {
    this->n = n;
    edges.clear();
    _for(i, 0, n) G[i].clear();
  }

  void add_edge(int from, int to, int dist) {
    edges.push_back(Edge(from, to, dist));
    m = edges.size();
    G[from].push_back(m - 1);
  }

  bool bellman_ford(int s) {
    queue<int> Q;
    memset(inq, 0, sizeof(inq));
    memset(cnt, 0, sizeof(cnt));
    _for(i, 0, n) d[i] = INF;
    d[s] = 0;
    inq[s] = true;
    Q.push(s);

    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      inq[u] = false;
      for (int i = 0; i < G[u].size(); i++) {
        Edge &e = edges[G[u][i]];
        if (d[u] < INF && d[e.to] > d[u] + e.dist) {
          d[e.to] = d[u] + e.dist;
          p[e.to] = G[u][i];
          if (!inq[e.to]) {
            Q.push(e.to);
            inq[e.to] = true;
            if (e.to == n - 1 && ++cnt[e.to] > n)
              return false;
            if (e.to != n - 1 && ++cnt[e.to] > 2 * n + 1)
              return true;
          }
        }
      }
    }
    return true;
  }
};
int main() {
  cin >> N >> M;
  Bellman_ford bf;
  bf.init(N);

  _for(i, 0, M) {
    int a, b;
    LL c;
    scanf("%d %d %lld", &a, &b, &c);
    bf.add_edge(a - 1, b - 1, -c);
  }

  if (bf.bellman_ford(0)) {
    printf("%lld\n", -bf.d[N - 1]);
  } else {
    printf("inf\n");
  }

  return 0;
}