Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, init, a) for (int i = init; i < a; i++)
#define rep(i, a) FOR(i, 0, a)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a, n) memset(a, n, sizeof(a))
#define all(a) a.begin(), a.end()
#define chmin(a, b) a = min(a, b);
#define chmax(a, b) a = max(a, b);

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

struct edge {
  int to, cost;
};
int N, M, id = 0;
vector<edge> G[400009];
map<pii, int> comid;

int getid(int node, int com) {
  pii p = {node, com};
  if (comid.find(p) != comid.end())
    return comid[p];
  return comid[p] = id++;
}

void addedge(int u, int v, int uc, int vc, int cost) {
  u = getid(u, uc), v = getid(v, vc);
  G[u].push_back({v, cost});
  G[v].push_back({u, cost});
}

int dijkstra(int s, int t, int range) {

  vector<int> d(range, INF);
  priority_queue<pii, vector<pii>, greater<pii>>
      que; // first:cost second:頂点 cost基準の昇順
  que.push({0, s});
  d[s] = 0;

  while (!que.empty()) {
    pii p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push({d[e.to], e.to});
      }
    }
  }
  return d[t];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  rep(i, M) {
    int u, v, c;
    cin >> u >> v >> c;
    addedge(u, v, c, c, 0);
    addedge(u, u, c, -1, 1);
    addedge(v, v, c, -1, 1);
  }

  int s = getid(1, -1);
  int t = getid(N, -1);
  int ans = dijkstra(s, t, id);

  if (ans == INF)
    ans = -1;
  else
    ans /= 2;

  cout1(ans)
}

Fixed submission:
#include "bits/stdc++.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, init, a) for (int i = init; i < a; i++)
#define rep(i, a) FOR(i, 0, a)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a, n) memset(a, n, sizeof(a))
#define all(a) a.begin(), a.end()
#define chmin(a, b) a = min(a, b);
#define chmax(a, b) a = max(a, b);

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

struct edge {
  int to, cost;
};
int N, M, id = 0;
vector<edge> G[500009];
map<pii, int> comid;

int getid(int node, int com) {
  pii p = {node, com};
  if (comid.find(p) != comid.end())
    return comid[p];
  return comid[p] = id++;
}

void addedge(int u, int v, int uc, int vc, int cost) {
  u = getid(u, uc), v = getid(v, vc);
  G[u].push_back({v, cost});
  G[v].push_back({u, cost});
}

int dijkstra(int s, int t, int range) {

  vector<int> d(range, INF);
  priority_queue<pii, vector<pii>, greater<pii>>
      que; // first:cost second:頂点 cost基準の昇順
  que.push({0, s});
  d[s] = 0;

  while (!que.empty()) {
    pii p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push({d[e.to], e.to});
      }
    }
  }
  return d[t];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  rep(i, M) {
    int u, v, c;
    cin >> u >> v >> c;
    addedge(u, v, c, c, 0);
    addedge(u, u, c, -1, 1);
    addedge(v, v, c, -1, 1);
  }

  int s = getid(1, -1);
  int t = getid(N, -1);
  int ans = dijkstra(s, t, id);

  if (ans == INF)
    ans = -1;
  else
    ans /= 2;

  cout1(ans)
}