Problem p02949 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define lb(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define ub(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
using Pair = pair<ll, pair<int, int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>>
const ll mod = 1000000007;
// const ll mod=998244353;
const ld pi = acos(-1.0);
const ll INF = 1LL << 61;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

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

  int n, m;
  ll z;
  cin >> n >> m >> z;

  vector<int> a(m);
  vector<int> b(m);
  vector<ll> c(m);
  vector<vector<int>> g(n);
  vector<vector<int>> gr(n);
  vector<bool> ok(n, true);
  rep(i, m) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    c[i] -= z;
    g[a[i]].pb(b[i]);
    gr[b[i]].pb(a[i]);
  }

  // 頂点１からいけない場所をok falseに
  vector<ll> dist(n, -1);
  deque<int> que;
  dist[0] = 0;
  que.pb(0);

  while (!que.empty()) {
    int v = que.front();
    que.pop_front();
    for (int nv : g[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      que.pb(nv);
    }
  }

  rep(i, n) {
    if (dist[i] == -1)
      ok[i] = false;
    dist[i] = -1;
  }

  // 頂点nにいけない場所をok falseに
  dist[n - 1] = 0;
  que.pb(n - 1);

  while (!que.empty()) {
    int v = que.front();
    que.pop_front();
    for (int nv : gr[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      que.pb(nv);
    }
  }

  rep(i, n) {
    if (dist[i] == -1)
      ok[i] = false;
    dist[i] = -INF;
  }

  // ok true同志にだけ変をはる
  vector<vector<pair<int, ll>>> gp(n);
  rep(i, m) {
    if (ok[a[i]] && ok[b[i]]) {
      gp[a[i]].pb({b[i], c[i]});
    }
  }

  dist[0] = 0;
  que.pb(0);
  int cnt = 0;
  while (!que.empty()) {
    cnt++;
    if (cnt > 1e6) {
      cout << -1 << endl;
      return 0;
    }
    int v = que.front();
    que.pop_front();
    for (auto x : gp[v]) {
      int nv = x.fi;
      ll y = x.se;
      if (dist[nv] >= dist[v] + y)
        continue;
      dist[nv] = dist[v] + y;
      que.pb(nv);
    }
  }

  cout << max(0ll, dist[n - 1]) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define lb(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define ub(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
using Pair = pair<ll, pair<int, int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>>
const ll mod = 1000000007;
// const ll mod=998244353;
const ld pi = acos(-1.0);
const ll INF = 1LL << 61;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

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

  int n, m;
  ll z;
  cin >> n >> m >> z;

  vector<int> a(m);
  vector<int> b(m);
  vector<ll> c(m);
  vector<vector<int>> g(n);
  vector<vector<int>> gr(n);
  vector<bool> ok(n, true);
  rep(i, m) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    c[i] -= z;
    g[a[i]].pb(b[i]);
    gr[b[i]].pb(a[i]);
  }

  // 頂点１からいけない場所をok falseに
  vector<ll> dist(n, -1);
  deque<int> que;
  dist[0] = 0;
  que.pb(0);

  while (!que.empty()) {
    int v = que.front();
    que.pop_front();
    for (int nv : g[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      que.pb(nv);
    }
  }

  rep(i, n) {
    if (dist[i] == -1)
      ok[i] = false;
    dist[i] = -1;
  }

  // 頂点nにいけない場所をok falseに
  dist[n - 1] = 0;
  que.pb(n - 1);

  while (!que.empty()) {
    int v = que.front();
    que.pop_front();
    for (int nv : gr[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      que.pb(nv);
    }
  }

  rep(i, n) {
    if (dist[i] == -1)
      ok[i] = false;
    dist[i] = -INF;
  }

  // ok true同志にだけ変をはる
  vector<vector<pair<int, ll>>> gp(n);
  rep(i, m) {
    if (ok[a[i]] && ok[b[i]]) {
      if (a[i] != b[i])
        gp[a[i]].pb({b[i], c[i]});
      else {
        if (c[i] > 0) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  }

  dist[0] = 0;
  que.pb(0);
  int cnt = 0;
  while (!que.empty()) {
    cnt++;
    if (cnt > 1e6) {
      cout << -1 << endl;
      return 0;
    }
    int v = que.front();
    que.pop_front();
    for (auto x : gp[v]) {
      int nv = x.fi;
      ll y = x.se;
      if (dist[nv] >= dist[v] + y)
        continue;
      dist[nv] = dist[v] + y;
      que.pb(nv);
    }
  }

  cout << max(0ll, dist[n - 1]) << endl;
}