Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FAST_IO                                                                \
  std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define Puase system("pause")
using namespace std;
typedef long long ll;
typedef unsigned int UINT;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, int> pli;
typedef pair<int, int> pint;

int const maxn = 2e5 + 10;
ll const INF = 0x3F3F3F3F3F3F3F3F;
ll dis[maxn << 2];
std::map<pint, int> Node;
struct node {
  int v, w, next;
} e[maxn << 5];
int cnt, head[maxn];
int vis[maxn];

void add(int u, int v, int w) {
  e[cnt].v = v;
  e[cnt].w = w;
  e[cnt].next = head[u];
  head[u] = cnt++;
}
int node_num = 0;
int getNodeNum(int u, int v) {
  auto x = make_pair(u, v);
  if (Node.find(x) == Node.end()) {
    Node[x] = ++node_num;
  }

  return Node[x];
}

void dijkstra() {
  memset(dis, 0x3f, sizeof(dis));
  priority_queue<pli> q;
  dis[1] = 0;
  q.push(make_pair(0, 1));
  int c = 0;
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    for (int i = head[u]; ~i; i = e[i].next) {
      int v = e[i].v;
      int w = e[i].w;
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        q.push(make_pair(-dis[v], v));
      }
    }
  }
}

int main(void) {
  FAST_IO;
  memset(head, -1, sizeof(head));
  int n, m;
  cin >> n >> m;
  node_num = n;
  for (int i = 0; i < m; i++) {
    int p, q, c;
    cin >> p >> q >> c;
    int u = getNodeNum(p, c);
    int v = getNodeNum(q, c);
    add(p, u, 1);
    add(u, p, 1);
    add(q, v, 1);
    add(v, q, 1);
    add(u, v, 0);
    add(v, u, 0);
  }
  dijkstra();

  cout << (dis[n] == INF ? -1 : dis[n] / 2) << endl;
  // Puase;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FAST_IO                                                                \
  std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define Puase system("pause")
using namespace std;
typedef long long ll;
typedef unsigned int UINT;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, int> pli;
typedef pair<int, int> pint;

int const maxn = 5e5 + 10;
ll const INF = 0x3F3F3F3F3F3F3F3F;
ll dis[maxn << 2];
std::map<pint, int> Node;
struct node {
  int v, w, next;
} e[maxn << 5];
int cnt, head[maxn];
int vis[maxn];

void add(int u, int v, int w) {
  e[cnt].v = v;
  e[cnt].w = w;
  e[cnt].next = head[u];
  head[u] = cnt++;
}
int node_num = 0;
int getNodeNum(int u, int v) {
  auto x = make_pair(u, v);
  if (Node.find(x) == Node.end()) {
    Node[x] = ++node_num;
  }

  return Node[x];
}

void dijkstra() {
  memset(dis, 0x3f, sizeof(dis));
  priority_queue<pli> q;
  dis[1] = 0;
  q.push(make_pair(0, 1));
  int c = 0;
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    for (int i = head[u]; ~i; i = e[i].next) {
      int v = e[i].v;
      int w = e[i].w;
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        q.push(make_pair(-dis[v], v));
      }
    }
  }
}

int main(void) {
  FAST_IO;
  memset(head, -1, sizeof(head));
  int n, m;
  cin >> n >> m;
  node_num = n;
  for (int i = 0; i < m; i++) {
    int p, q, c;
    cin >> p >> q >> c;
    int u = getNodeNum(p, c);
    int v = getNodeNum(q, c);
    add(p, u, 1);
    add(u, p, 1);
    add(q, v, 1);
    add(v, q, 1);
    add(u, v, 0);
    add(v, u, 0);
  }
  dijkstra();

  cout << (dis[n] == INF ? -1 : dis[n] / 2) << endl;
  // Puase;
  return 0;
}