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

#include <algorithm>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef pair<int, P> state;
const int INF = 1 << 28;

struct edge {
  int to;
  int cost;
  edge(){};
  edge(int to, int cost) {
    this->to = to;
    this->cost = cost;
  }
};
map<string, int> names;
int memo[4100][2100];
bool LPG[4100];
map<int, vector<edge>> G;

int main() {
  int cnt;
  int n, m, cap, d;
  string src, dst;
  string c1, c2, str;
  while (cin >> n >> m >> cap && (n || m || cap)) {
    int res = INF;
    cnt = 1;
    cap *= 10;
    cin >> src >> dst;

    names.clear();
    G.clear();
    memset(LPG, 0, sizeof(LPG));
    fill(&memo[0][0], &memo[0][0] + 4100 * 2100, INF);

    rep(i, n) {
      cin >> c1 >> c2 >> d;
      if (!names[c1])
        names[c1] = cnt++;
      if (!names[c2])
        names[c2] = cnt++;
      G[names[c1]].push_back(edge(names[c2], d));
      G[names[c2]].push_back(edge(names[c1], d));
    }
    rep(i, m) {
      cin >> str;
      LPG[names[str]] = true;
    }
    int s = names[src];
    int t = names[dst];
    memo[s][cap] = 0;
    priority_queue<state, vector<state>, greater<state>> que;
    que.push(state(0, P(s, cap)));
    while (!que.empty()) {
      state st = que.top();
      que.pop();
      int cost = st.first;
      int pos = st.second.first;
      int tank = st.second.second;
      if (cost > memo[pos][tank])
        continue;
      if (pos == t)
        res = min(res, cost);
      rep(i, (int)G[pos].size()) {
        int need = G[pos][i].cost;
        int nxt = G[pos][i].to;

        if (tank - need >= 0) {
          int ntank = LPG[nxt] ? cap : tank - need;
          if (memo[nxt][ntank] > cost + need) {
            memo[nxt][ntank] = cost + need;
            que.push(state(cost + need, P(nxt, ntank)));
          }
        }
      }
    }
    if (res == INF)
      res = -1;
    cout << res << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef pair<int, P> state;
const int INF = 1 << 28;

struct edge {
  int to;
  int cost;
  edge(){};
  edge(int to, int cost) {
    this->to = to;
    this->cost = cost;
  }
};
map<string, int> names;
int memo[6100][2100];
bool LPG[6100];
vector<edge> G[6100];

int main() {
  int cnt;
  int n, m, cap, d;
  string src, dst;
  string c1, c2, str;
  while (cin >> n >> m >> cap && (n || m || cap)) {
    int res = INF;
    cnt = 1;
    cap *= 10;
    cin >> src >> dst;

    names.clear();
    rep(i, 6100) G[i].clear();
    memset(LPG, 0, sizeof(LPG));
    fill(&memo[0][0], &memo[0][0] + 6100 * 2100, INF);

    rep(i, n) {
      cin >> c1 >> c2 >> d;
      if (!names[c1])
        names[c1] = cnt++;
      if (!names[c2])
        names[c2] = cnt++;
      G[names[c1]].push_back(edge(names[c2], d));
      G[names[c2]].push_back(edge(names[c1], d));
    }
    rep(i, m) {
      cin >> str;
      LPG[names[str]] = true;
    }
    int s = names[src];
    int t = names[dst];
    memo[s][cap] = 0;
    priority_queue<state, vector<state>, greater<state>> que;
    que.push(state(0, P(s, cap)));
    while (!que.empty()) {
      state st = que.top();
      que.pop();
      int cost = st.first;
      int pos = st.second.first;
      int tank = st.second.second;
      if (cost > memo[pos][tank])
        continue;
      if (pos == t)
        res = min(res, cost);
      rep(i, (int)G[pos].size()) {
        int need = G[pos][i].cost;
        int nxt = G[pos][i].to;

        if (tank - need >= 0) {
          int ntank = LPG[nxt] ? cap : tank - need;
          if (memo[nxt][ntank] > cost + need) {
            memo[nxt][ntank] = cost + need;
            que.push(state(cost + need, P(nxt, ntank)));
          }
        }
      }
    }
    if (res == INF)
      res = -1;
    cout << res << endl;
  }
  return 0;
}