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

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STR(x)                                                                 \\
  string x;                                                                    \\
  cin >> x;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

int v, e;
int cost[20][20];
int memo[1 << 16][15][15];
int dp(const int bit, const int now, const int goal) {
  if (__builtin_popcount(bit) == v)
    return cost[now][goal];
  if (memo[bit][now][goal] != -1)
    return memo[bit][now][goal];
  int ans = INF;
  FOR(i, v) {
    if (bit & 1 << i || cost[now][i] < 0)
      continue;
    ans = min(ans, dp(bit | 1 << i, i, goal) + cost[now][i]);
  }
  return memo[bit][now][goal] = ans;
}

int main() {
  cin >> v >> e;
  FOR(i, 20) FOR(j, 20) cost[i][j] = INF;
  memset(memo, -1, sizeof memo);
  FOR(i, e) {
    int s, t, d;
    cin >> s >> t >> d;
    cost[s][t] = d;
  }
  int ans = INF;
  FOR(i, v) { ans = min(ans, dp(1 << i, i, i)); }
  if (ans <= (v + 1) * 1000)
    cout << ans << endl;
  else
    cout << -1 << endl;
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STR(x)                                                                 \\
  string x;                                                                    \\
  cin >> x;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

int v, e;
int cost[20][20];
int memo[1 << 16][16][16];
int dp(const int bit, const int now, const int goal) {
  if (__builtin_popcount(bit) == v)
    return cost[now][goal];
  if (memo[bit][now][goal] != -1)
    return memo[bit][now][goal];
  int ans = INF;
  FOR(i, v) {
    if (bit & 1 << i || cost[now][i] < 0)
      continue;
    ans = min(ans, dp(bit | 1 << i, i, goal) + cost[now][i]);
  }
  return memo[bit][now][goal] = ans;
}

int main() {
  cin >> v >> e;
  FOR(i, 20) FOR(j, 20) cost[i][j] = INF;
  memset(memo, -1, sizeof memo);
  FOR(i, e) {
    int s, t, d;
    cin >> s >> t >> d;
    cost[s][t] = d;
  }
  int ans = INF;
  FOR(i, v) { ans = min(ans, dp(1 << i, i, i)); }
  if (ans <= (v + 1) * 1000)
    cout << ans << endl;
  else
    cout << -1 << endl;
}