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

#include <bits/stdc++.h>

using namespace std;

using pii = pair<int, int>;
using Edge = tuple<int, int, int>;
constexpr int MAX = 252521;
constexpr int INF = INT_MAX;

struct State {
  int v, cost, trans, en;

  State() {}
  State(int v, int cost, int trans, int en)
      : v{v}, cost{cost}, trans{trans}, en{en} {}

  bool operator<(const State &s) const {
    if (cost != s.cost) {
      return cost > s.cost;
    }
    return trans > s.trans;
  }
};

int N, T;
vector<Edge> G[MAX];

pii dijkstra() {
  vector<pii> d(MAX, pii(INF, INF));

  priority_queue<State> pq;
  pq.push(State(0, 0, 0, -1));
  while (!pq.empty()) {
    auto st = pq.top();
    pq.pop();
    int v = st.v, cost = st.cost, trans = st.trans, en = st.en;

    if (pii(cost, trans) > d[en])
      continue;

    if (v == 1) {
      return d[en];
    }

    for (const auto &g : G[v]) {
      int to, ncost, nen;
      int ntrans = trans;

      tie(to, ncost, nen) = g;
      ncost += cost;

      if (en != -1 && abs(en - nen) > 1) {
        ncost += T;
        ntrans++;
      }

      if (pii(ncost, ntrans) < d[nen]) {
        d[nen] = pii(ncost, ntrans);
        pq.push(State(to, ncost, ntrans, nen));
      }
    }
  }
  return pii(-1, -1);
}

int main() {
  string A, B;
  map<string, int> s2i;
  int n = 0;

  cin >> N >> T >> A >> B;

  s2i[A] = 0;
  s2i[B] = 1;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    vector<int> v;
    for (int j = 0; j < a; j++) {
      string s;
      cin >> s;
      if (s2i.count(s) == 0) {
        s2i[s] = s2i.size();
      }
      v.emplace_back(s2i[s]);
    }
    for (int j = 0; j < a - 1; j++) {
      int cost;
      cin >> cost;
      G[v[j]].emplace_back(v[j + 1], cost, n + j);
      G[v[j + 1]].emplace_back(v[j], cost, n + j);
    }
    n += a;
  }
  pii res = dijkstra();
  if (res.first == -1) {
    cout << -1 << endl;
  } else {
    cout << res.first << " " << res.second << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using pii = pair<int, int>;
using Edge = tuple<int, int, int, int>;
constexpr int MAX = 100000;
constexpr int INF = (1 << 28);

struct State {
  int v, cost, trans, line, en;

  State() {}
  State(int v, int cost, int trans, int line, int en)
      : v{v}, cost{cost}, trans{trans}, line{line}, en{en} {}

  bool operator<(const State &s) const {
    if (cost != s.cost) {
      return cost > s.cost;
    }
    return trans > s.trans;
  }
};

int N, T, n;
vector<Edge> G[MAX];

pii dijkstra(const vector<int> &init_lines) {
  vector<pii> d(n, pii(INF, INF));
  d[0] = pii(0, 0);

  priority_queue<State> pq;
  for (const auto &l : init_lines) {
    pq.push(State(0, 0, 0, l, -1));
  }

  while (!pq.empty()) {
    auto st = pq.top();
    pq.pop();
    int v = st.v, cost = st.cost, trans = st.trans;
    int line = st.line, en = st.en;

    if (v == 1)
      return d[v];

    for (const auto &g : G[v]) {
      int to = get<0>(g);
      int ncost = cost + get<1>(g);
      int ntrans = trans;

      int diff = abs(en - get<3>(g));
      if (line != get<2>(g) || (en != -1 && diff != 1)) {
        ncost += T;
        ntrans++;
      }

      pii nd = pii(ncost, ntrans);

      if (nd < d[to]) {
        d[to] = nd;
        pq.push(State(to, ncost, ntrans, get<2>(g), get<3>(g)));
      }
    }
  }
  return pii(-1, -1);
}

int main() {
  string A, B;
  map<string, int> s2i;
  vector<int> init_lines;
  int edge_num = 0;

  cin >> N >> T >> A >> B;

  s2i[A] = 0;
  s2i[B] = 1;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    vector<int> v;
    for (int j = 0; j < a; j++) {
      string s;
      cin >> s;
      if (s2i.count(s) == 0) {
        s2i[s] = s2i.size();
      }
      if (s2i[s] == 0 && a >= 2) {
        init_lines.emplace_back(i);
      }
      v.emplace_back(s2i[s]);
    }
    for (int j = 0; j < a - 1; j++) {
      int cost;
      cin >> cost;
      G[v[j]].emplace_back(v[j + 1], cost, i, edge_num);
      G[v[j + 1]].emplace_back(v[j], cost, i, edge_num);
      edge_num++;
    }
  }
  n = s2i.size();
  pii res = dijkstra(init_lines);
  if (res.first == -1) {
    cout << -1 << endl;
  } else {
    cout << res.first << " " << res.second << endl;
  }
  return 0;
}