Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int>>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_E 100000
#define MAX_V 100000

struct edge {
  ll from, to, cost;
};
ll d[MAX_V];
ll V, E;

edge es[MAX_E];

void shortest_path(ll s) {
  rep(i, 0, V) d[i] = INF;
  d[s] = 0;
  while (true) {
    bool update = false;
    rep(i, 0, E) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

bool find_negative_loop(ll s) {
  // memset(d, 0, sizeof(d));
  fill(d, d + V, INF);
  d[s] = 0;
  rep(i, 0, V) {
    rep(j, 0, E) {
      edge e = es[j];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        if (i == V - 1 and e.to == V - 1)
          return true;
      }
    }
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> V >> E;
  rep(i, 0, E) {
    ll a, b, c;
    cin >> a >> b >> c;
    edge e = {--a, --b, -c};
    es[i] = e;
  }

  if (find_negative_loop(0)) {
    cout << "inf" << endl;
    return 0;
  }

  shortest_path(0);
  cout << -d[V - 1] << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int>>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_E 100000
#define MAX_V 100000

struct edge {
  ll from, to, cost;
};
ll d[MAX_V];
ll V, E;

edge es[MAX_E];

void shortest_path(ll s) {
  rep(i, 0, V) d[i] = INF;
  d[s] = 0;
  while (true) {
    bool update = false;
    rep(i, 0, E) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

bool find_negative_loop(ll s) {
  // memset(d, 0, sizeof(d));
  fill(d, d + V, INF);
  d[s] = 0;
  rep(i, 0, V) {
    rep(j, 0, E) {
      edge e = es[j];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        if (i == V - 1 and e.to == V - 1)
          return true;
      }
    }
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> V >> E;
  rep(i, 0, E) {
    ll a, b, c;
    cin >> a >> b >> c;
    edge e = {--a, --b, -c};
    es[i] = e;
  }

  if (find_negative_loop(0)) {
    cout << "inf" << endl;
    return 0;
  }

  if (d[V - 1] == INF)
    cout << "inf" << endl;
  else
    cout << -d[V - 1] << endl;
}