Problem p03608 (C++)

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

int n, m, r;
int travelCities[9];
vector<vector<pii>> adjList(4000);

int dd[9][9];

void dijk(int s) {
  vector<int> dist(n, 1000000000);
  dist[s] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push(pii(0, s));
  while (!pq.empty()) {
    pii fr = pq.top();
    pq.pop();
    int d = fr.first, u = fr.second;
    if (d > dist[u])
      continue;
    for (int j = 0; j < adjList[u].size(); ++j) {
      pii v = adjList[u][j];
      if (dist[u] + v.second < dist[v.first]) {
        dist[v.first] = dist[u] + v.second;
        pq.push(pii(dist[v.first], v.first));
      }
    }
  }
}

int visited[205];

int solve(int s, int nm) {
  if (nm == r - 1)
    return 0;
  int mn(1000000000);
  visited[travelCities[s]] = 1;
  for (int i = 0; i < r; ++i) {
    if (!visited[travelCities[i]] && i != s) {
      mn = min(mn, dd[travelCities[s]][travelCities[i]] + solve(i, nm + 1));
      visited[travelCities[i]] = 0;
    }
  }
  visited[travelCities[s]] = 0;
  return mn;
}

int main() {
  cin >> n >> m >> r;
  for (int i = 0; i < r; ++i)
    cin >> travelCities[i], travelCities[i]--;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    adjList[a].push_back(pii(b, c));
    adjList[b].push_back(pii(a, c));
  }

  for (int i = 0; i < r; ++i) {
    int s = travelCities[i];
    vector<int> dist(n, 1000000000);
    dist[s] = 0;
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    pq.push(pii(0, s));
    while (!pq.empty()) {
      pii fr = pq.top();
      pq.pop();
      int d = fr.first, u = fr.second;
      if (d > dist[u])
        continue;
      for (int j = 0; j < adjList[u].size(); ++j) {
        pii v = adjList[u][j];
        if (dist[u] + v.second < dist[v.first]) {
          dist[v.first] = dist[u] + v.second;
          pq.push(pii(dist[v.first], v.first));
        }
      }
    }
    for (int j = i + 1; j < r; ++j) {
      // cout << travelCities[i] << " " << travelCities[j] <<" : "<<
      // dist[travelCities[j]] << endl;
      dd[travelCities[i]][travelCities[j]] =
          dd[travelCities[j]][travelCities[i]] = dist[travelCities[j]];
    }
  }
  int mn = 1000000000;
  for (int i = 0; i < r; ++i) {
    mn = min(mn, solve(i, 0));
  }
  cout << mn;
  return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

int n, m, r;
int travelCities[9];
vector<vector<pii>> adjList(4000);

int dd[205][205];

void dijk(int s) {
  vector<int> dist(n, 1000000000);
  dist[s] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push(pii(0, s));
  while (!pq.empty()) {
    pii fr = pq.top();
    pq.pop();
    int d = fr.first, u = fr.second;
    if (d > dist[u])
      continue;
    for (int j = 0; j < adjList[u].size(); ++j) {
      pii v = adjList[u][j];
      if (dist[u] + v.second < dist[v.first]) {
        dist[v.first] = dist[u] + v.second;
        pq.push(pii(dist[v.first], v.first));
      }
    }
  }
}

int visited[205];

int solve(int s, int nm) {
  if (nm == r - 1)
    return 0;
  int mn(1000000000);
  visited[travelCities[s]] = 1;
  for (int i = 0; i < r; ++i) {
    if (!visited[travelCities[i]] && i != s) {
      mn = min(mn, dd[travelCities[s]][travelCities[i]] + solve(i, nm + 1));
      visited[travelCities[i]] = 0;
    }
  }
  visited[travelCities[s]] = 0;
  return mn;
}

int main() {
  cin >> n >> m >> r;
  for (int i = 0; i < r; ++i)
    cin >> travelCities[i], travelCities[i]--;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    adjList[a].push_back(pii(b, c));
    adjList[b].push_back(pii(a, c));
  }

  for (int i = 0; i < r; ++i) {
    int s = travelCities[i];
    vector<int> dist(n, 1000000000);
    dist[s] = 0;
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    pq.push(pii(0, s));
    while (!pq.empty()) {
      pii fr = pq.top();
      pq.pop();
      int d = fr.first, u = fr.second;
      if (d > dist[u])
        continue;
      for (int j = 0; j < adjList[u].size(); ++j) {
        pii v = adjList[u][j];
        if (dist[u] + v.second < dist[v.first]) {
          dist[v.first] = dist[u] + v.second;
          pq.push(pii(dist[v.first], v.first));
        }
      }
    }
    for (int j = i + 1; j < r; ++j) {
      // cout << travelCities[i] << " " << travelCities[j] <<" : "<<
      // dist[travelCities[j]] << endl;
      dd[travelCities[i]][travelCities[j]] =
          dd[travelCities[j]][travelCities[i]] = dist[travelCities[j]];
    }
  }
  int mn = 1000000000;
  for (int i = 0; i < r; ++i) {
    mn = min(mn, solve(i, 0));
  }
  cout << mn;
  return 0;
}