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

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; }
template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << '[';
  REP(i, a.size()) {
    out << a[i];
    if (i != a.size() - 1)
      out << ',';
  }
  out << ']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

vector<PII> g[6010];
int d[6010][2010];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int n, m, cap;
    cin >> n >> m >> cap;
    cap *= 10;
    if (!n)
      break;
    string src, dst;
    cin >> src >> dst;
    int V = 0;
    map<string, int> mp;
    REP(i, 6010) g[i].clear();
    REP(i, n) {
      int l;
      string s, t;
      cin >> s >> t >> l;
      if (mp.find(s) == mp.end()) {
        mp[s] = V++;
      }
      if (mp.find(t) == mp.end()) {
        mp[t] = V++;
      }
      g[mp[s]].PB({mp[t], l});
      g[mp[t]].PB({mp[s], l});
      // cout << mp[s] << " " << mp[t] << " " << l << endl;
    }
    VI exist(V, 0);
    REP(i, m) {
      string s;
      cin >> s;
      exist[mp[s]] = 1;
    }
    int start = mp[src], goal = mp[dst];
    // cout << V << endl;

    REP(i, V) REP(j, cap + 1) d[i][j] = LLINF;
    d[start][cap] = 0;
    priority_queue<VI, VVI, greater<VI>> que;
    que.push({d[start][cap], start, cap});

    while (que.size()) {
      VI v = que.top();
      que.pop();
      // cout << v << endl;
      int dist = v[0], place = v[1], gas = v[2];
      if (dist > d[place][gas])
        continue;
      for (PII &p : g[place]) {
        int rest = exist[p.first] ? cap : gas - p.second;
        if (gas >= p.second && d[p.first][rest] > d[place][gas] + p.second) {
          d[p.first][rest] = d[place][gas] + p.second;
          que.push({d[p.first][rest], p.first, rest});
        }
      }
    }

    int ans = LLINF;
    REP(i, cap + 1) chmin(ans, d[goal][i]);
    if (ans == LLINF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}


Error: MLE

Fixed submission:

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; }
template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << '[';
  REP(i, a.size()) {
    out << a[i];
    if (i != a.size() - 1)
      out << ',';
  }
  out << ']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

vector<PII> g[3500];
int d[3500][2005];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int n, m, c;
    cin >> n >> m >> c;
    if (!n)
      break;
    c *= 10;
    string s, t;
    cin >> s >> t;
    map<string, int> mp;
    int cnt = 0;
    REP(i, 3500) g[i].clear();
    REP(i, n) {
      string x, y;
      int l;
      cin >> x >> y >> l;
      if (mp.find(x) == mp.end()) {
        mp[x] = cnt++;
      }
      if (mp.find(y) == mp.end()) {
        mp[y] = cnt++;
      }
      g[mp[x]].PB({mp[y], l});
      g[mp[y]].PB({mp[x], l});
    }
    VI exist(cnt, 0);
    REP(i, m) {
      string str;
      cin >> str;
      exist[mp[str]] = 1;
    }
    // cout << exist << endl;

    // for(auto i: mp) cout << i << endl;

    int src = mp[s], dst = mp[t];
    REP(i, 3500) REP(j, 2005) d[i][j] = LLINF;
    d[src][c] = 0;
    priority_queue<VI, VVI, greater<VI>> que;
    que.push({d[src][c], src, c});

    while (que.size()) {
      VI v = que.top();
      que.pop();
      // cout << v << endl;
      if (v[0] > d[v[1]][v[2]])
        continue;
      if (v[1] == dst)
        break;
      for (PII e : g[v[1]]) {
        if (v[2] >= e.second &&
            d[e.first][v[2] - e.second] > d[v[1]][v[2]] + e.second) {
          int tmp = exist[e.first] == 1 ? c : v[2] - e.second;
          d[e.first][tmp] = d[v[1]][v[2]] + e.second;
          que.push({d[e.first][tmp], e.first, tmp});
        }
      }
    }

    int ans = LLINF;
    REP(i, c + 1) chmin(ans, d[dst][i]);
    if (ans == LLINF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}