Problem p01488 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
vector<string> vec[500000], W;
vector<int> vec2[500000];
int n, t, num;
string a, b;
pair<int, int> dist[500000];
vector<pair<int, int>> X[500000];

pair<int, int> dijkstra(string S, string T) {
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      Q;
  int cnt4 = 0;
  for (int i = 0; i < 500000; i++)
    dist[i] = make_pair(1069999999, 1069999999);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < vec[i].size(); j++) {
      if (S == vec[i][j]) {
        Q.push(make_tuple(0, cnt4, 0));
        dist[cnt4] = make_pair(0, 0);
      }
      cnt4++;
    }
  }
  while (!Q.empty()) {
    int a1 = get<0>(Q.top()), a2 = get<1>(Q.top()), a3 = get<2>(Q.top());
    Q.pop();
    for (int i = 0; i < X[a2].size(); i++) {
      int to = X[a2][i].first;
      int U = 0;
      if (a2 >= num)
        U = 1;
      if (dist[to] > make_pair(a1 + X[a2][i].second, a3 + U)) {
        dist[to] = make_pair(a1 + X[a2][i].second, a3 + U);
        Q.push(make_tuple(dist[to].first, to, dist[to].second));
      }
    }
  }
  cnt4 = 0;
  pair<int, int> minx = make_pair(2139999999, 2139999999);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < vec[i].size(); j++) {
      if (T == vec[i][j]) {
        minx = min(minx, dist[cnt4]);
      }
      cnt4++;
    }
  }
  return minx;
}
int main() {
  cin >> n >> t >> a >> b;
  int cnt1 = 0;
  for (int i = 0; i < n; i++) {
    int E;
    cin >> E;
    for (int j = 0; j < E; j++) {
      string W2;
      cin >> W2;
      W.push_back(W2);
      vec[i].push_back(W2);
      cnt1++;
    }
    for (int j = 0; j < E - 1; j++) {
      int W3;
      cin >> W3;
      vec2[i].push_back(W3);
    }
  }
  int cnt2 = 0;
  sort(W.begin(), W.end());
  vector<string> V;
  V.push_back(W[0]);
  for (int i = 1; i < W.size(); i++) {
    if (W[i - 1] != W[i])
      V.push_back(W[i]);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < vec2[i].size(); j++) {
      X[cnt2].push_back(make_pair(cnt2 + 1, vec2[i][j]));
      X[cnt2 + 1].push_back(make_pair(cnt2, vec2[i][j]));
      cnt2++;
    }
    cnt2++;
  }
  num = cnt2;
  int cnt3 = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < vec[i].size(); j++) {
      int pos1 = lower_bound(W.begin(), W.end(), vec[i][j]) - W.begin();
      X[cnt2 + pos1].push_back(make_pair(cnt3, 0));
      X[cnt3].push_back(make_pair(cnt2 + pos1, t));
      cnt3++;
    }
  }
  pair<int, int> ans = dijkstra(a, b);
  if (ans.first > 1000000000)
    cout << "-1" << endl;
  else
    cout << ans.first << ' ' << ans.second << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX_N 80000
#define INF 1145141919

vector<string> S;
vector<string> T;
string U, V, W;
int n, t, p, q, st, en;
vector<string> R[MAX_N];
vector<int> B[MAX_N];
vector<int> C[MAX_N];
int color[MAX_N];
pair<int, int> d[MAX_N];

vector<pair<int, int>> X[MAX_N];

priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
               greater<tuple<int, int, int>>>
    Q;

pair<int, int> dijkstra(int P1, int Q1) {

  for (int i = 0; i < MAX_N; i++) {
    color[i] = WHITE;
    d[i] = make_pair(INF, INF);
  }

  color[P1] = GRAY;
  d[P1] = make_pair(0, 0);
  Q.push(make_tuple(0, 0, P1));

  while (!Q.empty()) {
    tuple<int, int, int> f = Q.top();
    int A = get<0>(f);
    int C = get<1>(f);
    int B = get<2>(f);
    Q.pop();
    color[B] = BLACK;

    if (d[B].first < A * (-1))
      continue;

    for (int v = 0; v < X[B].size(); v++) {
      int w = X[B][v].first;
      int z = X[B][v].second;

      if (color[w] == BLACK)
        continue;

      if (d[w].first > d[B].first + z) {

        d[w] = make_pair(d[B].first + z, d[B].second + 1);
        Q.push(make_tuple(d[w].first, d[w].second, w));
        color[w] = GRAY;
      }

      if (d[w].first == d[B].first + z && d[w].second > d[B].second + 1) {

        d[w] = make_pair(d[B].first + z, d[B].second + 1);
        Q.push(make_tuple(d[w].first, d[w].second, w));
        color[w] = GRAY;
      }
    }
  }
  return d[Q1];
}

int main() {
  cin >> n >> t;
  cin >> U >> V;
  for (int i = 0; i < n; i++) {
    cin >> p;
    for (int j = 0; j < p; j++) {
      cin >> W;
      R[i].push_back(W);
      S.push_back(W);
    }
    for (int j = 0; j < p - 1; j++) {
      cin >> q;
      if (j == 0) {
        C[i].push_back(q);
      }
      if (j >= 1) {
        C[i].push_back(q + C[i][j - 1]);
      }
    }
  }
  sort(S.begin(), S.end());
  for (int i = 0; i < S.size(); i++) {
    if (i == 0) {
      T.push_back(S[i]);
    } else {
      if (S[i - 1] != S[i]) {
        T.push_back(S[i]);
      }
    }
  }
  st = upper_bound(T.begin(), T.end(), U) - T.begin();
  en = upper_bound(T.begin(), T.end(), V) - T.begin();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < R[i].size(); j++) {
      B[i].push_back(upper_bound(T.begin(), T.end(), R[i][j]) - T.begin());
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < B[i].size(); j++) {
      for (int k = j + 1; k < B[i].size(); k++) {
        if (j == 0) {
          X[B[i][j]].push_back(make_pair(B[i][k], C[i][k - 1] + t));
          X[B[i][k]].push_back(make_pair(B[i][j], C[i][k - 1] + t));
        }
        if (j >= 1) {
          X[B[i][j]].push_back(
              make_pair(B[i][k], C[i][k - 1] - C[i][j - 1] + t));
          X[B[i][k]].push_back(
              make_pair(B[i][j], C[i][k - 1] - C[i][j - 1] + t));
        }
      }
    }
  }
  pair<int, int> r = dijkstra(st, en);
  if (r.first >= INF) {
    cout << "-1" << endl;
  } else {
    cout << r.first - t << ' ' << r.second - 1 << endl;
  }
  return 0;
}