Problem p04003 (C++)

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

using namespace std;

#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(), (v).end()
#define one first
#define two second
typedef long long ll;
typedef pair<int, int> pi;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll * INF * INF;

const int MAX_N = 3e5 + 10;

int nN, M;
vector<pi> Ed[MAX_N];
map<int, vector<int>> G[MAX_N];
map<int, int> Vc[MAX_N];
int Vix[MAX_N];
int N;

int main() {
  cin >> nN >> M;
  for (int i = 0; i < M; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    G[x][c].push_back(y);
    G[y][c].push_back(x);
  }
  for (int i = 1; i <= nN; i++) {
    Vix[i] = N++;
    for (auto &val : G[i])
      Vc[i][val.one] = N++;
  }

  int St = Vix[1], En = Vix[nN];
  for (int i = 1; i <= nN; i++) {
    for (auto &val : Vc[i]) {
      int c, v;
      tie(c, v) = val;
      for (int &x : G[i][c]) {
        int w = Vc[x][c];
        Ed[v].emplace_back(w, 0);
      }
      Ed[v].emplace_back(Vix[i], 0);
      Ed[Vix[i]].emplace_back(v, 1);
    }
    // printf("[%d -> %d]\n", i, Vix[i]);
  }

  vector<int> dis(N, INF);
  vector<bool> vis(N, 0);
  deque<int> Q;
  Q.push_front(St);
  dis[St] = 0;
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop_front();
    if (vis[v])
      continue;
    vis[v] = true;
    // printf("%d -> %d\n", v, dis[v]);
    for (auto &val : Ed[v]) {
      int w, d;
      tie(w, d) = val;
      if (!vis[w] && dis[w] > dis[v] + d)
        (d == 0 ? Q.push_front(w) : Q.push_back(w)), dis[w] = dis[v] + d;
    }
  }
  printf("%d\n", dis[En] == INF ? -1 : dis[En]);
  return 0;
}


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

using namespace std;

#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(), (v).end()
#define one first
#define two second
typedef long long ll;
typedef pair<int, int> pi;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll * INF * INF;

const int MAX_N = (2e5) * 2 + (1e5) + 100;

int nN, M;
vector<pi> Ed[MAX_N];
map<int, vector<int>> G[MAX_N];
map<int, int> Vc[MAX_N];
int Vix[MAX_N];
int N;

int main() {
  cin >> nN >> M;
  for (int i = 0; i < M; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    G[x][c].push_back(y);
    G[y][c].push_back(x);
  }
  for (int i = 1; i <= nN; i++) {
    Vix[i] = N++;
    for (auto &val : G[i])
      Vc[i][val.one] = N++;
  }

  int St = Vix[1], En = Vix[nN];
  for (int i = 1; i <= nN; i++) {
    for (auto &val : Vc[i]) {
      int c, v;
      tie(c, v) = val;
      for (int &x : G[i][c]) {
        int w = Vc[x][c];
        Ed[v].emplace_back(w, 0);
      }
      Ed[v].emplace_back(Vix[i], 0);
      Ed[Vix[i]].emplace_back(v, 1);
    }
    // printf("[%d -> %d]\n", i, Vix[i]);
  }

  vector<int> dis(N, INF);
  vector<bool> vis(N, 0);
  deque<int> Q;
  Q.push_front(St);
  dis[St] = 0;
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop_front();
    if (vis[v])
      continue;
    vis[v] = true;
    // printf("%d -> %d\n", v, dis[v]);
    for (auto &val : Ed[v]) {
      int w, d;
      tie(w, d) = val;
      if (!vis[w] && dis[w] > dis[v] + d)
        (d == 0 ? Q.push_front(w) : Q.push_back(w)), dis[w] = dis[v] + d;
    }
  }
  printf("%d\n", dis[En] == INF ? -1 : dis[En]);
  return 0;
}