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

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

struct aa {
  int now;
  int dis;
  int gas;
};
class Compare {
public:
  // keiroが昇順に並ぶ
  bool operator()(const aa &l, const aa &r) { return l.gas > r.gas; }
};
int main() {
  while (1) {
    int N, M, cap;
    cin >> N >> M >> cap;
    cap *= 10;
    if (!N)
      break;
    map<string, int> mp;
    int num = 2;
    vector<vector<pair<int, int>>> edges(6000);
    string sta, goa;
    cin >> sta >> goa;
    mp[sta] = 0;
    mp[goa] = 1;
    for (int i = 0; i < N; ++i) {
      string f, t;
      cin >> f >> t;
      int dis;
      cin >> dis;
      if (!mp.count(f)) {
        mp[f] = num++;
      }
      if (!mp.count(t)) {
        mp[t] = num++;
      }
      const int fid = mp[f];
      const int tid = mp[t];
      edges[fid].push_back(make_pair(tid, dis));
      edges[tid].push_back(make_pair(fid, dis));
    }
    edges.resize(mp.size());
    vector<bool> stat(edges.size(), false);
    for (int i = 0; i < M; ++i) {
      string st;
      cin >> st;
      if (mp.count(st)) {
        stat[mp[st]] = true;
      }
    }
    vector<vector<int>> mindiss(mp.size(), vector<int>(cap + 1, 99999999));
    priority_queue<aa, vector<aa>, Compare> que;
    que.push(aa{0, 0, cap});
    mindiss[0][cap] = 0;

    while (!que.empty()) {
      aa atop(que.top());
      que.pop();
      const int pregas = atop.gas;
      const int nowdis = atop.dis;

      for (auto i : edges[atop.now]) {
        if (pregas >= i.second) {
          const int newid = i.first;
          const int newgas = stat[newid] ? cap : atop.gas - i.second;
          const int newdis = nowdis + i.second;
          if (mindiss[newid][newgas] > newdis) {
            mindiss[newid][newgas] = newdis;
            que.push(aa{newid, newdis, newgas});
          }
        }
      }
    }
    int ans = 99999999;
    for (int i = 0; i < cap + 1; ++i) {
      ans = min(mindiss[1][i], ans);
    }
    if (ans == 99999999) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

bool isso(long long int a) {
  if (a == 1 || a == 0)
    return false;
  for (long long int i = 2; i * i <= a; ++i) {
    if ((a % i)) {
    } else {
      return false;
    }
  }
  return true;
}

long long int powint(long long int a, long long int b) {
  assert(b >= 0);
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  long long int ans = 1;
  long long int aa = powint(a, b / 2);
  ans *= aa * aa;
  if (b % 2)
    ans *= a;
  return ans;
}

struct edge {
  int from;
  int to;
  int cost;
};

struct aa {
  int now;
  int fuel;
  int cost;
};

class Compare {
public:
  // keiroが昇順に並ぶ
  bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }
};

int main() {

  while (1) {
    int N, M, cap;
    cin >> N >> M >> cap;
    if (!N)
      break;
    cap *= 10;
    string st, fi;
    cin >> st >> fi;
    map<string, int> mp;
    mp[st] = 0;
    mp[fi] = 1;
    int num = 2;
    vector<vector<edge>> edges(2 * N);
    for (int i = 0; i < N; ++i) {
      string a, b;
      cin >> a >> b;
      if (mp.count(a)) {
      } else {
        mp.emplace(a, num);
        num++;
      }
      if (mp.count(b)) {
      } else {
        mp.emplace(b, num);
        num++;
      }
      int co;
      cin >> co;
      edges[mp[a]].push_back(edge{mp[a], mp[b], co});
      edges[mp[b]].push_back(edge{mp[b], mp[a], co});
    }
    vector<bool> stands(mp.size(), false);
    for (int i = 0; i < M; ++i) {
      string st;
      cin >> st;
      if (mp.count(st)) {

        stands[mp[st]] = true;
      }
    }

    priority_queue<aa, vector<aa>, Compare> que;
    que.push(aa{0, cap, 0});
    vector<vector<int>> memo(mp.size(), vector<int>(cap + 1, 99999999));
    memo[0][cap] = 0;
    while (!que.empty()) {
      aa atop(que.top());
      if (atop.now == 1)
        break;
      que.pop();
      for (auto i : edges[atop.now]) {
        if (atop.fuel >= i.cost) {
          int nexfuel;
          if (stands[i.to]) {
            nexfuel = cap;
          } else {
            nexfuel = atop.fuel - i.cost;
          }
          if (memo[i.to][atop.fuel - i.cost] > atop.cost + i.cost) {
            que.push({i.to, nexfuel, atop.cost + i.cost});
            memo[i.to][nexfuel] = atop.cost + i.cost;
          }
        }
      }
    }
    int amin = 99999999;
    for (int i = 0; i <= cap; ++i) {
      amin = min(amin, memo[1][i]);
    }
    if (amin == 99999999) {
      cout << -1 << endl;
    } else {

      cout << amin << endl;
    }
  }
  return 0;
}