Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
int n, m, r, b[9], x, y, z, d[201][201], c[9], res = INT_MAX;
typedef pair<int, int> ii;
vector<ii> a[1001];
priority_queue<ii> qu;
void dijisktra(int u) {
  qu.push(ii(0, u));
  d[u][u] = 0;
  while (qu.size()) {
    int u1 = qu.top().second, du = qu.top().first;
    qu.pop();
    if (du != d[u][u1])
      continue;
    for (int i = 0; int v = a[u1][i].first; ++i) {
      int uv = a[u1][i].second;
      if (d[u][v] > du + uv) {
        d[u][v] = du + uv;
        qu.push(ii(d[u][v], v));
      }
    }
  }
}
bool kt[9];
void kiemtra() {
  int tam = 0;
  // for(int i=1;i<=r;++i)cout<<c[i]<< " ";cout<<endl;
  for (int i = 2; i <= r; ++i)
    tam += d[c[i - 1]][c[i]];
  // cout<<tam<<endl;
  res = min(res, tam);
}
void sinhdaynhiphan(int k) {
  for (int i = 1; i <= r; ++i)
    if (!kt[i]) {
      kt[i] = true;
      c[k] = b[i];
      if (k == r)
        kiemtra();
      else
        sinhdaynhiphan(k + 1);
      kt[i] = false;
    }
}
int main() {
  freopen("main.inp", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> r;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      d[i][j] = INT_MAX;
  for (int i = 1; i <= r; ++i)
    cin >> b[i];
  // for(int i=1;i<=r;++i)cout<<b[i]<<" ";
  // for(int i=1;i<=n;++i){cout<<i<<":  ";for(int j=1;j<=n;++j)cout<<d[i][j]<<"
  // ";cout<<endl;}
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y >> z;
    a[x].push_back(ii(y, z));
    a[y].push_back(ii(x, z));
  }
  for (int i = 1; i <= n; ++i)
    a[i].push_back(ii(0, 0));
  for (int i = 1; i <= n; ++i)
    dijisktra(i);
  // for(int i=1;i<=n;++i){cout<<i<<":  ";for(int j=1;j<=n;++j)cout<<d[i][j]<<"
  // ";cout<<endl;}
  sinhdaynhiphan(1);
  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
int n, m, r, b[9], x, y, z, d[201][201], c[9], res = INT_MAX;
typedef pair<int, int> ii;
vector<ii> a[1001];
priority_queue<ii> qu;
void dijisktra(int u) {
  qu.push(ii(0, u));
  d[u][u] = 0;
  while (qu.size()) {
    int u1 = qu.top().second, du = qu.top().first;
    qu.pop();
    if (du != d[u][u1])
      continue;
    for (int i = 0; int v = a[u1][i].first; ++i) {
      int uv = a[u1][i].second;
      if (d[u][v] > du + uv) {
        d[u][v] = du + uv;
        qu.push(ii(d[u][v], v));
      }
    }
  }
}
bool kt[9];
void kiemtra() {
  int tam = 0;
  // for(int i=1;i<=r;++i)cout<<c[i]<< " ";cout<<endl;
  for (int i = 2; i <= r; ++i)
    tam += d[c[i - 1]][c[i]];
  // cout<<tam<<endl;
  res = min(res, tam);
}
void sinhdaynhiphan(int k) {
  for (int i = 1; i <= r; ++i)
    if (!kt[i]) {
      kt[i] = true;
      c[k] = b[i];
      if (k == r)
        kiemtra();
      else
        sinhdaynhiphan(k + 1);
      kt[i] = false;
    }
}
int main() {
  // freopen("main.inp","r",stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> r;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      d[i][j] = INT_MAX;
  for (int i = 1; i <= r; ++i)
    cin >> b[i];
  // for(int i=1;i<=r;++i)cout<<b[i]<<" ";
  // for(int i=1;i<=n;++i){cout<<i<<":  ";for(int j=1;j<=n;++j)cout<<d[i][j]<<"
  // ";cout<<endl;}
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y >> z;
    a[x].push_back(ii(y, z));
    a[y].push_back(ii(x, z));
  }
  for (int i = 1; i <= n; ++i)
    a[i].push_back(ii(0, 0));
  for (int i = 1; i <= n; ++i)
    dijisktra(i);
  // for(int i=1;i<=n;++i){cout<<i<<":  ";for(int j=1;j<=n;++j)cout<<d[i][j]<<"
  // ";cout<<endl;}
  sinhdaynhiphan(1);
  cout << res;
  return 0;
}