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

#include <bits/stdc++.h>
#include <cmath>
#include <string.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define max3(A, B, C) max((A), max((B), (C)))
#define lb lower_bound
#define ub upper_bound

using namespace std;

const int N = 15;
const int INF = 0x3f3f3f3f;

int pd[1 << N][N][N];

int main(void) {
  int n, m;
  int adj[N][N];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      adj[i][j] = (i == j ? 0 : INF);
    }
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      pd[0][i][j] = 0;
  for (int i = 1; i < 1 << N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        pd[i][j][k] = INF;
      }
    }
  }

  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    adj[a][b] = c;
  }

  for (int i = 1; i < 1 << n; i++) {
    for (int j = 0; j < n; j++) {
      if ((i & (1 << j)) == 0)
        continue;
      for (int w = 0; w < n; w++) {
        if ((i & (1 << w)) == 0)
          continue;
        for (int k = 0; k < n; k++) {
          if (i & (1 << k)) {
            if (i - k != 0 || k == w) {
              pd[i][j][w] =
                  min(pd[i][j][w], pd[i ^ (1 << j)][k][w] + adj[k][j]);
            }
          }
        }
      }
    }
  }

  int minn = INF;
  int idx = (1 << n) - 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      minn = min(minn, pd[idx][i][j] + adj[i][j]);
    }
  }

  if (minn == INF)
    puts("-1");
  else
    printf("%d\\n", minn);

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <string.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define max3(A, B, C) max((A), max((B), (C)))
#define lb lower_bound
#define ub upper_bound

using namespace std;

const int N = 16;
const int INF = 0x3f3f3f3f;

int pd[1 << N][N][N];

int main(void) {
  int n, m;
  int adj[N][N];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      adj[i][j] = (i == j ? 0 : INF);
    }
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      pd[0][i][j] = 0;
  for (int i = 1; i < 1 << N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        pd[i][j][k] = INF;
      }
    }
  }

  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    adj[a][b] = c;
  }

  for (int i = 1; i < 1 << n; i++) {
    for (int j = 0; j < n; j++) {
      if ((i & (1 << j)) == 0)
        continue;
      for (int w = 0; w < n; w++) {
        if ((i & (1 << w)) == 0)
          continue;
        for (int k = 0; k < n; k++) {
          if (i & (1 << k)) {
            if (i - k != 0 || k == w) {
              pd[i][j][w] =
                  min(pd[i][j][w], pd[i ^ (1 << j)][k][w] + adj[k][j]);
            }
          }
        }
      }
    }
  }

  int minn = INF;
  int idx = (1 << n) - 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      minn = min(minn, pd[idx][i][j] + adj[i][j]);
    }
  }

  if (minn == INF)
    puts("-1");
  else
    printf("%d\\n", minn);

  return 0;
}