Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pa;
const long long INF = 1ll << 62;
const int maxn = 2e5 + 10;
ll n, m, cnt;
map<ll, ll> Ma[maxn]; // 标记
vector<Pa> V[maxn];   // 存放pair关系
int vis[maxn];
ll dist[maxn];
struct node {
  ll dist;
  int id;
  bool operator<(const node &b) const { return dist > b.dist; }
};
int getid(int u, int v) // 返回编号
{
  if (Ma[u][v] == 0)
    Ma[u][v] = ++cnt;
  return Ma[u][v];
}
void spfa() {
  for (int i = 0; i <= cnt; i++) {
    vis[i] = 0;
    dist[i] = INF;
  }
  node temp;
  temp.dist = 0;
  temp.id = 1;
  priority_queue<node> q;
  q.push(temp);
  dist[1] = 0;
  while (!q.empty()) {
    node u = q.top();
    q.pop();
    if (vis[u.id] == 1)
      continue;
    vis[u.id] = 1;
    for (int i = 0; i < V[u.id].size(); i++) {
      int v = V[u.id][i].first;
      int Valu = V[u.id][i].second;
      if (dist[v] > dist[u.id] + Valu) {
        dist[v] = dist[u.id] + Valu;
        node Temp;
        Temp.dist = dist[v];
        Temp.id = v;
        q.push(Temp);
      }
    }
  }
  if (dist[n] == INF)
    cout << "-1" << endl;
  else
    cout << dist[n] / 2 << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  while (cin >> n >> m) {
    for (int i = 0; i <= n; i++)
      Ma[i].clear();
    for (int i = 0; i <= n; i++)
      V[i].clear();
    cnt = n; // 从n+1开始记录编号
    for (int i = 0; i < m; i++) {
      int u, v, id;
      cin >> u >> v >> id;
      int uid = getid(u, id);
      int vid = getid(v, id);
      V[u].push_back(Pa(uid, 1));
      V[uid].push_back(Pa(u, 1));

      V[v].push_back(Pa(vid, 1));
      V[vid].push_back(Pa(v, 1));

      V[vid].push_back(Pa(uid, 0));
      V[uid].push_back(Pa(vid, 0));
    }
    spfa();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pa;
const long long INF = 1ll << 62;
const int maxn = 5e5 + 10;
ll n, m, cnt;
map<ll, ll> Ma[maxn]; // 标记
vector<Pa> V[maxn];   // 存放pair关系
int vis[maxn];
ll dist[maxn];
struct node {
  ll dist;
  int id;
  bool operator<(const node &b) const { return dist > b.dist; }
};
int getid(int u, int v) // 返回编号
{
  if (Ma[u][v] == 0)
    Ma[u][v] = ++cnt;
  return Ma[u][v];
}
void spfa() {
  for (int i = 0; i <= cnt; i++) {
    vis[i] = 0;
    dist[i] = INF;
  }
  node temp;
  temp.dist = 0;
  temp.id = 1;
  priority_queue<node> q;
  q.push(temp);
  dist[1] = 0;
  while (!q.empty()) {
    node u = q.top();
    q.pop();
    if (vis[u.id] == 1)
      continue;
    vis[u.id] = 1;
    for (int i = 0; i < V[u.id].size(); i++) {
      int v = V[u.id][i].first;
      int Valu = V[u.id][i].second;
      if (dist[v] > dist[u.id] + Valu) {
        dist[v] = dist[u.id] + Valu;
        node Temp;
        Temp.dist = dist[v];
        Temp.id = v;
        q.push(Temp);
      }
    }
  }
  if (dist[n] == INF)
    cout << "-1" << endl;
  else
    cout << dist[n] / 2 << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  while (cin >> n >> m) {
    for (int i = 0; i <= n; i++)
      Ma[i].clear();
    for (int i = 0; i <= n; i++)
      V[i].clear();
    cnt = n; // 从n+1开始记录编号
    for (int i = 0; i < m; i++) {
      int u, v, id;
      cin >> u >> v >> id;
      int uid = getid(u, id);
      int vid = getid(v, id);
      V[u].push_back(Pa(uid, 1));
      V[uid].push_back(Pa(u, 1));

      V[v].push_back(Pa(vid, 1));
      V[vid].push_back(Pa(v, 1));

      V[vid].push_back(Pa(uid, 0));
      V[uid].push_back(Pa(vid, 0));
    }
    spfa();
  }
  return 0;
}