Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 1000010
#define MAXM 1000010
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;

int read() {
  int res = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    res = res * 10 + ch - '0';
    ch = getchar();
  }
  return res;
}

struct Edge {
  int from, to, next, dist;
  Edge() {}
  Edge(int u, int v, int n, int d) : from(u), to(v), next(n), dist(d) {}
};

struct Node {
  int d, u;
  bool operator<(const Node &rhs) const { return d > rhs.d; }
};

int n, m, num;
Edge edges[MAXM];
int head[MAXN], tot, vis[MAXN], dis[MAXN];
map<int, int> mp[MAXN];

void AddEdge(int u, int v, int dist) {
  edges[tot] = Edge(u, v, head[u], dist);
  head[u] = tot++;
  edges[tot] = Edge(v, u, head[v], dist);
  head[v] = tot++;
}

void init() {
  tot = 0;
  num = n;
  memset(head, -1, sizeof(head));
}

void dijkstra(int s) {
  memset(dis, 0x3f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  priority_queue<Node> q;
  dis[s] = 0;
  q.push((Node){0, s});
  while (!q.empty()) {
    Node x = q.top();
    q.pop();
    int u = x.u;
    if (vis[u])
      continue;
    vis[u] = 1;
    for (int i = head[u]; ~i; i = edges[i].next) {
      Edge &e = edges[i];
      if (dis[e.to] > dis[u] + e.dist) {
        dis[e.to] = dis[u] + e.dist;
        q.push((Node){dis[e.to], e.to});
      }
    }
  }
}

int id(int a, int b) {
  if (mp[a][b])
    return mp[a][b];
  return mp[a][b] = ++num;
}

int main() {
  n = read(), m = read();
  init();
  for (int i = 0; i != m; ++i) {
    int u, v, com;
    u = read(), v = read(), com = read();
    AddEdge(u, id(u, com), 1);
    AddEdge(v, id(v, com), 1);
    AddEdge(id(u, com), id(v, com), 0);
  }
  dijkstra(1);
  int ans;
  if (dis[n] == INF)
    ans = -1;
  else
    ans = dis[n] / 2;
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 2000010
#define MAXM 2000010
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;

int read() {
  int res = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    res = res * 10 + ch - '0';
    ch = getchar();
  }
  return res;
}

struct Edge {
  int from, to, next, dist;
  Edge() {}
  Edge(int u, int v, int n, int d) : from(u), to(v), next(n), dist(d) {}
};

struct Node {
  int d, u;
  bool operator<(const Node &rhs) const { return d > rhs.d; }
};

int n, m, num;
Edge edges[MAXM];
int head[MAXN], tot, vis[MAXN], dis[MAXN];
map<int, int> mp[MAXN];

void AddEdge(int u, int v, int dist) {
  edges[tot] = Edge(u, v, head[u], dist);
  head[u] = tot++;
  edges[tot] = Edge(v, u, head[v], dist);
  head[v] = tot++;
}

void init() {
  tot = 0;
  num = n;
  memset(head, -1, sizeof(head));
}

void dijkstra(int s) {
  memset(dis, 0x3f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  priority_queue<Node> q;
  dis[s] = 0;
  q.push((Node){0, s});
  while (!q.empty()) {
    Node x = q.top();
    q.pop();
    int u = x.u;
    if (vis[u])
      continue;
    vis[u] = 1;
    for (int i = head[u]; ~i; i = edges[i].next) {
      Edge &e = edges[i];
      if (dis[e.to] > dis[u] + e.dist) {
        dis[e.to] = dis[u] + e.dist;
        q.push((Node){dis[e.to], e.to});
      }
    }
  }
}

int id(int a, int b) {
  if (mp[a][b])
    return mp[a][b];
  return mp[a][b] = ++num;
}

int main() {
  n = read(), m = read();
  init();
  for (int i = 0; i != m; ++i) {
    int u, v, com;
    u = read(), v = read(), com = read();
    AddEdge(u, id(u, com), 1);
    AddEdge(v, id(v, com), 1);
    AddEdge(id(u, com), id(v, com), 0);
  }
  dijkstra(1);
  int ans;
  if (dis[n] == INF)
    ans = -1;
  else
    ans = dis[n] / 2;
  printf("%d\n", ans);
}