Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
struct edge {
  int to, cost;
};
static const int INF = 1000010000;
static const int NIL = -1;
static const int MOD = 1000000007;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort(v.begin(), v.end());
#define pb push_back
#define mp make_pair
#define np next_permutation
#define pq priority_queue

int gcd(int a, int b) {
  int c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int lcm(int a, int b) {
  int c = gcd(a, b);
  a /= c;
  return a * b;
}
int nCr(int a, int b) {
  int i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}
int pow(int a, int b) {
  int i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
long long llpow(long long a, long long b) {
  long long i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
long long llgcd(long long a, long long b) {
  long long c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
long long lllcm(long long a, long long b) {
  long long c = llgcd(a, b);
  a /= c;
  return a * b;
}
long long llnCr(long long a, long long b) {
  long long i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}

// int dx4[4] = {0,1,0,-1}, dy4[4] = {-1,0,1,0};
// int dx5[5] = {-1,0,0,0,1}, dy5[5] = {0,-1,0,1,0};
// int dx8[8] = {-1,0,1,1,1,0,-1,-1}, dy8[8] = {1,1,1,0,-1,-1,-1,0};
// int dx9[9] = {-1,0,1,1,1,0,-1,-1,0}, dy9[9] = {1,1,1,0,-1,-1,-1,0,0};

int V;
vector<edge> G[201];
int d[201];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V + 1, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    REP(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int n, m, R;
  cin >> n >> m >> R;
  V = n;
  vector<int> r;
  REP(i, R) {
    int tmp;
    cin >> tmp;
    r.pb(tmp);
  }
  SORT(r);
  REP(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    edge e;
    e.cost = c;
    e.to = b;
    G[a].pb(e);
    e.to = a;
    G[b].pb(e);
  }
  int dr[9][9];
  REP(i, R) {
    dijkstra(r[i]);
    REP(j, R) { dr[r[i]][r[j]] = d[r[j]]; }
  }
  int ans = INF;
  do {
    int cost = 0;
    REP(i, R - 1) { cost += dr[r[i]][r[i + 1]]; }
    ans = min(ans, cost);
  } while (next_permutation(r.begin(), r.end()));
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
struct edge {
  int to, cost;
};
static const int INF = 1000010000;
static const int NIL = -1;
static const int MOD = 1000000007;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort(v.begin(), v.end());
#define pb push_back
#define mp make_pair
#define np next_permutation
#define pq priority_queue

int gcd(int a, int b) {
  int c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int lcm(int a, int b) {
  int c = gcd(a, b);
  a /= c;
  return a * b;
}
int nCr(int a, int b) {
  int i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}
int pow(int a, int b) {
  int i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
long long llpow(long long a, long long b) {
  long long i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
long long llgcd(long long a, long long b) {
  long long c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
long long lllcm(long long a, long long b) {
  long long c = llgcd(a, b);
  a /= c;
  return a * b;
}
long long llnCr(long long a, long long b) {
  long long i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}

// int dx4[4] = {0,1,0,-1}, dy4[4] = {-1,0,1,0};
// int dx5[5] = {-1,0,0,0,1}, dy5[5] = {0,-1,0,1,0};
// int dx8[8] = {-1,0,1,1,1,0,-1,-1}, dy8[8] = {1,1,1,0,-1,-1,-1,0};
// int dx9[9] = {-1,0,1,1,1,0,-1,-1,0}, dy9[9] = {1,1,1,0,-1,-1,-1,0,0};

int V;
vector<edge> G[201];
int d[201];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V + 1, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    REP(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int n, m, R;
  cin >> n >> m >> R;
  V = n;
  vector<int> r;
  REP(i, R) {
    int tmp;
    cin >> tmp;
    r.pb(tmp);
  }
  SORT(r);
  REP(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    edge e;
    e.cost = c;
    e.to = b;
    G[a].pb(e);
    e.to = a;
    G[b].pb(e);
  }
  int dr[201][201];
  REP(i, R) {
    dijkstra(r[i]);
    REP(j, R) { dr[r[i]][r[j]] = d[r[j]]; }
  }
  int ans = INF;
  do {
    int cost = 0;
    REP(i, R - 1) { cost += dr[r[i]][r[i + 1]]; }
    ans = min(ans, cost);
  } while (next_permutation(r.begin(), r.end()));
  cout << ans << endl;
}