Problem p04003 (C++)

Buggy submission (status: Runtime Error):
// ΔARC 061E
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5;
const int C = 1e6 + 5;
vector<pair<int, int>> V[C + 5];
vector<int> G[N], P[N];
int vis[N];
int f[N], ff;
int b[N], bb;
void dfs(int u) {
  vis[u] = 1;
  b[++bb] = u;
  int i, s, x;
  s = G[u].size();
  for (i = 0; i < s; i = i + 1) {
    x = G[u][i];
    if (!vis[x])
      dfs(x);
  }
}
queue<pair<int, int>> Q;
int main() {
  int n, m, i, j, k, x, y, z, s, o;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i = i + 1) {
    scanf("%d%d%d", &x, &y, &z);
    V[z].push_back(make_pair(x, y));
  }
  o = n;
  for (i = 1; i < C; i = i + 1) {
    s = V[i].size();
    ff = 0;
    for (j = 0; j < s; j = j + 1) {
      G[V[i][j].first].push_back(V[i][j].second);
      G[V[i][j].second].push_back(V[i][j].first);
      f[++ff] = V[i][j].first;
      f[++ff] = V[i][j].second;
    }
    for (j = 1; j <= ff; j = j + 1) {
      if (!vis[f[j]]) {
        bb = 0;
        dfs(f[j]);
        o++;
        for (k = 1; k <= bb; k = k + 1) {
          P[b[k]].push_back(o);
          P[o].push_back(b[k]);
        }
      }
    }
    for (j = 1; j <= ff; j = j + 1) {
      G[f[j]].clear();
      vis[f[j]] = 0;
    }
  }
  Q.push(make_pair(1, 0));
  while (!Q.empty()) {
    x = Q.front().first;
    y = Q.front().second;
    Q.pop();
    vis[x] = 1;
    if (x == n) {
      cout << y / 2 << endl;
      return 0;
    }
    s = P[x].size();
    for (i = 0; i < s; i = i + 1)
      if (!vis[P[x][i]])
        Q.push(make_pair(P[x][i], y + 1)), vis[P[x][i]] = 1;
  }
  cout << "-1\n";
  return 0;
}
/*
8 11
1 3 1
1 4 2
2 3 1
2 5 1
3 4 3
3 6 3
3 7 3
4 8 4
5 6 1
6 7 5
7 8 5

2

*/

Fixed submission:
// ΔARC 061E
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5;
const int C = 1e6 + 5;
vector<pair<int, int>> V[C + 5];
vector<int> G[N], P[4 * N];
int vis[4 * N];
int f[4 * N], ff;
int b[4 * N], bb;
void dfs(int u) {
  vis[u] = 1;
  b[++bb] = u;
  int i, s, x;
  s = G[u].size();
  for (i = 0; i < s; i = i + 1) {
    x = G[u][i];
    if (!vis[x])
      dfs(x);
  }
}
queue<pair<int, int>> Q;
int main() {
  int n, m, i, j, k, x, y, z, s, o;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i = i + 1) {
    scanf("%d%d%d", &x, &y, &z);
    V[z].push_back(make_pair(x, y));
  }
  o = n;
  for (i = 1; i < C; i = i + 1) {
    s = V[i].size();
    ff = 0;
    for (j = 0; j < s; j = j + 1) {
      G[V[i][j].first].push_back(V[i][j].second);
      G[V[i][j].second].push_back(V[i][j].first);
      f[++ff] = V[i][j].first;
      f[++ff] = V[i][j].second;
    }
    for (j = 1; j <= ff; j = j + 1) {
      if (!vis[f[j]]) {
        bb = 0;
        dfs(f[j]);
        o++;
        for (k = 1; k <= bb; k = k + 1) {
          P[b[k]].push_back(o);
          P[o].push_back(b[k]);
        }
      }
    }
    for (j = 1; j <= ff; j = j + 1) {
      G[f[j]].clear();
      vis[f[j]] = 0;
    }
  }
  Q.push(make_pair(1, 0));
  while (!Q.empty()) {
    x = Q.front().first;
    y = Q.front().second;
    Q.pop();
    vis[x] = 1;
    if (x == n) {
      cout << y / 2 << endl;
      return 0;
    }
    s = P[x].size();
    for (i = 0; i < s; i = i + 1)
      if (!vis[P[x][i]])
        Q.push(make_pair(P[x][i], y + 1)), vis[P[x][i]] = 1;
  }
  cout << "-1\n";
  return 0;
}
/*
8 11
1 3 1
1 4 2
2 3 1
2 5 1
3 4 3
3 6 3
3 7 3
4 8 4
5 6 1
6 7 5
7 8 5

2

*/