Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 3e5 + 7;
const int INF = 1e9 + 239;

int d[N];
vector<pair<int, int>> g[N];

set<int> sn[N];
map<pair<int, int>, int> ind;

int dij(int st, int en) {
  fill(d, d + N, INF);
  deque<int> q;
  d[st] = 0;
  q.push_back(st);
  while (!q.empty()) {
    int v = q.front();
    q.pop_front();
    for (auto t : g[v]) {
      if (d[t.first] > d[v] + t.second) {
        d[t.first] = d[v] + t.second;
        if (!t.second)
          q.push_front(t.first);
        else
          q.push_back(t.first);
      }
    }
  }
  return d[en];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    ind[{i, -1}] = i;
  }
  vector<tuple<int, int, int>> ed;
  while (m--) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    c--;
    sn[u].insert(c);
    sn[v].insert(c);
    ed.push_back({u, v, c});
  }
  int pnt = n;
  for (int i = 0; i < n; i++) {
    for (auto x : sn[i]) {
      ind[{i, x}] = pnt;
      pnt++;
    }
  }
  for (int i = 0; i < n; i++) {
    for (auto x : sn[i]) {
      g[ind[{i, -1}]].push_back({ind[{i, x}], 1});
      g[ind[{i, x}]].push_back({ind[{i, -1}], 1});
    }
  }
  for (auto t : ed) {
    int u, v, c;
    tie(u, v, c) = t;
    g[ind[{u, c}]].push_back({ind[{v, c}], 0});
    g[ind[{v, c}]].push_back({ind[{u, c}], 0});
  }
  int ans = dij(ind[{0, -1}], ind[{n - 1, -1}]);
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans / 2 << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 5e5 + 7;
const int INF = 1e9 + 239;

int d[N];
vector<pair<int, int>> g[N];

set<int> sn[N];
map<pair<int, int>, int> ind;

int dij(int st, int en) {
  fill(d, d + N, INF);
  deque<int> q;
  d[st] = 0;
  q.push_back(st);
  while (!q.empty()) {
    int v = q.front();
    q.pop_front();
    for (auto t : g[v]) {
      if (d[t.first] > d[v] + t.second) {
        d[t.first] = d[v] + t.second;
        if (!t.second)
          q.push_front(t.first);
        else
          q.push_back(t.first);
      }
    }
  }
  return d[en];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    ind[{i, -1}] = i;
  }
  vector<tuple<int, int, int>> ed;
  while (m--) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    c--;
    sn[u].insert(c);
    sn[v].insert(c);
    ed.push_back({u, v, c});
  }
  int pnt = n;
  for (int i = 0; i < n; i++) {
    for (auto x : sn[i]) {
      ind[{i, x}] = pnt;
      pnt++;
    }
  }
  for (int i = 0; i < n; i++) {
    for (auto x : sn[i]) {
      g[ind[{i, -1}]].push_back({ind[{i, x}], 1});
      g[ind[{i, x}]].push_back({ind[{i, -1}], 1});
    }
  }
  for (auto t : ed) {
    int u, v, c;
    tie(u, v, c) = t;
    g[ind[{u, c}]].push_back({ind[{v, c}], 0});
    g[ind[{v, c}]].push_back({ind[{u, c}], 0});
  }
  int ans = dij(ind[{0, -1}], ind[{n - 1, -1}]);
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans / 2 << endl;
  }
}