Problem p02323 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

#define MAX_N 16
int n;
int d[MAX_N][MAX_N];

int dp[1 << MAX_N][MAX_N];

int rec(int S, int v) { //??¢????????£??????????????????S??¨???????????????v

  if (dp[S][v] >=
      0) { //??¢?????????S????????????v??????????????????????????????
    return dp[S][v];
  }
  if (S == (1 << n) - 1 &&
      v ==
          0) { // S?????¨??????????????????????????¶?????§???????????????????????£?????????
    dp[S][v] = 0;
    return 0;
  }
  int res = INF;
  for (int u = 0; u < n; u++) {
    if (!(S >> u & 1)) { //??????u??????????????£??????????????¨???
      res = min(
          res,
          rec(S | 1 << u, u) +
              d[v]
               [u]); //??????u???S???????????????u->v????????????????????????????????´?????¨????????¨???res????????????
    }
  }
  dp[S][v] = res;
  // cout<<res<<endl;
  return res;
}
int main() {
  memset(dp, -1, sizeof(dp));

  int V, E;
  cin >> V >> E;
  n = V;
  REP(i, n) REP(j, n) d[i][j] = INF;
  REP(i, E) {
    int s, t, cost;
    cin >> s >> t >> cost;
    d[s][t] = cost;
  }
  int res = rec(0, 0);
  if (res < INF)
    cout << res << endl;
  else
    cout << -1 << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

#define MAX_N 20
int n;
int d[MAX_N][MAX_N];

int dp[1 << MAX_N][MAX_N];

int rec(int S, int v) { //??¢????????£??????????????????S??¨???????????????v

  if (dp[S][v] >=
      0) { //??¢?????????S????????????v??????????????????????????????
    return dp[S][v];
  }
  if (S == (1 << n) - 1 &&
      v ==
          0) { // S?????¨??????????????????????????¶?????§???????????????????????£?????????
    dp[S][v] = 0;
    return 0;
  }
  int res = INF;
  for (int u = 0; u < n; u++) {
    if (!(S >> u & 1)) { //??????u??????????????£??????????????¨???
      res = min(
          res,
          rec(S | 1 << u, u) +
              d[v]
               [u]); //??????u???S???????????????u->v????????????????????????????????´?????¨????????¨???res????????????
    }
  }
  dp[S][v] = res;
  // cout<<res<<endl;
  return res;
}
int main() {
  memset(dp, -1, sizeof(dp));

  int V, E;
  cin >> V >> E;
  n = V;
  REP(i, n) REP(j, n) d[i][j] = INF;
  REP(i, E) {
    int s, t, cost;
    cin >> s >> t >> cost;
    d[s][t] = cost;
  }
  int res = rec(0, 0);
  if (res < INF)
    cout << res << endl;
  else
    cout << -1 << endl;
  return 0;
}