Problem p01546 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 LINF = 1LL << 60;
const int INF = 1 << 30;

int V, E, K;
int64 g[31][150][150];

int main() {
  fill_n(**g, 31 * 150 * 150, -LINF);

  cin >> V >> E >> K;
  for (int i = 0; i < V; i++) {
    g[0][i][i] = 0;
  }
  for (int i = 0; i < E; i++) {
    int a, b;
    int64 c;
    cin >> a >> b >> c;
    g[0][a][b] = max(g[0][a][b], c);
  }

  for (int i = 0; i < 30; i++) {
    for (int j = 0; j < V; j++) {
      for (int k = 0; k < V; k++) {
        for (int l = 0; l < V; l++) {
          g[i + 1][k][l] = max(g[i + 1][k][l], g[i][k][j] + g[i][j][l]);
        }
      }
    }
  }

  auto check = [&](int bit) {
    vector<int64> dp(V);
    for (int k = 30; k >= 0; k--) {
      if ((bit >> k) & 1) {
        vector<int64> dp2(V);
        for (int i = 0; i < V; i++) {
          for (int j = 0; j < V; j++) {
            dp2[j] = max(dp2[j], dp[i] + g[k][i][j]);
          }
        }
        dp.swap(dp2);
      }
    }
    return *max_element(begin(dp), end(dp)) >= K;
  };

  int ok = INF, ng = 0;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }

  if (ok == INF) {
    cout << -1 << endl;
  } else {
    cout << ok << endl;

    if (ok <= 100) {
      int64 dp[101][150] = {{}}, pv[101][150];
      for (int i = 0; i < ok; i++) {
        for (int j = 0; j < V; j++) {
          for (int k = 0; k < V; k++) {
            if (dp[i][j] + g[0][j][k] > dp[i + 1][k]) {
              dp[i + 1][k] = dp[i][j] + g[0][j][k];
              pv[i + 1][k] = j;
            }
          }
        }
      }
      int high = 00;
      for (int i = 0; i < V; i++) {
        if (dp[ok][i] >= dp[ok][high])
          high = i;
      }
      vector<int> route;
      for (int i = ok; i >= 0; i--) {
        route.push_back(high);
        high = pv[i][high];
      }
      reverse(begin(route), end(route));
      for (int i = 0; i < route.size(); i++) {
        if (i)
          cout << " ";
        cout << route[i];
      }
      cout << endl;
    }
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 LINF = 1LL << 60;
const int INF = 1 << 30;

int V, E, K;
int64 g[31][150][150];

int main() {
  fill_n(**g, 31 * 150 * 150, -LINF);

  cin >> V >> E >> K;
  for (int i = 0; i < E; i++) {
    g[0][i][i] = 0;
  }
  for (int i = 0; i < E; i++) {
    int a, b;
    int64 c;
    cin >> a >> b >> c;
    g[0][a][b] = max(g[0][a][b], c);
  }

  for (int i = 0; i < 30; i++) {
    for (int j = 0; j < V; j++) {
      for (int k = 0; k < V; k++) {
        for (int l = 0; l < V; l++) {
          g[i + 1][k][l] = max(g[i + 1][k][l], g[i][k][j] + g[i][j][l]);
        }
      }
    }
  }

  auto check = [&](int bit) {
    vector<int64> dp(V);
    for (int k = 30; k >= 0; k--) {
      if ((bit >> k) & 1) {
        vector<int64> dp2(V);
        for (int i = 0; i < V; i++) {
          for (int j = 0; j < V; j++) {
            dp2[j] = max(dp2[j], dp[i] + g[k][i][j]);
          }
        }
        dp.swap(dp2);
      }
    }
    return *max_element(begin(dp), end(dp)) >= K;
  };

  int ok = INF, ng = 0;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }

  if (ok == INF) {
    cout << -1 << endl;
  } else {
    cout << ok << endl;

    if (ok <= 100) {
      int64 dp[101][150] = {{}}, pv[101][150];
      for (int i = 0; i < ok; i++) {
        for (int j = 0; j < V; j++) {
          for (int k = 0; k < V; k++) {
            if (dp[i][j] + g[0][j][k] > dp[i + 1][k]) {
              dp[i + 1][k] = dp[i][j] + g[0][j][k];
              pv[i + 1][k] = j;
            }
          }
        }
      }
      int high = 00;
      for (int i = 0; i < V; i++) {
        if (dp[ok][i] >= dp[ok][high])
          high = i;
      }
      vector<int> route;
      for (int i = ok; i >= 0; i--) {
        route.push_back(high);
        high = pv[i][high];
      }
      reverse(begin(route), end(route));
      for (int i = 0; i < route.size(); i++) {
        if (i)
          cout << " ";
        cout << route[i];
      }
      cout << endl;
    }
  }
}