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

#include <bits/stdc++.h>

using namespace std;

typedef pair<pair<int, int>, pair<int, int>> Pi;

int N, T;
vector<pair<pair<int, int>, int>> g[100000], h[100000];

pair<int, int> Dijkstra(int s, int t) {

  map<pair<int, int>, pair<int, int>> v;
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  que.emplace(make_pair(0, 0), make_pair(s, -1));

  for (auto &e : g[s]) {
    que.emplace(make_pair(0, 0), e.first);
    v[e.first] = {0, 0};
  }

  auto push = [&](pair<int, int> c, pair<int, int> cost) {
    if (!v.count(c) || cost < v[c]) {
      que.emplace(cost, c);
      v[c] = cost;
    }
  };

  while (!que.empty()) {
    auto p = que.top();
    que.pop();

    if (p.first > v[p.second])
      continue;
    if (p.second.second == -1) {
      if (p.second.first == t)
        return p.first;
      for (auto &e : g[p.second.first]) {
        push(e.first, {p.first.first + T, p.first.second + 1});
      }
    } else {
      int add = h[p.second.first][p.second.second].first.first;
      if (~add)
        push({p.second.first, p.second.second - 1},
             {p.first.first + add, p.first.second});
      add = h[p.second.first][p.second.second].first.second;
      if (~add)
        push({p.second.first, p.second.second + 1},
             {p.first.first + add, p.first.second});
      push({h[p.second.first][p.second.second].second, -1}, p.first);
    }
  }
  return {-1, -1};
};

int main() {
  string A, B;
  map<string, int> conv;
  cin >> N >> T;
  cin >> A >> B;
  for (int i = 0; i < N; i++) {
    int sz;
    cin >> sz;
    vector<int> line(sz), tt(sz - 1);
    for (auto &p : line) {
      string s;
      cin >> s;
      if (!conv.count(s))
        conv[s] = conv.size() - 1;
      p = conv[s];
    }
    for (int j = 0; j < sz - 1; j++)
      cin >> tt[j];
    for (int j = 0; j < sz; j++) {
      g[line[j]].emplace_back(make_pair(i, j), 0);
      h[i].emplace_back(
          make_pair(j == 0 ? -1 : tt[j - 1], j + 1 == sz ? -1 : tt[j]),
          line[j]);
    }
  }
  auto get = Dijkstra(conv[A], conv[B]);
  if (~get.first)
    cout << get.first << " " << get.second << endl;
  else
    cout << get.first << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef pair<pair<int, int>, pair<int, int>> Pi;

int N, T;
vector<pair<pair<int, int>, int>> g[50000], h[100000];

pair<int, int> Dijkstra(int s, int t) {

  map<pair<int, int>, pair<int, int>> v;
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  que.emplace(make_pair(0, 0), make_pair(s, -1));

  for (auto &e : g[s]) {
    que.emplace(make_pair(0, 0), e.first);
    v[e.first] = {0, 0};
  }

  auto push = [&](pair<int, int> c, pair<int, int> cost) {
    if (!v.count(c) || cost < v[c]) {
      que.emplace(cost, c);
      v[c] = cost;
    }
  };

  while (!que.empty()) {
    auto p = que.top();
    que.pop();

    if (p.first > v[p.second])
      continue;
    if (p.second.second == -1) {
      if (p.second.first == t)
        return p.first;
      for (auto &e : g[p.second.first]) {
        push(e.first, {p.first.first + T, p.first.second + 1});
      }
    } else {
      int add = h[p.second.first][p.second.second].first.first;
      if (~add)
        push({p.second.first, p.second.second - 1},
             {p.first.first + add, p.first.second});
      add = h[p.second.first][p.second.second].first.second;
      if (~add)
        push({p.second.first, p.second.second + 1},
             {p.first.first + add, p.first.second});
      push({h[p.second.first][p.second.second].second, -1}, p.first);
    }
  }
  return {-1, -1};
};

int main() {

  string A, B;
  map<string, int> conv;

  cin >> N >> T;
  cin >> A >> B;
  for (int i = 0; i < N; i++) {
    int sz;
    cin >> sz;
    vector<int> line(sz), tt(sz - 1);
    for (auto &p : line) {
      string s;
      cin >> s;
      if (!conv.count(s))
        conv[s] = conv.size() - 1;
      p = conv[s];
    }
    for (int j = 0; j < sz - 1; j++)
      cin >> tt[j];
    for (int j = 0; j < sz; j++) {
      g[line[j]].emplace_back(make_pair(i, j), 0);
      h[i].emplace_back(
          make_pair(j == 0 ? -1 : tt[j - 1], j + 1 == sz ? -1 : tt[j]),
          line[j]);
    }
  }
  auto get = Dijkstra(conv[A], conv[B]);
  if (~get.first)
    cout << get.first << " " << get.second << endl;
  else
    cout << get.first << endl;
}