Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, m;
const int maxn = 1000 + 10;
typedef long long ll;
struct Edge {
  int from, to;
  ll dist;
  Edge(int u, int v, ll d) : from(u), to(v), dist(d) {}
};
vector<Edge> edges;
vector<int> G[maxn];
ll d[maxn];
int inq[maxn], cnt[maxn];
void addEdge(int from, int to, ll dist) {
  edges.push_back(Edge(from, to, dist));
  int t = edges.size();
  G[from].push_back(t - 1);
}
bool done[maxn];
bool ok = false;
void dfs(int u) {
  if (u == n) {
    ok = true;
    return;
  }
  if (done[u])
    return;
  done[u] = true;
  for (int i = 0; i < (int)G[u].size(); ++i) {
    Edge &e = edges[G[u][i]];
    dfs(e.to);
  }
}
const ll inf = (ll)1e18;
bool judge(int u) {
  ok = false;
  dfs(u);
  return ok;
}
bool dijkstra() {
  for (int i = 1; i <= n; ++i) {
    d[i] = inf;
  }
  d[1] = 0;
  inq[1] = 1;
  queue<int> Q;
  Q.push(1);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    inq[u] = 0;
    for (int i = 0; i < (int)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;
        if (!inq[e.to]) {
          Q.push(e.to);
          inq[e.to] = 1;
          if (++cnt[e.to] > n) {
            if (judge(e.to))
              return false;
          }
        }
      }
    }
  }
  return true;
}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    c = -c;
    addEdge(a, b, c);
  }
  if (dijkstra()) {
    cout << -d[n] << endl;
  } else {
    cout << "inf\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m;
const int maxn = 1000 + 10;
typedef long long ll;
struct Edge {
  int from, to;
  ll dist;
  Edge(int u, int v, ll d) : from(u), to(v), dist(d) {}
};
vector<Edge> edges;
vector<int> G[maxn];
ll d[maxn];
int inq[maxn], cnt[maxn];
void addEdge(int from, int to, ll dist) {
  edges.push_back(Edge(from, to, dist));
  int t = edges.size();
  G[from].push_back(t - 1);
}
bool done[maxn];
bool ok = false;
void dfs(int u) {
  if (u == n) {
    ok = true;
    return;
  }
  if (done[u])
    return;
  done[u] = true;
  for (int i = 0; i < (int)G[u].size(); ++i) {
    Edge &e = edges[G[u][i]];
    dfs(e.to);
  }
}
const ll inf = (ll)1e18;
bool judge(int u) {
  ok = false;
  dfs(u);
  return ok;
}
bool dijkstra() {
  for (int i = 1; i <= n; ++i) {
    d[i] = inf;
  }
  d[1] = 0;
  inq[1] = 1;
  queue<int> Q;
  Q.push(1);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    inq[u] = 0;
    for (int i = 0; i < (int)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;
        if (!inq[e.to] && cnt[e.to] <= n + 1) {
          Q.push(e.to);
          inq[e.to] = 1;
          if (++cnt[e.to] > n) {
            if (judge(e.to))
              return false;
          }
        }
      }
    }
  }
  return true;
}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    c = -c;
    addEdge(a, b, c);
  }
  if (dijkstra()) {
    cout << -d[n] << endl;
  } else {
    cout << "inf\n";
  }
  return 0;
}