Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PI 4 * atan(1.0)
typedef long long ll;
typedef long double ld;
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
const int SIZE = 205;
// int arr[SIZE]={};
// ここまでテンプレ
vector<pair<int, int>> node[10];
int N, M, R;
vector<int> RR;
int v[201] = {};
bool done[10] = {};
int dfs(int s, int ans);
int main() {
  cin >> N >> M >> R;
  int d[SIZE][SIZE] = {};
  for (int i = 0; i < R; i++) {
    int r;
    cin >> r;
    r--;
    v[r] = i;
    RR.push_back(r);
  }
  for (int i = 0; i < SIZE; i++)
    for (int j = 0; j < SIZE; j++) {
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }
  for (int i = 0; i < M; i++) {
    int A, B, C;
    cin >> A >> B >> C;
    A--, B--;
    d[A][B] = C;
    d[B][A] = C;
  }
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < R; j++) {
      if (i != RR[j])
        node[i].push_back(make_pair(RR[j], d[i][RR[j]]));
    }
  }
  int mini = INF;
  for (int i = 0; i < R; i++) {
    done[v[RR[i]]] = 1;
    mini = min(mini, dfs(RR[i], 0));
    done[v[RR[i]]] = 0;
  }
  cout << mini << endl;
  return 0;
}
int dfs(int s, int ans) {
  int mini = INF;
  for (int i = 0; i < node[s].size(); i++) {
    int next = node[s][i].first;
    int d = node[s][i].second;
    if (done[v[next]] == 0) {
      // cout<<v[next]<<" "<<done[v[next]]<<endl;
      done[v[next]] = 1;
      ans += d;
      mini = min(mini, dfs(next, ans));
      done[v[next]] = 0;
      ans -= d;
    }
  }
  if (mini < INF) {
    return mini;
  } else
    return ans;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PI 4 * atan(1.0)
typedef long long ll;
typedef long double ld;
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
const int SIZE = 205;
// int arr[SIZE]={};
// ここまでテンプレ
vector<pair<int, int>> node[SIZE];
int N, M, R;
vector<int> RR;
int v[201] = {};
bool done[10] = {};
int dfs(int s, int ans);
int main() {
  cin >> N >> M >> R;
  int d[SIZE][SIZE] = {};
  for (int i = 0; i < R; i++) {
    int r;
    cin >> r;
    r--;
    v[r] = i;
    RR.push_back(r);
  }
  for (int i = 0; i < SIZE; i++)
    for (int j = 0; j < SIZE; j++) {
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }
  for (int i = 0; i < M; i++) {
    int A, B, C;
    cin >> A >> B >> C;
    A--, B--;
    d[A][B] = C;
    d[B][A] = C;
  }
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < R; j++) {
      if (i != RR[j])
        node[i].push_back(make_pair(RR[j], d[i][RR[j]]));
    }
  }
  int mini = INF;
  for (int i = 0; i < R; i++) {
    done[v[RR[i]]] = 1;
    mini = min(mini, dfs(RR[i], 0));
    done[v[RR[i]]] = 0;
  }
  cout << mini << endl;
  return 0;
}
int dfs(int s, int ans) {
  int mini = INF;
  for (int i = 0; i < node[s].size(); i++) {
    int next = node[s][i].first;
    int d = node[s][i].second;
    if (done[v[next]] == 0) {
      // cout<<v[next]<<" "<<done[v[next]]<<endl;
      done[v[next]] = 1;
      ans += d;
      mini = min(mini, dfs(next, ans));
      done[v[next]] = 0;
      ans -= d;
    }
  }
  if (mini < INF) {
    return mini;
  } else
    return ans;
}