Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn = 200010;
int dist[maxn];
vector<pair<int, int>> g[maxn];
vector<int> newg[maxn];
vector<int> all[maxn];
int was[maxn], x[maxn], ptr[maxn];
int main() {
  int n, m, i, j, k;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    int p, q, c;
    scanf("%d%d%d", &p, &q, &c);
    p--;
    q--;
    g[p].push_back(make_pair(c, q));
    g[q].push_back(make_pair(c, p));
    all[c].push_back(p);
    all[c].push_back(q);
  }
  for (i = 0; i < n; i++) {
    sort(g[i].begin(), g[i].end());
    ptr[i] = 0;
  }
  for (i = 0; i < n; i++) {
    was[i] = -1;
  }
  int newn = n;
  for (i = 0; i < maxn; i++) {
    newg[i].clear();
  }
  for (int color = 0; color < maxn; color++) {
    if (all[color].empty())
      continue;
    for (int it = 0; it < all[color].size(); it++) {
      int v = all[color][it];
      if (was[v] == color)
        continue;
      int b = 0, e = 1;
      x[0] = v;
      was[v] = color;
      while (b < e) {
        while (ptr[x[b]] < g[x[b]].size()) {
          int c = g[x[b]][ptr[x[b]]].first;
          if (c != color)
            break;
          int u = g[x[b]][ptr[x[b]]].second;
          if (was[u] != color) {
            was[u] = color;
            x[e] = u;
            e++;
          }
          ptr[x[b]]++;
        }
        b++;
      }
      for (i = 0; i < e; i++) {
        newg[x[i]].push_back(newn);
        newg[newn].push_back(x[i]);
      }
      newn++;
    }
  }
  for (i = 0; i < newn; i++) {
    dist[i] = -2;
  }
  int b = 0, e = 1;
  x[0] = 0;
  dist[0] = 0;
  while (b < e) {
    for (j = 0; j < newg[x[b]].size(); j++) {
      int u = newg[x[b]][j];
      if (dist[u] == -2) {
        dist[u] = dist[x[b]] + 1;
        x[e] = u;
        e++;
      }
    }
    b++;
  }
  cout << dist[n - 1] / 2 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn = 2000100;
int dist[maxn];
vector<pair<int, int>> g[maxn];
vector<int> newg[maxn];
vector<int> all[maxn];
int was[maxn], x[maxn], ptr[maxn];
int main() {
  int n, m, i, j, k;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    int p, q, c;
    scanf("%d%d%d", &p, &q, &c);
    p--;
    q--;
    g[p].push_back(make_pair(c, q));
    g[q].push_back(make_pair(c, p));
    all[c].push_back(p);
    all[c].push_back(q);
  }
  for (i = 0; i < n; i++) {
    sort(g[i].begin(), g[i].end());
    ptr[i] = 0;
  }
  for (i = 0; i < n; i++) {
    was[i] = -1;
  }
  int newn = n;
  for (i = 0; i < maxn; i++) {
    newg[i].clear();
  }
  for (int color = 0; color < maxn; color++) {
    if (all[color].empty())
      continue;
    for (int it = 0; it < all[color].size(); it++) {
      int v = all[color][it];
      if (was[v] == color)
        continue;
      int b = 0, e = 1;
      x[0] = v;
      was[v] = color;
      while (b < e) {
        while (ptr[x[b]] < g[x[b]].size()) {
          int c = g[x[b]][ptr[x[b]]].first;
          if (c != color)
            break;
          int u = g[x[b]][ptr[x[b]]].second;
          if (was[u] != color) {
            was[u] = color;
            x[e] = u;
            e++;
          }
          ptr[x[b]]++;
        }
        b++;
      }
      for (i = 0; i < e; i++) {
        newg[x[i]].push_back(newn);
        newg[newn].push_back(x[i]);
      }
      newn++;
    }
  }
  for (i = 0; i < newn; i++) {
    dist[i] = -2;
  }
  int b = 0, e = 1;
  x[0] = 0;
  dist[0] = 0;
  while (b < e) {
    for (j = 0; j < newg[x[b]].size(); j++) {
      int u = newg[x[b]][j];
      if (dist[u] == -2) {
        dist[u] = dist[x[b]] + 1;
        x[e] = u;
        e++;
      }
    }
    b++;
  }
  cout << dist[n - 1] / 2 << endl;
  return 0;
}