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

#include <bits/stdc++.h>

using namespace std;

const int logMaxStep = 30;

typedef long long ll;
typedef vector<vector<ll>> Graph;

int GetLogStep(int S) {
  int res;
  for (res = 0; (1 << res) <= S; ++res)
    ;
  return res;
}
Graph nextG(const Graph &g1, const Graph &g2) {
  int N = g1.size();
  Graph res(N, vector<ll>(N, 0));
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      for (int k = 0; k < N; ++k) {
        if (g1[i][k] > 0 && g2[k][j] > 0) {
          res[i][j] = max(res[i][j], g1[i][k] + g2[k][j]);
        }
      }
    }
  }
  return res;
}
vector<int> nextDP(vector<ll> &dp, Graph &g) {
  int N = g.size();
  vector<int> prevV(N, -1);
  vector<ll> ndp(N, -1);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (dp[i] >= 0 && g[i][j] > 0) {
        if (dp[i] + g[i][j] > ndp[j]) {
          ndp[j] = dp[i] + g[i][j];
          prevV[j] = i;
        }
      }
    }
  }
  for (int i = 0; i < N; ++i) {
    dp[i] = ndp[i];
  }
  return prevV;
}
ll GetMaxScore(vector<Graph> &powG, int S) {
  int N = powG[0].size();
  int logS = GetLogStep(S);
  vector<ll> dp(N, 0);
  for (int i = 0; i < logS; ++i) {
    Graph &g = powG[i];
    if ((1 << i) & S) {
      nextDP(dp, g);
    }
  }
  ll res = -1;
  for (int i = 0; i < dp.size(); ++i) {
    res = max(res, dp[i]);
  }
  return res;
}
ll check200Step(Graph &G, int K) {
  int N = G.size();
  vector<ll> dp(N, 0);
  vector<vector<int>> prevV;
  ll lastAns = -1;
  for (int t = 0; t < 1000; ++t) {
    prevV.push_back(nextDP(dp, G));
    int aid = 0;
    for (int i = 0; i < dp.size(); ++i) {
      if (dp[i] > dp[aid])
        aid = i;
    }
    if (dp[aid] >= K) {
      cout << t + 1 << endl;
      if (t + 1 <= 100) {
        vector<int> vl;
        int v = aid;
        vl.push_back(v);
        for (int i = t; i >= 0; --i) {
          v = prevV[i][v];
          vl.push_back(v);
        }
        reverse(vl.begin(), vl.end());
        for (int i = 0; i < vl.size(); ++i) {
          if (i)
            cout << " ";
          cout << vl[i];
        }
        cout << endl;
      }
      return -2;
    }
    lastAns = dp[aid];
  }
  return lastAns;
}
int main() {
  int N, E, K;
  cin >> N >> E >> K;
  Graph G(N, vector<ll>(N, 0));
  for (int i = 0; i < E; ++i) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    G[a][b] = max(G[a][b], c);
  }
  ll result = check200Step(G, K);
  if (result > 0) {
    vector<Graph> powG(logMaxStep + 1);
    powG[0] = G;
    for (int i = 0; i < logMaxStep; ++i) {
      powG[i + 1] = nextG(powG[i], powG[i]);
    }
    /*
                    for (int i = N; i <= N+100; ++i) {
                            if (GetMaxScore(powG, i+1) >= result) {
                                    assert(GetMaxScore(powG, i+1) >
       GetMaxScore(powG, i));
                            }
                    }
    */
    int ans = K;
    for (int rest = 0; rest < N; ++rest) {
      int l = 0, r = K / N + 2;
      while (r - l > 1) {
        int m = (l + r) / 2;
        if (GetMaxScore(powG, m * N + rest) >= K) {
          r = m;
        } else {
          l = m;
        }
      }
      ans = min(ans, r * N + rest);
    }
    cout << ans << endl;
  } else if (result == -1) {
    cout << -1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int logMaxStep = 30;

typedef long long ll;
typedef vector<vector<ll>> Graph;

int GetLogStep(int S) {
  int res;
  for (res = 0; (1 << res) <= S; ++res)
    ;
  return res;
}
Graph nextG(const Graph &g1, const Graph &g2) {
  int N = g1.size();
  Graph res(N, vector<ll>(N, 0));
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      for (int k = 0; k < N; ++k) {
        if (g1[i][k] > 0 && g2[k][j] > 0) {
          res[i][j] = max(res[i][j], g1[i][k] + g2[k][j]);
        }
      }
    }
  }
  return res;
}
vector<int> nextDP(vector<ll> &dp, Graph &g) {
  int N = g.size();
  vector<int> prevV(N, -1);
  vector<ll> ndp(N, -1);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (dp[i] >= 0 && g[i][j] > 0) {
        if (dp[i] + g[i][j] > ndp[j]) {
          ndp[j] = dp[i] + g[i][j];
          prevV[j] = i;
        }
      }
    }
  }
  for (int i = 0; i < N; ++i) {
    dp[i] = ndp[i];
  }
  return prevV;
}
ll GetMaxScore(vector<Graph> &powG, int S) {
  int N = powG[0].size();
  int logS = GetLogStep(S);
  vector<ll> dp(N, 0);
  for (int i = 0; i < logS; ++i) {
    Graph &g = powG[i];
    if ((1 << i) & S) {
      nextDP(dp, g);
    }
  }
  ll res = -1;
  for (int i = 0; i < dp.size(); ++i) {
    res = max(res, dp[i]);
  }
  return res;
}
int check200Step(Graph &G, int K) {
  int N = G.size();
  vector<ll> dp(N, 0);
  vector<vector<int>> prevV;
  ll lastAns = -1;
  for (int t = 0; t < 200; ++t) {
    prevV.push_back(nextDP(dp, G));
    int aid = 0;
    for (int i = 0; i < dp.size(); ++i) {
      if (dp[i] > dp[aid])
        aid = i;
    }
    if (dp[aid] >= K) {
      cout << t + 1 << endl;
      if (t + 1 <= 100) {
        vector<int> vl;
        int v = aid;
        vl.push_back(v);
        for (int i = t; i >= 0; --i) {
          v = prevV[i][v];
          vl.push_back(v);
        }
        reverse(vl.begin(), vl.end());
        for (int i = 0; i < vl.size(); ++i) {
          if (i)
            cout << " ";
          cout << vl[i];
        }
        cout << endl;
      }
      return -2;
    }
    lastAns = dp[aid];
  }
  return lastAns;
}
int main() {
  int N, E, K;
  cin >> N >> E >> K;
  Graph G(N, vector<ll>(N, 0));
  for (int i = 0; i < E; ++i) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    G[a][b] = max(G[a][b], c);
  }
  int result = check200Step(G, K);
  if (result > 0) {
    vector<Graph> powG(logMaxStep + 1);
    powG[0] = G;
    for (int i = 0; i < logMaxStep; ++i) {
      powG[i + 1] = nextG(powG[i], powG[i]);
    }

    for (int i = N; i <= N + 100; ++i) {
      if (GetMaxScore(powG, i + 1) >= result) {
        assert(GetMaxScore(powG, i + 1) > GetMaxScore(powG, i));
      }
    }
    int l = 0, r = K + 1;
    for (int t = 0; t < logMaxStep; ++t) {
      int m = (l + r) / 2;
      if (GetMaxScore(powG, m) >= K) {
        r = m;
      } else {
        l = m;
      }
    }
    assert(r - l <= 1);
    assert(r <= K);
    cout << r << endl;
  } else if (result == -1) {
    cout << -1 << endl;
  }
}