Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <map>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
inline int read() {
  int w = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    w = (w << 3) + (w << 1) + ch - 48;
    ch = getchar();
  }
  return w * f;
}
int n, m, cnt, head[200010], cur;
map<int, int> mapp[200010];
struct Edge {
  int from, to, next, dis;
} edge[5000010];
inline void addedge(int u, int v, int w) {
  cnt++;
  edge[cnt].from = u;
  edge[cnt].to = v;
  edge[cnt].dis = w;
  edge[cnt].next = head[u];
  head[u] = cnt;
}
inline void ins(int u, int v, int w) {
  addedge(u, v, w);
  addedge(v, u, w);
  return;
}
inline int find(int x, int y) {
  if (mapp[x][y] != 0)
    return mapp[x][y];
  else {
    mapp[x][y] = ++cur;
    return cur;
  }
}
int dis[2000010];
struct Node {
  int u, dis;
  friend bool operator<(Node x, Node y) { return x.dis > y.dis; }
};
priority_queue<Node> q;
inline void dij() {
  memset(dis, 0x3f, sizeof(dis));
  dis[1] = 0;
  q.push((Node){1, dis[1]});
  while (!q.empty()) {
    Node cur = q.top();
    q.pop();
    int u = cur.u;
    for (int i = head[u]; i; i = edge[i].next) {
      int v = edge[i].to;
      int w = edge[i].dis;
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        q.push((Node){v, dis[v]});
      }
    }
  }
}
signed main() {
  n = read();
  m = read();
  cur = n;
  for (int i = 1; i <= m; i++) {
    int x = read();
    int y = read();
    int z = read();
    int id1 = find(x, z);
    int id2 = find(y, z);
    ins(x, id1, 1);
    ins(y, id2, 1);
    ins(id1, id2, 0);
  }
  dij();
  if (dis[n] == INF)
    cout << -1 << endl;
  else
    cout << dis[n] / 2 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <map>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
inline int read() {
  int w = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    w = (w << 3) + (w << 1) + ch - 48;
    ch = getchar();
  }
  return w * f;
}
int n, m, cnt, head[500010], cur;
map<int, int> mapp[2000010];
struct Edge {
  int from, to, next, dis;
} edge[5000010];
inline void addedge(int u, int v, int w) {
  cnt++;
  edge[cnt].from = u;
  edge[cnt].to = v;
  edge[cnt].dis = w;
  edge[cnt].next = head[u];
  head[u] = cnt;
}
inline void ins(int u, int v, int w) {
  addedge(u, v, w);
  addedge(v, u, w);
  return;
}
inline int find(int x, int y) {
  if (mapp[x][y] != 0)
    return mapp[x][y];
  else {
    mapp[x][y] = ++cur;
    return cur;
  }
}
int dis[2000010];
struct Node {
  int u, dis;
  friend bool operator<(Node x, Node y) { return x.dis > y.dis; }
};
priority_queue<Node> q;
inline void dij() {
  memset(dis, 0x3f, sizeof(dis));
  dis[1] = 0;
  q.push((Node){1, dis[1]});
  while (!q.empty()) {
    Node cur = q.top();
    q.pop();
    int u = cur.u;
    for (int i = head[u]; i; i = edge[i].next) {
      int v = edge[i].to;
      int w = edge[i].dis;
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        q.push((Node){v, dis[v]});
      }
    }
  }
}
signed main() {
  n = read();
  m = read();
  cur = n;
  for (int i = 1; i <= m; i++) {
    int x = read();
    int y = read();
    int z = read();
    int id1 = find(x, z);
    int id2 = find(y, z);
    ins(x, id1, 1);
    ins(y, id2, 1);
    ins(id1, id2, 0);
  }
  dij();
  if (dis[n] == INF)
    cout << -1 << endl;
  else
    cout << dis[n] / 2 << endl;
  return 0;
}