Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define mem(a, b) memset(a, b, sizeof(a))
#define ll long long
#define ull unsigned long long
#define EPS 1e-8
using namespace std;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
const double pi = acos(-1.0);
int read() {
  int ret = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    ret = ret * 10 + ch - '0';
  return ret;
}
const int MAX_N = 205;
const int INF = 0x3f3f3f3f;
ll d[MAX_N][MAX_N];
int R[10];
bool vis[10];
int n, m, r, a, b;
ll c;
ll ans;
void floyd() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}
void dfs(int before, int t, ll tmp) {
  if (tmp >= ans) {
    return;
  }
  if (t == r) {
    ans = min(tmp, ans);
    return;
  }
  for (int i = 1; i <= r; i++) {
    if (!vis[R[i]]) {
      vis[R[i]] = true;
      if (before != 0)
        dfs(i, t + 1, tmp + d[R[i]][R[before]]);
      else {
        dfs(i, t + 1, tmp);
      }
      vis[R[i]] = false;
    }
  }
}
int main() {
#ifdef TEST
  freopen("input.txt", "r", stdin);
#endif
  scanf("%d%d%d", &n, &m, &r);
  mem(d, 0);
  mem(R, 0);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }
  }
  mem(vis, 0);
  ans = 1000000000000000000;
  for (int i = 1; i <= r; i++) {
    scanf("%d", R + i);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%lld", &a, &b, &c);
    d[b][a] = d[a][b] = c;
  }
  floyd();
  dfs(0, 0, 0);
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define mem(a, b) memset(a, b, sizeof(a))
#define ll long long
#define ull unsigned long long
#define EPS 1e-8
using namespace std;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
const double pi = acos(-1.0);
int read() {
  int ret = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    ret = ret * 10 + ch - '0';
  return ret;
}
const int MAX_N = 205;
const int INF = 0x3f3f3f3f;
ll d[MAX_N][MAX_N];
int R[10];
bool vis[MAX_N];
int n, m, r, a, b;
ll c;
ll ans;
void floyd() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}
void dfs(int before, int t, ll tmp) {
  if (tmp >= ans) {
    return;
  }
  if (t == r) {
    ans = min(tmp, ans);
    return;
  }
  for (int i = 1; i <= r; i++) {
    if (!vis[R[i]]) {
      vis[R[i]] = true;
      if (before != 0)
        dfs(i, t + 1, tmp + d[R[i]][R[before]]);
      else {
        dfs(i, t + 1, tmp);
      }
      vis[R[i]] = false;
    }
  }
}
int main() {
#ifdef TEST
  freopen("input.txt", "r", stdin);
#endif
  scanf("%d%d%d", &n, &m, &r);
  mem(d, 0);
  mem(R, 0);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }
  }
  mem(vis, 0);
  ans = 1000000000000000000;
  for (int i = 1; i <= r; i++) {
    scanf("%d", R + i);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%lld", &a, &b, &c);
    d[b][a] = d[a][b] = c;
  }
  floyd();
  dfs(0, 0, 0);
  printf("%lld\n", ans);
  return 0;
}