Problem p03608 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
#define pr pair
#define mkp make_pair
#define lowbit(x) ((x) & (-x))
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MOD 1000000007

inline int scan() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void Out(int a) {
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a >= 10)
    Out(a / 10);
  putchar(a % 10 + '0');
}
#define MX 205
/**************************/

int n, m, R;
int ans;
int goal[MX];
int mp[MX][MX];
int G[25][25];
int dp[22][(1 << 22)];

void floyd() {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (mp[i][j] > mp[i][k] + mp[k][j])
          mp[i][j] = mp[i][k] + mp[k][j];
}

int main() {
  scanf("%d%d%d", &n, &m, &R);
  for (int i = 1; i <= R; i++)
    goal[i] = scan();
  memset(mp, 0x3f, sizeof(mp));
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    mp[a][b] = mp[b][a] = c;
  }

  floyd();
  for (int i = 1; i <= R; i++)
    for (int j = 1; j <= R; j++)
      G[i][j] = mp[goal[i]][goal[j]];

  memset(dp, 0x3f, sizeof(dp));
  for (int i = 1; i <= R; i++)
    dp[i - 1][(1 << (i - 1))] = 0;

  for (int i = 0; i < (1 << R); i++) // sta
    for (int j = 1; j <= R; j++)
      if ((1 << (j - 1)) & i)        // qi
        for (int k = 1; k <= R; k++) // zhong
          dp[k - 1][i | (1 << (k - 1))] =
              min(dp[k - 1][i | (1 << (k - 1))], dp[j - 1][i] + G[j][k]);

  int ans = INF;
  for (int i = 1; i <= R; i++)
    ans = min(ans, dp[i - 1][(1 << R) - 1]);
  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
#define pr pair
#define mkp make_pair
#define lowbit(x) ((x) & (-x))
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MOD 1000000007

inline int scan() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void Out(int a) {
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a >= 10)
    Out(a / 10);
  putchar(a % 10 + '0');
}
#define MX 205
/**************************/

int n, m, R;
int ans;
int goal[MX];
int mp[MX][MX];
int G[25][25];
int dp[15][(1 << 15)];

void floyd() {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (mp[i][j] > mp[i][k] + mp[k][j])
          mp[i][j] = mp[i][k] + mp[k][j];
}

int main() {
  scanf("%d%d%d", &n, &m, &R);
  for (int i = 1; i <= R; i++)
    goal[i] = scan();
  memset(mp, 0x3f, sizeof(mp));
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    mp[a][b] = mp[b][a] = c;
  }

  floyd();
  for (int i = 1; i <= R; i++)
    for (int j = 1; j <= R; j++)
      G[i][j] = mp[goal[i]][goal[j]];

  memset(dp, 0x3f, sizeof(dp));
  for (int i = 1; i <= R; i++)
    dp[i - 1][(1 << (i - 1))] = 0;

  for (int i = 0; i < (1 << R); i++) // sta
    for (int j = 1; j <= R; j++)
      if ((1 << (j - 1)) & i)        // qi
        for (int k = 1; k <= R; k++) // zhong
          dp[k - 1][i | (1 << (k - 1))] =
              min(dp[k - 1][i | (1 << (k - 1))], dp[j - 1][i] + G[j][k]);

  int ans = INF;
  for (int i = 1; i <= R; i++)
    ans = min(ans, dp[i - 1][(1 << R) - 1]);
  printf("%d\n", ans);
}