Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 600005, INF = 1 << 30;

vector<pair<int, int>> S[MAX];
vector<int> dis(MAX);

void dijkstra(int u) {
  dis[u] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      PQ;

  PQ.push(make_pair(0, u));
  while (!PQ.empty()) {
    int a = PQ.top().first;
    int b = PQ.top().second;
    PQ.pop();
    if (dis[b] < a)
      continue;
    for (int i = 0; i < S[b].size(); i++) {
      int c = S[b][i].first;
      int d = S[b][i].second;
      if (dis[c] > dis[b] + d) {
        dis[c] = dis[b] + d;
        PQ.push(make_pair(dis[c], c));
      }
    }
  }
  return;
} // a,dが長さ

int main() {

  int N, M;
  cin >> N >> M;
  map<pair<int, int>, int> MA;

  for (int i = 0; i < 600005; i++) {
    dis[i] = INF;
  }

  int now = 1;
  for (int i = 0; i < M; i++) {
    int a, b, c, d, e;
    cin >> a >> b >> c;
    if (MA[make_pair(a, c)]) {
      if (MA[make_pair(b, c)]) {
        d = MA[make_pair(a, c)];
        e = MA[make_pair(b, c)];
      } else {
        d = MA[make_pair(a, c)];
        e = now;
        MA[make_pair(b, c)] = e;
        now++;
      }
    } else {
      if (MA[make_pair(b, c)]) {
        d = now;
        e = MA[make_pair(b, c)];
        MA[make_pair(a, c)] = d;
        now++;
      } else {
        d = now;
        now++;
        e = now;
        now++;
        MA[make_pair(a, c)] = d;
        MA[make_pair(b, c)] = e;
      }
    }
    S[d].push_back(make_pair(e, 0));
    S[e].push_back(make_pair(d, 0));
    S[d].push_back(make_pair(500000 + c, 0));
    S[e].push_back(make_pair(500000 + c, 0));
    S[500000 + c].push_back(make_pair(d, 1));
    S[500000 + c].push_back(make_pair(e, 1));
  }

  dijkstra(500001);

  if (dis[500000 + N] != INF)
    cout << dis[500000 + N] << endl;
  else
    cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 600005, INF = 1 << 30;

vector<pair<int, int>> S[MAX];
vector<int> dis(MAX);

void dijkstra(int u) {
  dis[u] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      PQ;

  PQ.push(make_pair(0, u));
  while (!PQ.empty()) {
    int a = PQ.top().first;
    int b = PQ.top().second;
    PQ.pop();
    if (dis[b] < a)
      continue;
    for (int i = 0; i < S[b].size(); i++) {
      int c = S[b][i].first;
      int d = S[b][i].second;
      if (dis[c] > dis[b] + d) {
        dis[c] = dis[b] + d;
        PQ.push(make_pair(dis[c], c));
      }
    }
  }
  return;
} // a,dが長さ

int main() {

  int N, M;
  cin >> N >> M;
  map<pair<int, int>, int> MA;

  for (int i = 0; i < 600005; i++) {
    dis[i] = INF;
  }

  int now = 1;
  for (int i = 0; i < M; i++) {
    int a, b, c, d, e;
    cin >> a >> b >> c;
    if (MA[make_pair(a, c)]) {
      if (MA[make_pair(b, c)]) {
        d = MA[make_pair(a, c)];
        e = MA[make_pair(b, c)];
      } else {
        d = MA[make_pair(a, c)];
        e = now;
        MA[make_pair(b, c)] = e;
        now++;
      }
    } else {
      if (MA[make_pair(b, c)]) {
        d = now;
        e = MA[make_pair(b, c)];
        MA[make_pair(a, c)] = d;
        now++;
      } else {
        d = now;
        now++;
        e = now;
        now++;
        MA[make_pair(a, c)] = d;
        MA[make_pair(b, c)] = e;
      }
    }
    S[d].push_back(make_pair(e, 0));
    S[e].push_back(make_pair(d, 0));
    S[d].push_back(make_pair(500000 + a, 0));
    S[e].push_back(make_pair(500000 + b, 0));
    S[500000 + a].push_back(make_pair(d, 1));
    S[500000 + b].push_back(make_pair(e, 1));
  }

  dijkstra(500001);

  if (dis[500000 + N] != INF)
    cout << dis[500000 + N] << endl;
  else
    cout << -1 << endl;
}