Problem p04003 (C++)

Buggy submission (status: Runtime Error):
/*********************
 *  Author: xuziyuan  *
 *********************/

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

int n, m, x, y, z, len, vis[500010], dist[500010], lst[500010];
vector<int> all[1000010], g2[500010];
vector<pii> g[500010];
queue<int> q;

int main() {
  cin >> n >> m;
  len = n;
  rep(i, m) {
    scanf("%d%d%d", &x, &y, &z);
    g[x].pb(mpr(y, z));
    g[y].pb(mpr(x, z));
    all[z].pb(x);
    all[z].pb(y);
  }
  repn(i, n) sort(g[i].begin(), g[i].end());
  repn(i, 1e6) {
    if (all[i].size() == 0)
      continue;
    rep(j, all[i].size()) {
      if (vis[all[i][j]] == i)
        continue;
      q.push(all[i][j]);
      vis[all[i][j]] = i;
      vector<int> v;
      v.clear();
      v.pb(all[i][j]);
      while (!q.empty()) {
        int t = q.front();
        q.pop();
        while (lst[t] < g[t].size()) {
          // if(i==2) cout<<j<<' '<<all[i].size()<<endl;
          if (g[t][lst[t]].fi != i)
            break;
          if (vis[g[t][lst[t]].se] != i) {
            vis[g[t][lst[t]].se] = i;
            q.push(g[t][lst[t]].se);
            v.pb(g[t][lst[t]].se);
          }
          lst[t]++;
        }
      }
      len++;
      rep(k, v.size()) {
        g2[len].pb(v[k]);
        g2[v[k]].pb(len);
      }
    }
  }
  rep(i, 500005) dist[i] = -2;
  dist[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    rep(i, g2[t].size()) {
      if (dist[g2[t][i]] > -2)
        continue;
      dist[g2[t][i]] = dist[t] + 1;
      q.push(g2[t][i]);
    }
  }
  cout << dist[n] / 2 << endl;
  return 0;
}

Fixed submission:
/*********************
 *  Author: xuziyuan  *
 *********************/

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

int n, m, x, y, z, len, vis[500010], dist[500010], lst[500010];
vector<int> all[1000010], g2[500010];
vector<pii> g[500010];
queue<int> q;

int main() {
  cin >> n >> m;
  len = n;
  rep(i, m) {
    scanf("%d%d%d", &x, &y, &z);
    g[x].pb(mpr(z, y));
    g[y].pb(mpr(z, x));
    all[z].pb(x);
    all[z].pb(y);
  }
  repn(i, n) sort(g[i].begin(), g[i].end());
  repn(i, 1e6) {
    if (all[i].size() == 0)
      continue;
    rep(j, all[i].size()) {
      if (vis[all[i][j]] == i)
        continue;
      q.push(all[i][j]);
      vis[all[i][j]] = i;
      vector<int> v;
      v.clear();
      v.pb(all[i][j]);
      while (!q.empty()) {
        int t = q.front();
        q.pop();
        while (lst[t] < g[t].size()) {
          // if(i==2) cout<<j<<' '<<all[i].size()<<endl;
          if (g[t][lst[t]].fi != i)
            break;
          if (vis[g[t][lst[t]].se] != i) {
            vis[g[t][lst[t]].se] = i;
            q.push(g[t][lst[t]].se);
            v.pb(g[t][lst[t]].se);
          }
          lst[t]++;
        }
      }
      len++;
      rep(k, v.size()) {
        g2[len].pb(v[k]);
        g2[v[k]].pb(len);
      }
    }
  }
  rep(i, 500005) dist[i] = -2;
  dist[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    rep(i, g2[t].size()) {
      if (dist[g2[t][i]] > -2)
        continue;
      dist[g2[t][i]] = dist[t] + 1;
      q.push(g2[t][i]);
    }
  }
  cout << dist[n] / 2 << endl;
  return 0;
}