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

#include <bits/stdc++.h>
#define N 6001
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
vector<P> G[N];
int n, m, cap, ls[N];
void add_edge(int a, int b, int d) {
  G[a].push_back(P(b, d));
  G[b].push_back(P(a, d));
}

int dijkstra() {
  map<P, int> D;
  int used[N] = {};
  priority_queue<PP, vector<PP>, greater<PP>> Q;
  Q.push(PP(0, P(cap, 0)));

  D[P(cap, 0)] = 0;
  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    int d = t.first, c = t.second.first, pos = t.second.second;

    if (pos == 1)
      return d;
    if (D[P(c, pos)] < d)
      continue;
    if (ls[pos])
      c = cap;
    if (used[pos] > c)
      continue;
    used[pos] = c;

    for (int i = 0; i < G[pos].size(); i++) {
      int nd = d + G[pos][i].second;
      int nx = G[pos][i].first;
      int nc = c - G[pos][i].second;
      if (nc >= 0 && (!D.count(P(nc, nx)) || D[P(nc, nx)] > nd))
        Q.push(PP(nd, P(nc, nx))), D[P(nc, nx)] = nd;
    }
  }
  return -1;
}

int main() {
  while (1) {
    cin >> n >> m >> cap;
    cap *= 10;
    if (!n && !m && !cap)
      break;
    for (int i = 0; i < N; i++)
      G[i].clear(), ls[i] = 0;

    map<string, int> M;
    string a, b;
    cin >> a >> b;
    M[a] = 0, M[b] = 1;
    int _n = 2;
    for (int i = 0, d; i < n; i++) {
      cin >> a >> b >> d;
      if (!M.count(a))
        M[a] = _n++;
      if (!M.count(b))
        M[b] = _n++;
      add_edge(M[a], M[b], d);
    }
    n = _n;
    for (int i = 0; i < m; i++) {
      cin >> a;
      if (M.count(a))
        ls[M[a]] = 1;
    }
    cout << dijkstra() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define N 6001
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
vector<P> G[N];
int n, m, cap, ls[N];
void add_edge(int a, int b, int d) {
  G[a].push_back(P(b, d));
  G[b].push_back(P(a, d));
}

int dijkstra() {
  map<P, int> D;
  priority_queue<PP, vector<PP>, greater<PP>> Q;
  Q.push(PP(0, P(cap, 0)));
  D[P(cap, 0)] = 0;
  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    int d = t.first, c = t.second.first, pos = t.second.second;
    if (pos == 1)
      return d;
    if (D[P(c, pos)] < d)
      continue;
    if (ls[pos])
      c = cap;

    for (int i = 0; i < G[pos].size(); i++) {
      int nd = d + G[pos][i].second;
      int nx = G[pos][i].first;
      int nc = c - G[pos][i].second;
      if (nc >= 0 && (!D.count(P(nc, nx)) || D[P(nc, nx)] > nd))
        Q.push(PP(nd, P(nc, nx))), D[P(nc, nx)] = nd;
    }
  }
  return -1;
}

int main() {
  while (1) {
    cin >> n >> m >> cap;
    cap *= 10;
    if (!n && !m && !cap)
      break;
    for (int i = 0; i < N; i++)
      G[i].clear(), ls[i] = 0;

    map<string, int> M;
    string a, b;
    cin >> a >> b;
    M[a] = 0, M[b] = 1;
    int _n = 2;
    for (int i = 0, d; i < n; i++) {
      cin >> a >> b >> d;
      if (!M.count(a))
        M[a] = _n++;
      if (!M.count(b))
        M[b] = _n++;
      add_edge(M[a], M[b], d);
    }
    n = _n;
    for (int i = 0; i < m; i++) {
      cin >> a;
      if (M.count(a))
        ls[M[a]] = 1;
    }
    cout << dijkstra() << endl;
  }
  return 0;
}