Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end

int n, m;
vector<P> subways;
vector<vector<P>> g;
ll dist[200010];
int s, t;
void dijkstra() {
  Fill(dist, INF);
  dist[s] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push({0, s});
  while (pq.size()) {
    P p = pq.top();
    pq.pop();
    int d = p.first, v = p.second;
    if (dist[v] < d)
      continue;
    for (P to : g[v]) {
      int nxtv = to.first;
      if (chmin(dist[nxtv], dist[v] + to.second))
        pq.push({dist[nxtv], nxtv});
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, 0, n) subways.push_back({i, -1});
  vector<int> u(m), v(m), w(m);
  rep(i, 0, m) {
    scanf("%d%d%d", &u[i], &v[i], &w[i]);
    u[i]--;
    v[i]--;
    subways.push_back({u[i], w[i]});
    subways.push_back({v[i], w[i]});
  }
  sort(ALL(subways));
  subways.erase(unique(ALL(subways)), subways.end());
  int sz = subways.size();
  map<P, int> enco;
  rep(i, 0, sz) enco[subways[i]] = i;
  g.resize(sz);
  rep(i, 0, sz) {
    if (subways[i].second == -1)
      continue;
    int sta = subways[i].first;
    int reach = enco[{sta, -1}];
    g[i].push_back({reach, 0});
    g[reach].push_back({i, 1});
  }
  rep(i, 0, m) {
    int sta = enco[{u[i], w[i]}], reach = enco[{v[i], w[i]}];
    g[sta].push_back({reach, 0});
    g[reach].push_back({sta, 0});
  }
  s = enco[{0, -1}], t = enco[{n - 1, -1}];
  dijkstra();
  printf(dist[t] == INF ? "-1\n" : "%lld\n", dist[t]);
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end

int n, m;
vector<P> subways;
vector<vector<P>> g;
ll dist[500010];
int s, t;
void dijkstra() {
  Fill(dist, INF);
  dist[s] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push({0, s});
  while (pq.size()) {
    P p = pq.top();
    pq.pop();
    int d = p.first, v = p.second;
    if (dist[v] < d)
      continue;
    for (P to : g[v]) {
      int nxtv = to.first;
      if (chmin(dist[nxtv], dist[v] + to.second))
        pq.push({dist[nxtv], nxtv});
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, 0, n) subways.push_back({i, -1});
  vector<int> u(m), v(m), w(m);
  rep(i, 0, m) {
    scanf("%d%d%d", &u[i], &v[i], &w[i]);
    u[i]--;
    v[i]--;
    subways.push_back({u[i], w[i]});
    subways.push_back({v[i], w[i]});
  }
  sort(ALL(subways));
  subways.erase(unique(ALL(subways)), subways.end());
  int sz = subways.size();
  map<P, int> enco;
  rep(i, 0, sz) enco[subways[i]] = i;
  g.resize(sz);
  rep(i, 0, sz) {
    if (subways[i].second == -1)
      continue;
    int sta = subways[i].first;
    int reach = enco[{sta, -1}];
    g[i].push_back({reach, 0});
    g[reach].push_back({i, 1});
  }
  rep(i, 0, m) {
    int sta = enco[{u[i], w[i]}], reach = enco[{v[i], w[i]}];
    g[sta].push_back({reach, 0});
    g[reach].push_back({sta, 0});
  }
  s = enco[{0, -1}], t = enco[{n - 1, -1}];
  dijkstra();
  printf(dist[t] == INF ? "-1\n" : "%lld\n", dist[t]);
  return 0;
}