Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#define TEST_MODE true

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= (int)b; --i)
#define range(i, a, b, c) for (int i = a; c > 0 ? i < b : i > b; i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define int long long

#ifdef LOCAL_ENV

#if TEST_MODE == true
const bool test = true;
#define dump(x) cerr << #x << " = " << (x) << " "
#define dumpl(x) dump(x) << endl
#else
const bool test = false;
#define dump(x)
#define dumpl(x)
#endif

#else
const bool test = false;
#define dump(x)
#define dumpl(x)
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = (int)1 << 60;
const ll INFL = (ll)1 << 60;
ll mod_n = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

template <class InputIterator> int sum(InputIterator begin, InputIterator end) {
  return accumulate(begin, end, 0ll);
}

template <typename T> T gcd(T a, T b) {
  T c;
  while (a != 0) {
    c = a;
    a = b % a;
    b = c;
  }
  return b;
}

template <typename T> T bit_count(T N) {
  T cnt = 0;
  while (N) {
    if (N & 1)
      cnt++;
    N >>= 1;
  }
  return cnt;
}

template <typename T> void print_vec(ostream &ostream, vector<T> a) {
  rep(i, a.size() - 1) ostream << a[i] << " ";
  ostream << a.back() << endl;
}

struct Point {
  int y, x;
  bool operator<(const Point &r) {
    if (x < r.x)
      return true;
    else
      return false;
  }
};

class Solver {
public:
  int H, W;
  int N, M, R;
  vector<int> r;

  struct Vertex {
    int id, dist, mask;
    bool operator>(const Vertex &r) const { return dist > r.dist; }
  };

  struct Edge {
    int to, weight;
    Edge(int to, int weight) : to(to), weight(weight){};
  };

  using Graph = vector<vector<Edge>>;

  using Weight = int;

  Graph g;
  int r_rev[205];
  int d[205][1 << 9];
  int start_id, goal_id;

  int dijkstra(int start_id) {
    rep(i, 205) rep(j, 1 << 9) { d[i][j] = INF; }
    priority_queue<Vertex, vector<Vertex>, greater<Vertex>> q;
    Vertex start;
    start.id = start_id;
    start.dist = 0;
    {
      int id = r_rev[start_id];
      if (id != -1) {
        d[start_id][1 << id] = 0;
        start.mask = 1 << id;
      }
    }
    for (q.push(start); !q.empty();) {
      Vertex p = q.top();
      q.pop();
      for (auto f : g[p.id]) {
        Vertex next_p;
        next_p.id = f.to;
        next_p.dist = p.dist + f.weight;
        next_p.mask = p.mask;
        {
          int id = r_rev[f.to];
          if (id != -1)
            next_p.mask |= 1 << id;
        }
        if (d[next_p.id][next_p.mask] > next_p.dist) {
          d[next_p.id][next_p.mask] = next_p.dist;
          q.push(next_p);
        }
      }
    }
    // rep(i, N) rep(mask, 1 << R) {
    //	cerr << i << " " << std::bitset<3>(mask)
    //		<< " "<< d[i][mask] << endl;
    // }
    int ans = INF;
    rep(i, R) { chmin(ans, d[r[i]][(1 << R) - 1]); }
    return ans;
  }

  bool grid_ok(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }

  void solve() {
    cin >> N >> M >> R;
    r.resize(R);
    rep(i, N) { r_rev[i] = -1; }
    rep(i, R) {
      cin >> r[i];
      r[i]--;
      r_rev[r[i]] = i;
    }
    g.resize(M);
    rep(i, M) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      g[a].push_back(Edge(b, c));
      g[b].push_back(Edge(a, c));
    }

    int ans = INF;
    rep(i, R) {
      int ret = dijkstra(r[i]);
      chmin(ans, ret);
    }
    cout << ans << endl;
  }
};

signed main() {
  srand((unsigned int)time(NULL));
  cout << fixed << setprecision(20);
  auto ptr = new Solver();
  ptr->solve();
  delete ptr;
  // while (true) {
  //	if (cin.eof() == true) break;
  //	auto ptr = new Solver();
  //	ptr->solve();
  //	delete ptr;
  // }
  return 0;
}


Fixed submission:
#define TEST_MODE true

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= (int)b; --i)
#define range(i, a, b, c) for (int i = a; c > 0 ? i < b : i > b; i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define int long long

#ifdef LOCAL_ENV

#if TEST_MODE == true
const bool test = true;
#define dump(x) cerr << #x << " = " << (x) << " "
#define dumpl(x) dump(x) << endl
#else
const bool test = false;
#define dump(x)
#define dumpl(x)
#endif

#else
const bool test = false;
#define dump(x)
#define dumpl(x)
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = (int)1 << 60;
const ll INFL = (ll)1 << 60;
ll mod_n = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

template <class InputIterator> int sum(InputIterator begin, InputIterator end) {
  return accumulate(begin, end, 0ll);
}

template <typename T> T gcd(T a, T b) {
  T c;
  while (a != 0) {
    c = a;
    a = b % a;
    b = c;
  }
  return b;
}

template <typename T> T bit_count(T N) {
  T cnt = 0;
  while (N) {
    if (N & 1)
      cnt++;
    N >>= 1;
  }
  return cnt;
}

template <typename T> void print_vec(ostream &ostream, vector<T> a) {
  rep(i, a.size() - 1) ostream << a[i] << " ";
  ostream << a.back() << endl;
}

struct Point {
  int y, x;
  bool operator<(const Point &r) {
    if (x < r.x)
      return true;
    else
      return false;
  }
};

class Solver {
public:
  int H, W;
  int N, M, R;
  vector<int> r;

  struct Vertex {
    int id, dist, mask;
    bool operator>(const Vertex &r) const { return dist > r.dist; }
  };

  struct Edge {
    int to, weight;
    Edge(int to, int weight) : to(to), weight(weight){};
  };

  using Graph = vector<vector<Edge>>;

  using Weight = int;

  Graph g;
  int r_rev[205];
  int d[205][1 << 9];
  int start_id, goal_id;

  int dijkstra(int start_id) {
    rep(i, 205) rep(j, 1 << 9) { d[i][j] = INF; }
    priority_queue<Vertex, vector<Vertex>, greater<Vertex>> q;
    Vertex start;
    start.id = start_id;
    start.dist = 0;
    {
      int id = r_rev[start_id];
      if (id != -1) {
        d[start_id][1 << id] = 0;
        start.mask = 1 << id;
      }
    }
    for (q.push(start); !q.empty();) {
      Vertex p = q.top();
      q.pop();
      for (auto f : g[p.id]) {
        Vertex next_p;
        next_p.id = f.to;
        next_p.dist = p.dist + f.weight;
        next_p.mask = p.mask;
        {
          int id = r_rev[f.to];
          if (id != -1)
            next_p.mask |= 1 << id;
        }
        if (d[next_p.id][next_p.mask] > next_p.dist) {
          d[next_p.id][next_p.mask] = next_p.dist;
          q.push(next_p);
        }
      }
    }
    // rep(i, N) rep(mask, 1 << R) {
    //	cerr << i << " " << std::bitset<3>(mask)
    //		<< " "<< d[i][mask] << endl;
    // }
    int ans = INF;
    rep(i, R) { chmin(ans, d[r[i]][(1 << R) - 1]); }
    return ans;
  }

  bool grid_ok(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }

  void solve() {
    cin >> N >> M >> R;
    r.resize(R);
    rep(i, N) { r_rev[i] = -1; }
    rep(i, R) {
      cin >> r[i];
      r[i]--;
      r_rev[r[i]] = i;
    }
    g.resize(N);
    rep(i, M) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      g[a].push_back(Edge(b, c));
      g[b].push_back(Edge(a, c));
    }

    int ans = INF;
    rep(i, R) {
      int ret = dijkstra(r[i]);
      chmin(ans, ret);
    }
    cout << ans << endl;
  }
};

signed main() {
  srand((unsigned int)time(NULL));
  cout << fixed << setprecision(20);
  auto ptr = new Solver();
  ptr->solve();
  delete ptr;
  // while (true) {
  //	if (cin.eof() == true) break;
  //	auto ptr = new Solver();
  //	ptr->solve();
  //	delete ptr;
  // }
  return 0;
}