Problem p03722 (C++)

Buggy submission (status: Runtime Error):
// ConsoleApplication1.cpp : コンソール アプリケーションのエントリ
// ポイントを定義します。
//

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
typedef long long i64;
using namespace std;
int n;
int m;
int a[1000];
int b[1000];
i64 c[1000];
struct Edge {
  int src, dst;
  i64 cost;

public:
  Edge(int src, int dst, i64 cost) {
    this->src = src;
    this->dst = dst;
    this->cost = cost;
  }
};
struct State {
  int cur;
  i64 cost;

public:
  State(int cur, i64 cost) {
    this->cur = cur;
    this->cost = cost;
  }
};
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d%lld", a + i, b + i, c + i);
    --a[i];
    --b[i];
    c[i] *= -1;
  }
  vector<vector<Edge>> g;
  vector<vector<int>> rev;
  g.resize(n);
  rev.resize(n);
  for (int i = 0; i < m; ++i) {
    Edge e = {a[i], b[i], c[i]};
    g[a[i]].push_back(Edge(a[i], b[i], c[i]));
    rev[b[i]].push_back(a[i]);
  }

  i64 d[1000];
  for (int i = 0; i < n; ++i) {
    d[i] = 1LL << 60;
  }

  d[0] = 0;
  vector<queue<State>> pend;
  pend.resize(n + 2);
  pend[0].push(State(0, 0));
  for (int i = 0; i < n; ++i) {
    while (pend[i].size() > 0) {
      // while (!pend[i].empty()) {
      State s = pend[i].front();
      pend[i].pop();
      if (d[s.cur] < s.cost)
        continue;
      for (Edge e : g[s.cur]) {
        if (d[e.dst] > d[s.cur] + e.cost) {
          d[e.dst] = d[s.cur] + e.cost;
          State ns = {e.dst, d[e.dst]};
          pend[i + 1].push(ns);
        }
      }
    }
  }
  bool reach[1000];
  queue<int> q;
  q.push(n - 1);
  reach[n - 1] = true;
  while (q.size() > 0) {
    int v = q.front();
    q.pop();
    for (int dst : rev[v]) {
      if (!reach[dst]) {
        reach[dst] = true;
        q.push(dst);
      }
    }
  }
  bool f = false;
  while (pend[n].size() > 0) {
    State s = pend[n].front();
    pend[n].pop();
    f |= reach[s.cur];
  }
  if (!f) {
    printf("%lld\n", -d[n - 1]);
  } else {
    printf("inf\n");
  }

  return 0;
}

Fixed submission:
// ConsoleApplication1.cpp : コンソール アプリケーションのエントリ
// ポイントを定義します。
//

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
typedef long long i64;
using namespace std;
int n;
int m;
int a[2000];
int b[2000];
i64 c[2000];
struct Edge {
  int src, dst;
  i64 cost;

public:
  Edge(int src, int dst, i64 cost) {
    this->src = src;
    this->dst = dst;
    this->cost = cost;
  }
};
struct State {
  int cur;
  i64 cost;

public:
  State(int cur, i64 cost) {
    this->cur = cur;
    this->cost = cost;
  }
};
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d%lld", a + i, b + i, c + i);
    --a[i];
    --b[i];
    c[i] *= -1;
  }
  vector<vector<Edge>> g;
  vector<vector<int>> rev;
  g.resize(n);
  rev.resize(n);
  for (int i = 0; i < m; ++i) {
    Edge e = {a[i], b[i], c[i]};
    g[a[i]].push_back(Edge(a[i], b[i], c[i]));
    rev[b[i]].push_back(a[i]);
  }

  i64 d[1000];
  for (int i = 0; i < n; ++i) {
    d[i] = 1LL << 60;
  }

  d[0] = 0;
  vector<queue<State>> pend;
  pend.resize(n + 2);
  pend[0].push(State(0, 0));
  for (int i = 0; i < n; ++i) {
    while (pend[i].size() > 0) {
      // while (!pend[i].empty()) {
      State s = pend[i].front();
      pend[i].pop();
      if (d[s.cur] < s.cost)
        continue;
      for (Edge e : g[s.cur]) {
        if (d[e.dst] > d[s.cur] + e.cost) {
          d[e.dst] = d[s.cur] + e.cost;
          State ns = {e.dst, d[e.dst]};
          pend[i + 1].push(ns);
        }
      }
    }
  }
  bool reach[1000];
  queue<int> q;
  q.push(n - 1);
  reach[n - 1] = true;
  while (q.size() > 0) {
    int v = q.front();
    q.pop();
    for (int dst : rev[v]) {
      if (!reach[dst]) {
        reach[dst] = true;
        q.push(dst);
      }
    }
  }
  bool f = false;
  while (pend[n].size() > 0) {
    State s = pend[n].front();
    pend[n].pop();
    f |= reach[s.cur];
  }
  if (!f) {
    printf("%lld\n", -d[n - 1]);
  } else {
    printf("inf\n");
  }

  return 0;
}