Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template <class T> ostream &prnt(ostream &out, T v) {
  out << v.size() << '\n';
  for (auto e : v)
    out << e << ' ';
  return out;
}
template <class T> ostream &operator<<(ostream &out, vector<T> v) {
  return prnt(out, v);
}
template <class T> ostream &operator<<(ostream &out, set<T> v) {
  return prnt(out, v);
}
template <class T1, class T2> ostream &operator<<(ostream &out, map<T1, T2> v) {
  return prnt(out, v);
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
  return out << '(' << p.st << ' ' << p.nd << ')';
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n)                                                            \
  do {                                                                         \
    cerr << #x "[]: ";                                                         \
    for (int _ = 0; _ < n; ++_)                                                \
      cerr << x[_] << " ";                                                     \
    cerr << '\n';                                                              \
  } while (0)
#define dbg_ok cerr << "OK!\n"

const int N = 100100;
vector<pair<int, int>> v[3 * N];
int n, d[3 * N], m, a, b, c;
map<pair<int, int>, int> mp;

int get(int node, int col) {
  if (mp.count({node, col}))
    return mp[{node, col}];
  int k = mp.size();
  mp[{node, col}] = k + n + 1;
  v[node].push_back({k + n + 1, 1});
  v[k + n + 1].push_back({node, 0});
  return mp[{node, col}];
}

int main() {
  ios_base::sync_with_stdio(false);

  cin >> n >> m;

  for (int i = 1; i <= m; i++) {
    cin >> a >> b >> c;
    int n1 = get(a, c);
    int n2 = get(b, c);
    v[n1].push_back({n2, 0});
    v[n2].push_back({n1, 0});
  }

  memset(d, 0x3f, sizeof d);
  priority_queue<pair<int, int>> q;
  q.push({0, 1});
  d[1] = 0;

  while (!q.empty()) {
    int x = q.top().nd;
    q.pop();

    for (auto i : v[x])
      if (d[i.st] > d[x] + i.nd) {
        d[i.st] = d[x] + i.nd;
        q.push({-d[i.st], i.st});
      }
  }
  // dbg_v(d, 3 * n)
  if (d[n] > 1e7)
    d[n] = -1;
  cout << d[n] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template <class T> ostream &prnt(ostream &out, T v) {
  out << v.size() << '\n';
  for (auto e : v)
    out << e << ' ';
  return out;
}
template <class T> ostream &operator<<(ostream &out, vector<T> v) {
  return prnt(out, v);
}
template <class T> ostream &operator<<(ostream &out, set<T> v) {
  return prnt(out, v);
}
template <class T1, class T2> ostream &operator<<(ostream &out, map<T1, T2> v) {
  return prnt(out, v);
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
  return out << '(' << p.st << ' ' << p.nd << ')';
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n)                                                            \
  do {                                                                         \
    cerr << #x "[]: ";                                                         \
    for (int _ = 0; _ < n; ++_)                                                \
      cerr << x[_] << " ";                                                     \
    cerr << '\n';                                                              \
  } while (0)
#define dbg_ok cerr << "OK!\n"

const int N = 200100;
vector<pair<int, int>> v[3 * N];
int n, d[3 * N], m, a, b, c;
map<pair<int, int>, int> mp;

int get(int node, int col) {
  if (mp.count({node, col}))
    return mp[{node, col}];
  int k = mp.size();
  mp[{node, col}] = k + n + 1;
  v[node].push_back({k + n + 1, 1});
  v[k + n + 1].push_back({node, 0});
  return mp[{node, col}];
}

int main() {
  ios_base::sync_with_stdio(false);

  cin >> n >> m;

  for (int i = 1; i <= m; i++) {
    cin >> a >> b >> c;
    int n1 = get(a, c);
    int n2 = get(b, c);
    v[n1].push_back({n2, 0});
    v[n2].push_back({n1, 0});
  }

  memset(d, 0x3f, sizeof d);
  priority_queue<pair<int, int>> q;
  q.push({0, 1});
  d[1] = 0;

  while (!q.empty()) {
    int x = q.top().nd;
    q.pop();

    for (auto i : v[x])
      if (d[i.st] > d[x] + i.nd) {
        d[i.st] = d[x] + i.nd;
        q.push({-d[i.st], i.st});
      }
  }
  // dbg_v(d, 3 * n)
  if (d[n] > 1e7)
    d[n] = -1;
  cout << d[n] << '\n';
}