Problem p01488 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

constexpr long long INF64 = 1e18;

using Weight = long long;

struct Edge {
  int src, dst, ds, belong, st, kaisuu;
  Weight weight;
  Edge(int src, int dst, int ds, Weight weight, int belong, int st, int kaisuu)
      : src(src), dst(dst), ds(ds), weight(weight), belong(belong), st(st),
        kaisuu(kaisuu) {}
};

auto operator<(const Edge &e, const Edge &f) {
  if (e.weight != f.weight)
    return e.weight > f.weight;
  else
    return e.kaisuu > f.kaisuu;
}

using Edges = vector<Edge>;
using Graph = vector<Edges>;

pair<Weight, int> dijkstra(const Graph &g, int s, int t, int N, int T) {
  const int V = g.size();
  vector<Weight> dist(100001, INF64);
  vector<int> prev(100001, -1);
  priority_queue<Edge> q;
  /*for(int i=0;i<N;++i){
              dist[s][i]=0;
              prev[s][i]=-2;
      }*/
  for (q.emplace(-2, s, -1, -T, -1, -2, -1); !q.empty();) {
    Edge e = q.top();
    q.pop();
    if (e.belong >= 0) {
      if (prev[e.ds] != -1)
        continue;
      else
        prev[e.ds] = e.src;
    }
    if (e.dst == t)
      return make_pair(e.weight, e.kaisuu);
    for (auto ge : g[e.dst]) {
      int plus = 0;
      if (e.belong != ge.belong || abs(e.st - ge.st) > 1)
        plus = T;
      if (dist[ge.ds] > e.weight + ge.weight + plus) {
        dist[ge.ds] = e.weight + ge.weight + plus;
        if (plus)
          plus = 1;
        q.emplace(ge.src, ge.dst, ge.ds, dist[ge.ds], ge.belong, ge.st,
                  e.kaisuu + plus);
      }
    }
  }
  return make_pair(-1, -1);
}

int main() {
  int N, T;
  cin >> N >> T;
  string A, B;
  cin >> A >> B;
  map<string, int> mp;
  int k = 1, f = 0;
  Graph g(100001);
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    int prev = -1;
    vector<int> station(a);
    for (int j = 0; j < a; ++j) {
      string s;
      cin >> s;
      if (mp[s] == 0)
        mp[s] = k++;
      station[j] = mp[s];
    }
    for (int j = 0; j < a - 1; ++j) {
      Weight t;
      cin >> t;
      g[station[j]].emplace_back(station[j], station[j + 1], f + 1, t, i, j + 1,
                                 -1);
      g[station[j + 1]].emplace_back(station[j + 1], station[j], f, t, i, j + 1,
                                     -1);
      f++;
    }
    f++;
  }
  pair<Weight, int> ans = dijkstra(g, mp[A], mp[B], N, T);
  if (ans.first == -1)
    cout << "-1" << endl;
  else
    cout << ans.first << " " << ans.second << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

constexpr long long INF64 = 1e18;

using Weight = long long;

struct Edge {
  int src, dst, belong, st, kaisuu;
  Weight weight;
  Edge(int src, int dst, Weight weight, int belong, int st, int kaisuu)
      : src(src), dst(dst), weight(weight), belong(belong), st(st),
        kaisuu(kaisuu) {}
};

auto operator<(const Edge &e, const Edge &f) {
  if (e.weight != f.weight)
    return e.weight > f.weight;
  else
    return e.kaisuu > f.kaisuu;
}

using Edges = vector<Edge>;
using Graph = vector<Edges>;

pair<Weight, int> dijkstra(const Graph &g, int s, int t, int N, int T) {
  const int V = g.size();
  vector<vector<Weight>> dist(V, vector<Weight>(N, INF64));
  vector<vector<int>> prev(V, vector<int>(N, -1));
  priority_queue<Edge> q;
  for (int i = 0; i < N; ++i) {
    dist[s][i] = 0;
    prev[s][i] = -2;
  }
  for (q.emplace(-2, s, -T, -1, -2, -1); !q.empty();) {
    Edge e = q.top();
    q.pop();
    if (e.belong >= 0) {
      if (prev[e.dst][e.belong] != -1)
        continue;
      else
        prev[e.dst][e.belong] = e.src;
    }
    if (e.dst == t)
      return make_pair(e.weight, e.kaisuu);
    for (auto ge : g[e.dst]) {
      int plus = 0;
      if (e.belong != ge.belong || abs(e.st - ge.st) != 1)
        plus = T;
      if (dist[ge.dst][ge.belong] > e.weight + ge.weight + plus) {
        dist[ge.dst][ge.belong] = e.weight + ge.weight + plus;
        if (plus)
          plus = 1;
        q.emplace(ge.src, ge.dst, dist[ge.dst][ge.belong], ge.belong, ge.st,
                  e.kaisuu + plus);
      }
    }
  }
  return make_pair(-1, -1);
}

int main() {
  int N, T;
  cin >> N >> T;
  string A, B;
  cin >> A >> B;
  map<string, int> mp;
  int k = 1;
  Graph g(100001);
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    int prev = -1;
    vector<int> station(a);
    for (int j = 0; j < a; ++j) {
      string s;
      cin >> s;
      if (mp[s] == 0)
        mp[s] = k++;
      station[j] = mp[s];
    }
    for (int j = 0; j < a - 1; ++j) {
      Weight t;
      cin >> t;
      g[station[j]].emplace_back(station[j], station[j + 1], t, i, j + 1, -1);
      g[station[j + 1]].emplace_back(station[j + 1], station[j], t, i, j + 1,
                                     -1);
    }
  }
  pair<Weight, int> ans = dijkstra(g, mp[B], mp[A], N, T);
  if (ans.first == -1)
    cout << "-1" << endl;
  else
    cout << ans.first << " " << ans.second << endl;
}