Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-15
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
int vnum;
vpint adj[300001];
int d[300001], color[300001], p[300001];
void dijkstra(int s) {
  priority_queue<pint> PQ;
  rep(i, vnum) {
    d[i] = INF;
    color[i] = 0;
  }
  d[s] = 0;
  color[s] = 1;
  PQ.push(mp(0, s));
  while (!PQ.empty()) {
    pint f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = 2;
    if (d[u] < -f.first)
      continue;
    rep(j, adj[u].size()) {
      int v = adj[u][j].first;
      if (color[v] == 2)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(mp(-d[v], v));
        color[v] = 1;
      }
    }
  }
}
int P[200001], Q[200001], c[200001];
vint stacomp[100001];
int main() {
  int N, M;
  map<pint, bool> alreadystacomp;
  vpint vindex;
  map<pint, int> rvindex;
  scan(N);
  scan(M);
  rep(i, M) {
    scan(P[i]);
    scan(Q[i]);
    scan(c[i]);
    P[i]--;
    Q[i]--;
    c[i]--;
    if (alreadystacomp[mp(P[i], c[i])] == false) {
      stacomp[P[i]].push_back(c[i]);
      alreadystacomp[mp(P[i], c[i])] = true;
      rvindex[mp(P[i], c[i])] = vindex.size();
      vindex.push_back(mp(P[i], c[i]));
    }
    if (alreadystacomp[mp(Q[i], c[i])] == false) {
      stacomp[Q[i]].push_back(c[i]);
      alreadystacomp[mp(Q[i], c[i])] = true;
      rvindex[mp(Q[i], c[i])] = vindex.size();
      vindex.push_back(mp(Q[i], c[i]));
    }
    adj[rvindex[mp(P[i], c[i])]].push_back(mp(rvindex[mp(Q[i], c[i])], 0));
    adj[rvindex[mp(Q[i], c[i])]].push_back(mp(rvindex[mp(P[i], c[i])], 0));
  }
  rep(i, N) {
    rvindex[mp(i, -1)] = vindex.size();
    vindex.push_back(mp(i, -1));
    rep(j, stacomp[i].size()) {
      adj[rvindex[mp(i, stacomp[i][j])]].push_back(mp(rvindex[mp(i, -1)], 0));
      adj[rvindex[mp(i, -1)]].push_back(mp(rvindex[mp(i, stacomp[i][j])], 1));
    }
  }
  vnum = vindex.size();
  dijkstra(rvindex[mp(0, -1)]);
  if (d[rvindex[mp(N - 1, -1)]] == INF)
    prin(-1);
  else
    prin(d[rvindex[mp(N - 1, -1)]]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-15
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
int vnum;
vpint adj[500001];
int d[500001], color[500001];
void dijkstra(int s) {
  priority_queue<pint> PQ;
  rep(i, vnum) {
    d[i] = INF;
    color[i] = 0;
  }
  d[s] = 0;
  color[s] = 1;
  PQ.push(mp(0, s));
  while (!PQ.empty()) {
    pint f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = 2;
    if (d[u] < -f.first)
      continue;
    rep(j, adj[u].size()) {
      int v = adj[u][j].first;
      if (color[v] == 2)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(mp(-d[v], v));
        color[v] = 1;
      }
    }
  }
}
int P[200001], Q[200001], c[200001];
vint stacomp[100001];
int main() {
  int N, M;
  map<pint, bool> alreadystacomp;
  vpint vindex;
  map<pint, int> rvindex;
  scan(N);
  scan(M);
  rep(i, M) {
    scan(P[i]);
    scan(Q[i]);
    scan(c[i]);
    P[i]--;
    Q[i]--;
    c[i]--;
    if (alreadystacomp[mp(P[i], c[i])] == false) {
      stacomp[P[i]].push_back(c[i]);
      alreadystacomp[mp(P[i], c[i])] = true;
      rvindex[mp(P[i], c[i])] = vindex.size();
      vindex.push_back(mp(P[i], c[i]));
    }
    if (alreadystacomp[mp(Q[i], c[i])] == false) {
      stacomp[Q[i]].push_back(c[i]);
      alreadystacomp[mp(Q[i], c[i])] = true;
      rvindex[mp(Q[i], c[i])] = vindex.size();
      vindex.push_back(mp(Q[i], c[i]));
    }
    adj[rvindex[mp(P[i], c[i])]].push_back(mp(rvindex[mp(Q[i], c[i])], 0));
    adj[rvindex[mp(Q[i], c[i])]].push_back(mp(rvindex[mp(P[i], c[i])], 0));
  }
  rep(i, N) {
    rvindex[mp(i, -1)] = vindex.size();
    vindex.push_back(mp(i, -1));
    rep(j, stacomp[i].size()) {
      adj[rvindex[mp(i, stacomp[i][j])]].push_back(mp(rvindex[mp(i, -1)], 0));
      adj[rvindex[mp(i, -1)]].push_back(mp(rvindex[mp(i, stacomp[i][j])], 1));
    }
  }
  vnum = vindex.size();
  dijkstra(rvindex[mp(0, -1)]);
  if (d[rvindex[mp(N - 1, -1)]] == INF)
    prin(-1);
  else
    prin(d[rvindex[mp(N - 1, -1)]]);
  return 0;
}