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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;

int V, E, K;
const int INF = (1 << 29);
mat G;

mat g[30];
mat u[30];

int add(int a, int b) {
  if (a == -INF || b == -INF)
    return -INF;
  return min(a + b, INF);
}

pair<mat, mat> merge(mat A, mat B, bool flag = false) {
  mat X(V, vec(V, -INF));
  mat Y(V, vec(V, -INF));

  for (int from = 0; from < V; from++) {
    for (int to = 0; to < V; to++) {
      for (int ex = 0; ex < V; ex++) {
        int cost = add(A[from][ex], B[ex][to]);
        if (X[from][to] < cost) {
          X[from][to] = cost;
          Y[from][to] = ex;
        }
      }
    }
  }
  return make_pair(X, Y);
}

int calc(int m, int &si, int &ti) {
  mat t;
  for (int i = 0; i < 30; i++) {
    if (m >> i & 1) {
      if (t.empty()) {
        t = g[i];
      } else {
        t = merge(t, g[i]).first;
      }
    }
  }

  int res = -INF;
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      if (res < t[i][j]) {
        si = i;
        ti = j;
        res = t[i][j];
      }
    }
  }
  return res;
}

void Dfs(vec &s, int from, int to, int len) {
  if (len <= 1)
    return;
  if (len == (len & -len)) {

    for (int i = 0; i < 30; i++) {
      if (len >> i & 1) {
        int ex = u[i][from][to];
        Dfs(s, from, ex, len / 2);
        s.push_back(ex);
        Dfs(s, ex, to, len / 2);
        return;
      }
    }
  }

  mat t;

  int ex = -1;
  int sub = -1;

  for (int i = 0; i < 30; i++) {
    if (~len >> i & 1)
      continue;
    if (t.empty()) {
      t = g[i];
      continue;
    }
    pair<mat, mat> tmp = merge(t, g[i]);
    t = tmp.first;
    ex = tmp.second[from][to];
    sub = (1 << i);
  }

  Dfs(s, from, ex, len - sub);
  s.push_back(ex);
  Dfs(s, ex, to, sub);
}

int main() {
  cin >> V >> E >> K;
  G = mat(V, vec(V, -INF));
  g[0] = u[0] = G;
  for (int i = 0; i < V; i++)
    g[0][i][i] = 0;
  for (int i = 0; i < E; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a][b] = max(G[a][b], c);
    if (g[0][a][b] < c) {
      g[0][a][b] = c;
      u[0][a][b] = b;
    }
  }

  for (int i = 1; i < 30; i++) {
    pair<mat, mat> temp = merge(g[i - 1], g[i - 1]);
    g[i] = temp.first;
    u[i] = temp.second;
  }
  int left = 1, right = K + 1, mid, si, ti;
  while (left < right) {
    mid = (left + right) / 2;
    if (calc(mid, si, ti) >= K) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }

  for (int i = 0; i < V; i++)
    g[0][i][i] = -INF;
  for (int i = 1; i < 30; i++) {
    pair<mat, mat> temp = merge(g[i - 1], g[i - 1]);
    g[i] = temp.first;
    u[i] = temp.second;
  }
  calc(left, si, ti);

  if (left == K + 1) {
    cout << -1 << endl;
  } else {
    cout << left << endl;
    if (left <= 100) {
      vec tab;
      tab.push_back(si);
      Dfs(tab, si, ti, left);
      tab.push_back(ti);

      int sum = 0;
      for (int i = 0; i < (int)tab.size(); i++) {

        if (i > 0) {
          int cost = G[tab[i - 1]][tab[i]];
          if (cost == -INF) {
            assert(0);
          } else {
            sum = add(sum, cost);
          }
        }

        if (i)
          cout << ' ';
        cout << tab[i];
      }
      cout << endl;
      // cout<<sum<<' '<<calc(left,si,ti)<<endl;
      assert(sum == calc(left, si, ti));
    }
  }
  return 0;
}


Error: -6
86235e68-6f67-4fc8-a9f5-2a41ac2045f8.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p01546/C++/s366245359.cpp:164: int main(): Assertion `sum == calc(left,si,ti)' failed.


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;

int V, E, K;
const int INF = (1 << 29);
mat G;

mat g[30];
mat u[30];

int add(int a, int b) {
  if (a == -INF || b == -INF)
    return -INF;
  return min(a + b, INF);
}

pair<mat, mat> merge(mat A, mat B, bool flag = false) {
  mat X(V, vec(V, -INF));
  mat Y(V, vec(V, -INF));

  for (int from = 0; from < V; from++) {
    for (int to = 0; to < V; to++) {
      for (int ex = 0; ex < V; ex++) {
        int cost = add(A[from][ex], B[ex][to]);
        if (X[from][to] < cost) {
          X[from][to] = cost;
          Y[from][to] = ex;
        }
      }
    }
  }
  return make_pair(X, Y);
}

int calc(int m, int &si, int &ti) {
  mat t;
  for (int i = 0; i < 30; i++) {
    if (m >> i & 1) {
      if (t.empty()) {
        t = g[i];
      } else {
        t = merge(t, g[i]).first;
      }
    }
  }

  int res = -INF;
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      if (res < t[i][j]) {
        si = i;
        ti = j;
        res = t[i][j];
      }
    }
  }
  return res;
}

void Dfs(vec &s, int from, int to, int len) {
  if (len <= 1)
    return;
  if (len == (len & -len)) {

    for (int i = 0; i < 30; i++) {
      if (len >> i & 1) {
        int ex = u[i][from][to];
        Dfs(s, from, ex, len / 2);
        s.push_back(ex);
        Dfs(s, ex, to, len / 2);
        return;
      }
    }
  }

  mat t;

  int ex = -1;
  int sub = -1;

  for (int i = 0; i < 30; i++) {
    if (~len >> i & 1)
      continue;
    if (t.empty()) {
      t = g[i];
      continue;
    }
    pair<mat, mat> tmp = merge(t, g[i]);
    t = tmp.first;
    ex = tmp.second[from][to];
    sub = (1 << i);
  }

  Dfs(s, from, ex, len - sub);
  s.push_back(ex);
  Dfs(s, ex, to, sub);
}

int main() {
  cin >> V >> E >> K;
  G = mat(V, vec(V, -INF));
  g[0] = u[0] = G;
  for (int i = 0; i < V; i++)
    g[0][i][i] = 0;
  for (int i = 0; i < E; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a][b] = max(G[a][b], c);
    if (g[0][a][b] < c) {
      g[0][a][b] = c;
      u[0][a][b] = b;
    }
  }

  for (int i = 1; i < 30; i++) {
    pair<mat, mat> temp = merge(g[i - 1], g[i - 1]);
    g[i] = temp.first;
    u[i] = temp.second;
  }
  int left = 1, right = K + 1, mid, si, ti;
  while (left < right) {
    mid = (left + right) / 2;
    if (calc(mid, si, ti) >= K) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }

  for (int i = 0; i < V; i++)
    g[0][i][i] = -INF;
  for (int i = 1; i < 30; i++) {
    pair<mat, mat> temp = merge(g[i - 1], g[i - 1]);
    g[i] = temp.first;
    u[i] = temp.second;
  }

  if (left == K + 1) {
    cout << -1 << endl;
  } else {
    cout << left << endl;
    if (left <= 100) {
      vec tab;
      tab.push_back(si);
      Dfs(tab, si, ti, left);
      tab.push_back(ti);

      int sum = 0;
      for (int i = 0; i < (int)tab.size(); i++) {

        if (i > 0) {
          int cost = G[tab[i - 1]][tab[i]];
          if (cost == -INF) {
            assert(0);
          } else {
            sum = add(sum, cost);
          }
        }

        if (i)
          cout << ' ';
        cout << tab[i];
      }
      cout << endl;
      cout << sum << ' ' << calc(left, si, ti) << endl;
      assert(sum == calc(left, si, ti));
    }
  }
  return 0;
}