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

#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> g[6000];
bool f[6000];
// int dist[6000][2010];

int main() {
  int n, m, cap;
  while (cin >> n >> m >> cap, n) {
    for (int i = 0; i < 6000; i++)
      g[i].clear();
    memset(f, 0, sizeof(f));
    cap *= 10;
    string src, dst;
    cin >> src >> dst;
    map<string, int> mp;
    mp[src] = mp.size() - 1;
    mp[dst] = mp.size() - 1;
    for (int i = 0; i < n; i++) {
      string a, b;
      int c;
      cin >> a >> b >> c;
      if (!mp.count(a))
        mp[a] = mp.size() - 1;
      if (!mp.count(b))
        mp[b] = mp.size() - 1;
      g[mp[a]].emplace_back(mp[b], c);
      g[mp[b]].emplace_back(mp[a], c);
    }
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      f[mp[s]] = true;
    }
    vector<vector<int>> dist(mp.size(), vector<int>(cap + 1, 1 << 28));
    // fill_n(*dist, 6000 * 2010, 1 << 28);
    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                   greater<tuple<int, int, int>>>
        pq;
    dist[0][cap] = 0;
    pq.push(make_tuple(0, 0, cap));
    int goal = mp[dst];
    int res = -1;
    while (pq.size()) {
      int s, v, r;
      tie(s, v, r) = pq.top();
      pq.pop();
      if (v == goal) {
        res = s;
        break;
      }
      if (dist[v][r] < s)
        continue;
      if (f[v])
        r = cap;
      for (pair<int, int> &e : g[v]) {
        if (r < e.second)
          continue;
        if (dist[e.first][r - e.second] > s + e.second) {
          dist[e.first][r - e.second] = s + e.second;
          pq.push(make_tuple(s + e.second, e.first, r - e.second));
        }
      }
    }
    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> g[6000];
bool f[6000];
int dist[6000][2010];

int main() {
  int n, m, cap;
  while (cin >> n >> m >> cap, n) {
    for (int i = 0; i < 6000; i++)
      g[i].clear();
    memset(f, 0, sizeof(f));
    cap *= 10;
    string src, dst;
    cin >> src >> dst;
    map<string, int> mp;
    mp[src] = mp.size() - 1;
    mp[dst] = mp.size() - 1;
    for (int i = 0; i < n; i++) {
      string a, b;
      int c;
      cin >> a >> b >> c;
      if (!mp.count(a))
        mp[a] = mp.size() - 1;
      if (!mp.count(b))
        mp[b] = mp.size() - 1;
      g[mp[a]].emplace_back(mp[b], c);
      g[mp[b]].emplace_back(mp[a], c);
    }
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      f[mp[s]] = true;
    }
    fill_n(*dist, 6000 * 2010, 1 << 28);
    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                   greater<tuple<int, int, int>>>
        pq;
    dist[0][cap] = 0;
    pq.push(make_tuple(0, 0, cap));
    int goal = mp[dst];
    int res = -1;
    while (pq.size()) {
      int s, v, r;
      tie(s, v, r) = pq.top();
      pq.pop();
      if (v == goal) {
        res = s;
        break;
      }
      if (dist[v][r] < s)
        continue;
      if (f[v])
        r = cap;
      for (pair<int, int> &e : g[v]) {
        if (r < e.second)
          continue;
        if (dist[e.first][r - e.second] > s + e.second) {
          dist[e.first][r - e.second] = s + e.second;
          pq.push(make_tuple(s + e.second, e.first, r - e.second));
        }
      }
    }
    cout << res << endl;
  }
}