Problem p01546 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi < " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(pachico, n) cout << " " << a[pachico];                                   \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(pachico, v.size()) cout << " " << v[pachico];                            \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(pachico, s) cout << " " << pachico;                                     \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 151;

template <typename T> class mat : public vector<vector<T>> {
private:
  int r, c; // 行,列
public:
  int row() const { return r; }
  int column() const { return c; }
  mat(int n, int m, T val = 0) {
    this->r = n, this->c = m;
    rep(i, n) { this->push_back(vector<T>(m, val)); }
  }
  mat operator+(const mat &another) {
    if (this->r != another.r && this->c != another.c) {
      cout << "足し算失敗(サイズ不一致)" << endl;
      exit(1);
    }
    mat<T> X(this->r, this->c);
    rep(i, this->r) {
      rep(j, this->c) { X[i][j] = (*this)[i][j] + another[i][j]; }
    }
    return X;
  }
  mat operator+(const T val) {
    mat<T> X(this->r, this->c);
    rep(i, this->r) {
      rep(j, this->c) { X[i][j] = (*this)[i][j] + val; }
    }
    return X;
  }
  mat operator-(const mat &another) {
    if (this->r != another.r && this->c != another.c) {
      cout << "引き算失敗(サイズ不一致)" << endl;
      exit(1);
    }
    mat<T> X(this->r, this->c);
    rep(i, this->r) {
      rep(j, this->c) { X[i][j] = (*this)[i][j] - another[i][j]; }
    }
    return X;
  }
  mat operator-(const T val) {
    mat<T> X(this->r, this->c);
    rep(i, this->r) {
      rep(j, this->c) { X[i][j] = (*this)[i][j] - val; }
    }
    return X;
  }
  vector<T> operator*(const vector<T> &another) {
    if (this->c != (int)another.size()) {
      cout << "掛け算失敗(サイズ不一致)" << endl;
      exit(1);
    }
    vector<T> vec(this->r, 0);
    rep(i, this->r) {
      rep(j, this->c) { vec[i] += (*this)[i][j] * another[j]; }
    }
    return vec;
  }
  mat operator*(const mat &another) {
    if (this->c != another.r) {
      cout << "掛け算失敗(サイズ不一致)" << endl;
      exit(1);
    }
    mat<T> X = *this;
    rep(i, this->r) {
      rep(k, this->c) {
        rep(j, another.c) {
          if ((*this)[i][k] && another[k][j]) {
            cmx(X[i][j], (*this)[i][k] + another[k][j]);
          }
        }
      }
    }
    return X;
  }
  mat operator-() {
    mat<T> X(this->r, this->c);
    rep(i, this->r) {
      rep(j, this->c) { X[i][j] = -(*this)[i][j]; }
    }
    return X;
  }
  void print() {
    rep(i, this->r) {
      rep(j, (this->c) - 1) { cout << (*this)[i][j] << ","; }
      cout << (*this)[i][(this->c) - 1] << endl;
    }
  }
};

template <typename T> mat<T> pow(mat<T> A, ll cnt) {
  if (A.row() != A.column()) {
    cout << "累乗不可" << endl;
  }
  mat<T> B = A;
  --cnt;
  while (cnt > 0) {
    if (cnt & 1) {
      B = B * A;
    }
    A = A * A;
    cnt >>= 1;
  }
  return B;
}

struct edge {
  int to, cost;
};

vector<edge> G[MAX_N];

bool possible(mat<int> &A, const int cri, const int K) {
  int n = A.row();
  mat<int> res = pow(A, cri);
  int mx = 0;
  rep(i, n) {
    rep(j, n) { cmx(mx, res[i][j]); }
  }
  return (mx >= K);
}

int mx[101][151];
int pre[101][151];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, K;
  cin >> n >> m >> K;
  mat<int> A(n, n);
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    cmx(A[a][b], c);
  }
  rep(i, n) {
    rep(j, n) {
      if (A[i][j]) {
        G[i].pb((edge){j, A[i][j]});
      }
    }
  }
  mat<int> res = pow(A, 100);
  if (!possible(A, K, K)) {
    cout << "-1\\n";
    return 0;
  }
  int l = 0, r = K;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (possible(A, mid, K)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << "\\n";
  if (r > 100) {
    return 0;
  }
  rep(i, r) {
    rep(j, n) {
      each(e, G[j]) {
        if (mx[i + 1][e.to] < mx[i][j] + e.cost) {
          mx[i + 1][e.to] = mx[i][j] + e.cost;
          pre[i + 1][e.to] = j;
        }
      }
    }
  }
  int cri = *max_element(mx[r], mx[r] + n);
  vector<int> ans;
  rep(i, n) {
    if (mx[r][i] == cri) {
      int nw = i;
      rrep(j, r + 1) {
        ans.pb(nw);
        nw = pre[j][nw];
      }
      reverse(all(ans));
      each(it, ans) { cout << it << " "; }
      cout << "\\n";
      break;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi < " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(pachico, n) cout << " " << a[pachico];                                   \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(pachico, v.size()) cout << " " << v[pachico];                            \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(pachico, s) cout << " " << pachico;                                     \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 151;

template <typename T> class mat : public vector<vector<T>> {
private:
  int r, c; // 行,列
public:
  int row() const { return r; }
  int column() const { return c; }
  mat(int n, int m, T val = 0) {
    this->r = n, this->c = m;
    rep(i, n) { this->push_back(vector<T>(m, val)); }
  }
  mat operator+(const mat &another) {
    if (this->r != another.r && this->c != another.c) {
      cout << "足し算失敗(サイズ不一致)" << endl;
      exit(1);
    }
    mat<T> X(this->r, this->c);
    rep(i, this->r) {
      rep(j, this->c) { X[i][j] = (*this)[i][j] + another[i][j]; }
    }
    return X;
  }
  mat operator+(const T val) {
    mat<T> X(this->r, this->c);
    rep(i, this->r) {
      rep(j, this->c) { X[i][j] = (*this)[i][j] + val; }
    }
    return X;
  }
  mat operator-(const mat &another) {
    if (this->r != another.r && this->c != another.c) {
      cout << "引き算失敗(サイズ不一致)" << endl;
      exit(1);
    }
    mat<T> X(this->r, this->c);
    rep(i, this->r) {
      rep(j, this->c) { X[i][j] = (*this)[i][j] - another[i][j]; }
    }
    return X;
  }
  mat operator-(const T val) {
    mat<T> X(this->r, this->c);
    rep(i, this->r) {
      rep(j, this->c) { X[i][j] = (*this)[i][j] - val; }
    }
    return X;
  }
  vector<T> operator*(const vector<T> &another) {
    if (this->c != (int)another.size()) {
      cout << "掛け算失敗(サイズ不一致)" << endl;
      exit(1);
    }
    vector<T> vec(this->r, 0);
    rep(i, this->r) {
      rep(j, this->c) { vec[i] += (*this)[i][j] * another[j]; }
    }
    return vec;
  }
  mat operator*(const mat &another) {
    if (this->c != another.r) {
      cout << "掛け算失敗(サイズ不一致)" << endl;
      exit(1);
    }
    mat<T> X(this->r, another.c);
    rep(i, this->r) {
      rep(k, this->c) {
        rep(j, another.c) {
          if (k == j || ((*this)[i][k] && another[k][j])) {
            cmx(X[i][j], (*this)[i][k] + another[k][j]);
          }
        }
      }
    }
    return X;
  }
  mat operator-() {
    mat<T> X(this->r, this->c);
    rep(i, this->r) {
      rep(j, this->c) { X[i][j] = -(*this)[i][j]; }
    }
    return X;
  }
  void print() {
    rep(i, this->r) {
      rep(j, (this->c) - 1) { cout << (*this)[i][j] << ","; }
      cout << (*this)[i][(this->c) - 1] << endl;
    }
  }
};

template <typename T> mat<T> pow(mat<T> A, ll cnt) {
  if (A.row() != A.column()) {
    cout << "累乗不可" << endl;
  }
  mat<T> B = A;
  --cnt;
  while (cnt > 0) {
    if (cnt & 1) {
      B = B * A;
    }
    A = A * A;
    cnt >>= 1;
  }
  return B;
}

struct edge {
  int to, cost;
};

vector<edge> G[MAX_N];

bool possible(mat<int> &A, const int cri, const int K) {
  int n = A.row();
  mat<int> res = pow(A, cri);
  int mx = 0;
  rep(i, n) {
    rep(j, n) { cmx(mx, res[i][j]); }
  }
  return (mx >= K);
}

int mx[101][151];
int pre[101][151];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, K;
  cin >> n >> m >> K;
  mat<int> A(n, n);
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    cmx(A[a][b], c);
  }
  rep(i, n) {
    rep(j, n) {
      if (A[i][j]) {
        G[i].pb((edge){j, A[i][j]});
      }
    }
  }
  mat<int> res = pow(A, 100);
  if (!possible(A, K, K)) {
    cout << "-1\\n";
    return 0;
  }
  int l = 0, r = K;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (possible(A, mid, K)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << "\\n";
  if (r > 100) {
    return 0;
  }
  rep(i, r) {
    rep(j, n) {
      each(e, G[j]) {
        if (mx[i + 1][e.to] < mx[i][j] + e.cost) {
          mx[i + 1][e.to] = mx[i][j] + e.cost;
          pre[i + 1][e.to] = j;
        }
      }
    }
  }
  int cri = *max_element(mx[r], mx[r] + n);
  vector<int> ans;
  rep(i, n) {
    if (mx[r][i] == cri) {
      int nw = i;
      rrep(j, r + 1) {
        ans.pb(nw);
        nw = pre[j][nw];
      }
      reverse(all(ans));
      each(it, ans) { cout << it << " "; }
      cout << "\\n";
      break;
    }
  }
  return 0;
}