Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MST(a, x) memset(a, x, sizeof(a))
#define ll long long
#define PB push_back
#define PH push
#define MP make_pair
#define FT first
#define SD second
#define N 400005
#define M 21
#define INF 1000000007
#define MOD 1000000007
#define MOD2 1000000009
#define eps 1e-14
using namespace std;
vector<pair<int, int>> e[N];
map<pair<int, int>, int> mp;
int x[N], y[N], z[N];
int n, m, cnt;
int get(int u, int c) {
  auto ret = MP(u, c);
  if (mp.find(ret) == mp.end())
    mp[ret] = ++cnt;
  return mp[ret];
}

int dis[N], vis[N];
queue<int> q;
void spfa(int st) {
  MST(vis, 0);
  FOR(i, 1, cnt + n) dis[i] = INF;
  while (!q.empty())
    q.pop();
  dis[st] = 0;
  vis[st] = 1;
  q.push(st);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (auto v : e[u]) {
      if (dis[v.FT] > dis[u] + v.SD) {
        dis[v.FT] = dis[u] + v.SD;
        if (!vis[v.FT]) {
          q.push(v.FT);
          vis[v.FT] = 1;
        }
      }
    }
  }
}
void addedge(int x, int y, int z) {
  int xx = get(x, z);
  int yy = get(y, z);
  e[xx].PB(MP(yy, 0));
  e[yy].PB(MP(xx, 0));
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> n >> m) {
    FOR(i, 1, n) e[i].clear();
    cnt = 0;
    MST(vis, 0);
    FOR(i, 1, m) {
      cin >> x[i] >> y[i] >> z[i];
      addedge(x[i], y[i], z[i]);
    }
    FOR(i, 1, m) {
      // cout<<get(x[i],z[i])<<" "<<x[i]+cnt<<" "<<get(y[i],z[i])<<"
      // "<<y[i]+cnt<<endl;
      e[get(x[i], z[i])].PB(MP(x[i] + cnt, 1));
      e[x[i] + cnt].PB(MP(get(x[i], z[i]), 1));
      e[get(y[i], z[i])].PB(MP(y[i] + cnt, 1));
      e[y[i] + cnt].PB(MP(get(y[i], z[i]), 1));
    }
    spfa(1 + cnt);
    if (dis[n + cnt] == INF)
      cout << -1 << endl;
    else
      cout << dis[n + cnt] / 2 << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MST(a, x) memset(a, x, sizeof(a))
#define ll long long
#define PB push_back
#define PH push
#define MP make_pair
#define FT first
#define SD second
#define N 800005
#define M 21
#define INF 1000000007
#define MOD 1000000007
#define MOD2 1000000009
#define eps 1e-14
using namespace std;
vector<pair<int, int>> e[N];
map<pair<int, int>, int> mp;
int x[N], y[N], z[N];
int n, m, cnt;
int get(int u, int c) {
  auto ret = MP(u, c);
  if (mp.find(ret) == mp.end())
    mp[ret] = ++cnt;
  return mp[ret];
}

int dis[N], vis[N];
queue<int> q;
void spfa(int st) {
  MST(vis, 0);
  FOR(i, 1, cnt + n) dis[i] = INF;
  while (!q.empty())
    q.pop();
  dis[st] = 0;
  vis[st] = 1;
  q.push(st);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (auto v : e[u]) {
      if (dis[v.FT] > dis[u] + v.SD) {
        dis[v.FT] = dis[u] + v.SD;
        if (!vis[v.FT]) {
          q.push(v.FT);
          vis[v.FT] = 1;
        }
      }
    }
  }
}
void addedge(int x, int y, int z) {
  int xx = get(x, z);
  int yy = get(y, z);
  e[xx].PB(MP(yy, 0));
  e[yy].PB(MP(xx, 0));
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> n >> m) {
    FOR(i, 1, n) e[i].clear();
    cnt = 0;
    MST(vis, 0);
    FOR(i, 1, m) {
      cin >> x[i] >> y[i] >> z[i];
      addedge(x[i], y[i], z[i]);
    }
    FOR(i, 1, m) {
      // cout<<get(x[i],z[i])<<" "<<x[i]+cnt<<" "<<get(y[i],z[i])<<"
      // "<<y[i]+cnt<<endl;
      e[get(x[i], z[i])].PB(MP(x[i] + cnt, 1));
      e[x[i] + cnt].PB(MP(get(x[i], z[i]), 1));
      e[get(y[i], z[i])].PB(MP(y[i] + cnt, 1));
      e[y[i] + cnt].PB(MP(get(y[i], z[i]), 1));
    }
    spfa(1 + cnt);
    if (dis[n + cnt] == INF)
      cout << -1 << endl;
    else
      cout << dis[n + cnt] / 2 << endl;
  }
  return 0;
}