Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int n, m;
const int maxN = 3e5 + 10;
vector<pair<int, int>> init[maxN];
vector<int> clr[maxN];
vector<pair<int, int>> g[maxN];
vector<int> id_clr[maxN];
pair<int, int> bck[maxN];
vector<int> trans[maxN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    clr[a].emplace_back(c);
    clr[b].emplace_back(c);
    g[a].emplace_back(b, c);
    g[b].emplace_back(a, c);
  }
  for (int i = 1; i <= n; i++) {
    sort(clr[i].begin(), clr[i].end());
    clr[i].erase(unique(clr[i].begin(), clr[i].end()), clr[i].end());
    id_clr[i].resize(clr[i].size());
  }
  int sz = n + 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < clr[i].size(); j++) {
      bck[sz] = make_pair(i, j);
      id_clr[i][j] = sz++;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (auto it : g[i]) {
      int c = it.second;
      int his_pos =
          lower_bound(clr[i].begin(), clr[i].end(), c) - clr[i].begin();
      trans[id_clr[i][his_pos]].emplace_back(
          id_clr[it.first]
                [lower_bound(clr[it.first].begin(), clr[it.first].end(), c) -
                 clr[it.first].begin()]);
    }
  }
  vector<int> dist(sz + 1, -1);
  deque<int> q;
  for (int v : id_clr[1]) {
    dist[v] = 1;
    q.push_front(v);
  }
  while (!q.empty()) {
    int v = q.front();
    q.pop_front();
    if (v <= n) {
      for (int id : id_clr[v]) {
        if (dist[id] == -1 || dist[id] > dist[v] + 1) {
          dist[id] = dist[v] + 1;
          q.push_back(id);
        }
      }
    } else {
      for (int p : trans[v]) {
        if (dist[p] == -1 || dist[p] > dist[v]) {
          dist[p] = dist[v];
          q.push_front(p);
        }
        if (dist[bck[p].first] == -1 || dist[bck[p].first] > dist[v]) {
          dist[bck[p].first] = dist[v];
          q.push_front(bck[p].first);
        }
      }
    }
  }
  int best = 1e9;
  for (int v : id_clr[n]) {
    if (dist[v] != -1)
      best = min(best, dist[v]);
  }
  if (dist[n] != -1)
    best = min(best, dist[n]);
  if (best > 1e7)
    best = -1;
  cout << best;
  return 0;
}

Fixed submission:
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int n, m;
const int maxN = 7e5 + 10;
vector<pair<int, int>> init[maxN];
vector<int> clr[maxN];
vector<pair<int, int>> g[maxN];
vector<int> id_clr[maxN];
pair<int, int> bck[maxN];
vector<int> trans[maxN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    clr[a].emplace_back(c);
    clr[b].emplace_back(c);
    g[a].emplace_back(b, c);
    g[b].emplace_back(a, c);
  }
  for (int i = 1; i <= n; i++) {
    sort(clr[i].begin(), clr[i].end());
    clr[i].erase(unique(clr[i].begin(), clr[i].end()), clr[i].end());
    id_clr[i].resize(clr[i].size());
  }
  int sz = n + 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < clr[i].size(); j++) {
      bck[sz] = make_pair(i, j);
      id_clr[i][j] = sz++;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (auto it : g[i]) {
      int c = it.second;
      int his_pos =
          lower_bound(clr[i].begin(), clr[i].end(), c) - clr[i].begin();
      trans[id_clr[i][his_pos]].emplace_back(
          id_clr[it.first]
                [lower_bound(clr[it.first].begin(), clr[it.first].end(), c) -
                 clr[it.first].begin()]);
    }
  }
  vector<int> dist(sz + 1, -1);
  deque<int> q;
  for (int v : id_clr[1]) {
    dist[v] = 1;
    q.push_front(v);
  }
  while (!q.empty()) {
    int v = q.front();
    q.pop_front();
    if (v <= n) {
      for (int id : id_clr[v]) {
        if (dist[id] == -1 || dist[id] > dist[v] + 1) {
          dist[id] = dist[v] + 1;
          q.push_back(id);
        }
      }
    } else {
      for (int p : trans[v]) {
        if (dist[p] == -1 || dist[p] > dist[v]) {
          dist[p] = dist[v];
          q.push_front(p);
        }
        if (dist[bck[p].first] == -1 || dist[bck[p].first] > dist[v]) {
          dist[bck[p].first] = dist[v];
          q.push_front(bck[p].first);
        }
      }
    }
  }
  int best = 1e9;
  for (int v : id_clr[n]) {
    if (dist[v] != -1)
      best = min(best, dist[v]);
  }
  if (dist[n] != -1)
    best = min(best, dist[n]);
  if (best > 1e7)
    best = -1;
  cout << best;
  return 0;
}