Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
#define N 300050
#define M 1200050
int head[N], to[M], nxt[M], n, m, cnt, val[M], tot, vis[N], dis[N], S, T;
priority_queue<pair<int, int>> q;
struct A {
  int x, y, z, id;
  bool operator<(const A &u) const { return z < u.z; }
} e[N];
vector<A> G[N];
inline void add(int u, int v, int w) {
  to[++cnt] = v;
  nxt[cnt] = head[u];
  head[u] = cnt;
  val[cnt] = w;
}
int main() {
  scanf("%d%d", &n, &m);
  int i, j;
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
    e[i].id = i;
    G[e[i].x].push_back(e[i]);
    G[e[i].y].push_back(e[i]);
  }
  tot = m;
  S = n + m + 1, T = n + m + 2;
  for (i = 1; i <= n; i++) {
    sort(G[i].begin(), G[i].end());
    int lim = G[i].size();
    tot++;
    for (j = 0; j < lim; j++) {
      add(G[i][j].id, tot, 0);
      add(tot, G[i][j].id, 1);
    }
    int k = 0, l;
    for (j = 0; j < lim; j = k) {
      for (; k < lim && G[i][j].z == G[i][k].z; k++)
        ;
      for (l = j + 1; l < k; l++) {
        add(G[i][l].id, G[i][l - 1].id, 0);
        add(G[i][l - 1].id, G[i][l].id, 0);
      }
    }
    for (j = 0; j < lim; j++) {
      if (i == 1)
        add(S, G[i][j].id, 0);
      if (i == n)
        add(G[i][j].id, T, 0);
    }
  }
  tot += 2;
  memset(dis, 0x3f, sizeof(dis));
  dis[S] = 0;
  q.push(make_pair(0, S));
  while (!q.empty()) {
    int x = q.top().second;
    q.pop();
    if (vis[x])
      continue;
    vis[x] = 1;
    for (i = head[x]; i; i = nxt[i])
      if (dis[to[i]] > dis[x] + val[i]) {
        dis[to[i]] = dis[x] + val[i];
        q.push(make_pair(-dis[to[i]], to[i]));
      }
  }
  if (dis[T] > max(n, m))
    puts("-1");
  else
    printf("%d\n", dis[T] + 1);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
#define N 300050
#define M 2400050
int head[N], to[M], nxt[M], n, m, cnt, val[M], tot, vis[N], dis[N], S, T;
priority_queue<pair<int, int>> q;
struct A {
  int x, y, z, id;
  bool operator<(const A &u) const { return z < u.z; }
} e[N];
vector<A> G[N];
inline void add(int u, int v, int w) {
  to[++cnt] = v;
  nxt[cnt] = head[u];
  head[u] = cnt;
  val[cnt] = w;
}
int main() {
  scanf("%d%d", &n, &m);
  int i, j;
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
    e[i].id = i;
    G[e[i].x].push_back(e[i]);
    G[e[i].y].push_back(e[i]);
  }
  tot = m;
  S = n + m + 1, T = n + m + 2;
  for (i = 1; i <= n; i++) {
    sort(G[i].begin(), G[i].end());
    int lim = G[i].size();
    tot++;
    for (j = 0; j < lim; j++) {
      add(G[i][j].id, tot, 0);
      add(tot, G[i][j].id, 1);
    }
    int k = 0, l;
    for (j = 0; j < lim; j = k) {
      for (; k < lim && G[i][j].z == G[i][k].z; k++)
        ;
      for (l = j + 1; l < k; l++) {
        add(G[i][l].id, G[i][l - 1].id, 0);
        add(G[i][l - 1].id, G[i][l].id, 0);
      }
    }
    for (j = 0; j < lim; j++) {
      if (i == 1)
        add(S, G[i][j].id, 0);
      if (i == n)
        add(G[i][j].id, T, 0);
    }
  }
  tot += 2;
  memset(dis, 0x3f, sizeof(dis));
  dis[S] = 0;
  q.push(make_pair(0, S));
  while (!q.empty()) {
    int x = q.top().second;
    q.pop();
    if (vis[x])
      continue;
    vis[x] = 1;
    for (i = head[x]; i; i = nxt[i])
      if (dis[to[i]] > dis[x] + val[i]) {
        dis[to[i]] = dis[x] + val[i];
        q.push(make_pair(-dis[to[i]], to[i]));
      }
  }
  if (dis[T] > max(n, m))
    puts("-1");
  else
    printf("%d\n", dis[T] + 1);
}