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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int n, m;
double table[128][128], memo[128][128];

double rec(int p, int prv) {
  if (p == 0)
    return 1.0;
  if (memo[p][prv] != -1)
    return memo[p][prv];

  double res = -1;
  for (int i = 0; i < n; i++)
    chmax(res, table[prv][i] * rec(p - 1, i));

  return memo[p][prv] = res;
}

int main() {
  int i, j;
  while (cin >> n >> m && n) {
    rep(i, n) rep(j, n) cin >> table[i][j];

    rep(i, 128) rep(j, 128) memo[i][j] = -1;

    double ans = -1;

    rep(i, n) chmax(ans, rec(m - 1, i));

    printf("%.2lf\\n", ans);
  }
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int n, m;
double table[128][128], memo[128][128];
double maxGrow[128];

double rec(int p, int prv) {
  if (p == m - 2)
    return maxGrow[prv];
  if (memo[p][prv] != -1)
    return memo[p][prv];

  int sld;
  double res = -1;
  for (int i = 0; i < n; i++) {
    double sub = rec(p + 1, i);

    if (res < sub) {
      res = sub;
      sld = i;
    }
  }

  return memo[p][prv] = table[prv][sld] * res;
}

int main() {
  int i, j, k;
  while (cin >> n >> m && n) {
    rep(i, n) rep(j, n) cin >> table[i][j];

    rep(i, 128) maxGrow[i] = *max_element(table[i], table[i] + 128);
    double ans = 0;

    rep(i, n) {
      rep(j, 128) rep(k, 128) memo[j][k] = -1;

      chmax(ans, rec(0, i));
    }

    printf("%.2f\\n", ans);
  }
}