Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
long long READ() {
  long long xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
char one() {
  char ch = getchar();
  while (ch == ' ' || ch == '\n')
    ch = getchar();
  return ch;
}
const int maxn = 100010;
int N, M, lin[maxn * 5], len, cnt;
struct edge {
  int y, next, v;
} e[maxn * 12];
inline void insert(int xx, int yy, int vv) {
  e[++len].next = lin[xx];
  lin[xx] = len;
  e[len].y = yy;
  e[len].v = vv;
}
inline void ins(int xx, int yy, int vv) {
  // printf("%d %d %d\n",xx,yy,vv);
  insert(xx, yy, vv);
  insert(yy, xx, vv);
}
map<pair<int, int>, int> id;
int get_id(int x, int y) {
  if (id[make_pair(x, y)])
    return id[make_pair(x, y)];
  return id[make_pair(x, y)] = ++cnt;
}
deque<int> q;
int dis[maxn * 4];
void bfs() {
  memset(dis, 10, sizeof(dis));
  dis[1] = 0;
  q.clear();
  q.push_back(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop_front();
    for (int i = lin[u]; i; i = e[i].next)
      if (dis[e[i].y] > dis[u] + e[i].v) {
        dis[e[i].y] = dis[u] + e[i].v;
        if (!e[i].v)
          q.push_front(e[i].y);
        else
          q.push_back(e[i].y);
      }
  }
  if (dis[N] == dis[0])
    puts("-1");
  else
    printf("%d\n", dis[N] / 2);
}
int main() {
  // freopen("in","r",stdin);
  cnt = N = read(), M = read();
  for (int i = 1; i <= M; i++) {
    int x = read(), y = read(), c = read();
    ins(get_id(x, c), get_id(y, c), 0);
    ins(get_id(x, c), x, 1);
    ins(get_id(y, c), y, 1);
  }
  bfs();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
long long READ() {
  long long xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
char one() {
  char ch = getchar();
  while (ch == ' ' || ch == '\n')
    ch = getchar();
  return ch;
}
const int maxn = 100010;
int N, M, lin[maxn * 5], len, cnt;
struct edge {
  int y, next, v;
} e[maxn * 12];
inline void insert(int xx, int yy, int vv) {
  e[++len].next = lin[xx];
  lin[xx] = len;
  e[len].y = yy;
  e[len].v = vv;
}
inline void ins(int xx, int yy, int vv) {
  // printf("%d %d %d\n",xx,yy,vv);
  insert(xx, yy, vv);
  insert(yy, xx, vv);
}
map<pair<int, int>, int> id;
int get_id(int x, int y) {
  if (id[make_pair(x, y)])
    return id[make_pair(x, y)];
  return id[make_pair(x, y)] = ++cnt;
}
deque<int> q;
int dis[maxn * 5];
void bfs() {
  memset(dis, 10, sizeof(dis));
  dis[1] = 0;
  q.clear();
  q.push_back(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop_front();
    for (int i = lin[u]; i; i = e[i].next)
      if (dis[e[i].y] > dis[u] + e[i].v) {
        dis[e[i].y] = dis[u] + e[i].v;
        if (!e[i].v)
          q.push_front(e[i].y);
        else
          q.push_back(e[i].y);
      }
  }
  if (dis[N] == dis[0])
    puts("-1");
  else
    printf("%d\n", dis[N] / 2);
}
int main() {
  // freopen("in","r",stdin);
  cnt = N = read(), M = read();
  for (int i = 1; i <= M; i++) {
    int x = read(), y = read(), c = read();
    ins(get_id(x, c), get_id(y, c), 0);
    ins(get_id(x, c), x, 1);
    ins(get_id(y, c), y, 1);
  }
  bfs();
  return 0;
}