Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1234567;
int u[maxn], dist[maxn];
void init(int sz) {
  for (int i = 0; i < sz; i++)
    u[i] = i;
}
int find(int x) {
  if (x == u[x])
    return x;
  return u[x] = find(u[x]);
}
vector<pair<int, int>> e[maxn], ed[maxn];
int nds;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    ed[z].push_back({x, y});
  }
  nds = n + 1;
  for (int i = 0; i < maxn; i++) {
    if (ed[i].empty())
      continue;
    vector<int> now;
    for (auto ee : ed[i]) {
      now.push_back(ee.first);
      now.push_back(ee.second);
    }
    sort(now.begin(), now.end());
    now.erase(unique(now.begin(), now.end()), now.end());
    int len = now.size();
    init(len);
    for (auto ee : ed[i])
      u[find(lower_bound(now.begin(), now.end(), ee.first) - now.begin())] =
          find(lower_bound(now.begin(), now.end(), ee.second) - now.begin());
    vector<int> who(len);
    for (int i = 0; i < len; i++)
      who[i] = nds++;
    for (auto ee : ed[i]) {
      int x, y;
      tie(x, y) = ee;
      int par = who[find(x)];
      e[x].push_back({par, 1});
      e[y].push_back({par, 1});
      e[par].push_back({x, 0});
      e[par].push_back({y, 0});
    }
  }
  deque<pair<int, int>> q;
  memset(dist, -1, sizeof(dist));
  q.push_back({1, 0});
  dist[1] = 0;
  while (!q.empty()) {
    int cur, steps;
    tie(cur, steps) = q.front();
    q.pop_front();
    if (dist[cur] != steps)
      continue;
    if (cur == n)
      return cout << steps << endl, 0;
    for (auto ee : e[cur]) {
      if (dist[ee.first] == -1 || dist[ee.first] > dist[cur] + ee.second) {
        dist[ee.first] = dist[cur] + ee.second;
        if (ee.second)
          q.push_back({ee.first, dist[ee.first]});
        else
          q.push_front({ee.first, dist[ee.first]});
      }
    }
  }
  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1234567;
int u[maxn], dist[maxn];
void init(int sz) {
  for (int i = 0; i < sz; i++)
    u[i] = i;
}
int find(int x) {
  if (x == u[x])
    return x;
  return u[x] = find(u[x]);
}
vector<pair<int, int>> e[maxn], ed[maxn];
int nds;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    ed[z].push_back({x, y});
  }
  nds = n + 1;
  for (int i = 0; i < maxn; i++) {
    if (ed[i].empty())
      continue;
    vector<int> now;
    for (auto ee : ed[i]) {
      now.push_back(ee.first);
      now.push_back(ee.second);
    }
    sort(now.begin(), now.end());
    now.erase(unique(now.begin(), now.end()), now.end());
    int len = now.size();
    init(len);
    for (auto ee : ed[i])
      u[find(lower_bound(now.begin(), now.end(), ee.first) - now.begin())] =
          find(lower_bound(now.begin(), now.end(), ee.second) - now.begin());
    vector<int> who(len);
    for (int i = 0; i < len; i++)
      who[i] = nds++;
    for (auto ee : ed[i]) {
      int x, y;
      tie(x, y) = ee;
      int par = who[find(lower_bound(now.begin(), now.end(), x) - now.begin())];
      e[x].push_back({par, 1});
      e[y].push_back({par, 1});
      e[par].push_back({x, 0});
      e[par].push_back({y, 0});
    }
  }
  deque<pair<int, int>> q;
  memset(dist, -1, sizeof(dist));
  q.push_back({1, 0});
  dist[1] = 0;
  while (!q.empty()) {
    int cur, steps;
    tie(cur, steps) = q.front();
    q.pop_front();
    if (dist[cur] != steps)
      continue;
    if (cur == n)
      return cout << steps << endl, 0;
    for (auto ee : e[cur]) {
      if (dist[ee.first] == -1 || dist[ee.first] > dist[cur] + ee.second) {
        dist[ee.first] = dist[cur] + ee.second;
        if (ee.second)
          q.push_back({ee.first, dist[ee.first]});
        else
          q.push_front({ee.first, dist[ee.first]});
      }
    }
  }
  cout << -1 << endl;
  return 0;
}