Problem p04003 (C++)

Buggy submission (status: Runtime Error):
// 基本テンプレート (縮小版)

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, M;
map<pii, int> id;
vector<int> stations[100010];
vector<pii> G[100010];
int dist[500010];
int cnt = 0;

int getId(int p, int a) {
  pii pr = make_pair(p, a);
  if (id.count(pr))
    return id[pr];
  else
    return id[pr] = cnt++;
}

signed main() {
  cin >> N >> M;
  rep(i, 0, M) {
    int p, q, c;
    cin >> p >> q >> c;
    p--;
    q--;
    int u = getId(p, c), v = getId(q, c);
    G[u].push_back(make_pair(v, 0));
    G[v].push_back(make_pair(u, 0));
    stations[p].push_back(c);
    stations[q].push_back(c);
  }
  rep(i, 0, N) {
    rep(j, 0, stations[i].size()) {
      int cmp = stations[i][j];
      int u = getId(i, cmp), v = getId(i, 0);
      G[u].push_back(make_pair(v, 0));
      G[v].push_back(make_pair(u, 1));
    }
  }

  rep(i, 0, 500010) dist[i] = INF;
  int init = getId(0, 0), goal = getId(N - 1, 0);
  priority_queue<pii, vector<pii>, greater<pii>> q;
  dist[init] = 0;
  q.push(make_pair(0, init));
  while (!q.empty()) {
    pii t = q.top();
    q.pop();
    int cur = t.second, cost = dist[cur];
    for (auto to : G[cur]) {
      if (dist[to.first] > dist[cur] + to.second) {
        dist[to.first] = dist[cur] + to.second;
        q.push(make_pair(dist[to.first], to.first));
      }
    }
  }
  cout << (dist[goal] == INF ? -1 : dist[goal]) << endl;
}

Fixed submission:
// 基本テンプレート (縮小版)

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, M;
map<pii, int> id;
vector<int> stations[100010];
vector<pii> G[1500010];
int dist[1500010];
int cnt = 0;

int getId(int p, int a) {
  pii pr = make_pair(p, a);
  if (id.count(pr))
    return id[pr];
  else
    return id[pr] = cnt++;
}

signed main() {
  cin >> N >> M;
  rep(i, 0, M) {
    int p, q, c;
    cin >> p >> q >> c;
    p--;
    q--;
    int u = getId(p, c), v = getId(q, c);
    G[u].push_back(make_pair(v, 0));
    G[v].push_back(make_pair(u, 0));
    stations[p].push_back(c);
    stations[q].push_back(c);
  }
  rep(i, 0, N) {
    rep(j, 0, stations[i].size()) {
      int cmp = stations[i][j];
      int u = getId(i, cmp), v = getId(i, 0);
      G[u].push_back(make_pair(v, 0));
      G[v].push_back(make_pair(u, 1));
    }
  }

  rep(i, 0, 500010) dist[i] = INF;
  int init = getId(0, 0), goal = getId(N - 1, 0);
  priority_queue<pii, vector<pii>, greater<pii>> q;
  dist[init] = 0;
  q.push(make_pair(0, init));
  while (!q.empty()) {
    pii t = q.top();
    q.pop();
    int cur = t.second, cost = dist[cur];
    for (auto to : G[cur]) {
      if (dist[to.first] > dist[cur] + to.second) {
        dist[to.first] = dist[cur] + to.second;
        q.push(make_pair(dist[to.first], to.first));
      }
    }
  }
  cout << (dist[goal] == INF ? -1 : dist[goal]) << endl;
}