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

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

struct edge {
  int n, c;
  edge(int n = 0, int c = 0) : n(n), c(c) {}
};

struct state {
  int n, c, cap;
  state(int n = 0, int c = 0, int cap = 0) : n(n), c(c), cap(cap) {}
  bool operator<(const state &s) const { return c > s.c; }
};

const int INF = 1 << 29;
bool lpg[6000];
int n, m, cap, cnt, d[6000][2001], goal;
map<string, int> mp;
vector<edge> G[6000];

void init() {
  cnt = 0;
  mp.clear();
  for (int i = 0; i < 6000; i++)
    G[i].clear();
  fill(lpg, lpg + 6000, false);
  fill(d[0], d[6000], INF);
}

void solve() {
  priority_queue<state> Q;
  state u(0, 0, cap), v;
  d[0][cap] = 0;

  for (Q.push(u); !Q.empty();) {
    u = Q.top();
    Q.pop();
    // cout << u.n << ' ' << u.c << ' ' << u.cap << endl;

    if (u.n == goal) {
      cout << u.c << endl;
      return;
    } else if (u.c > d[u.n][u.cap])
      continue;

    for (int i = 0; i < G[u.n].size(); i++) {
      v = state(G[u.n][i].n, u.c + G[u.n][i].c, u.cap - G[u.n][i].c);
      if (v.cap >= 0 && v.c < d[v.n][v.cap]) {
        if (lpg[v.n])
          v.cap = cap;
        for (int j = v.cap; j >= 0; j--)
          d[v.n][j] = min(d[v.n][j], v.c);
        Q.push(v);
      }
    }
  }
  cout << -1 << endl;
}

int main() {
  while (cin >> n >> m >> cap && (n | m | cap)) {
    string str, str2;
    init();
    cin >> str >> str2;
    mp[str] = cnt++;
    if (mp.find(str2) == mp.end())
      mp[str2] = cnt++;
    goal = mp[str2];
    cap *= 10;

    for (int i = 0; i < n; i++) {
      int c;
      cin >> str >> str2 >> c;
      if (mp.find(str) == mp.end())
        mp[str] = cnt++;
      if (mp.find(str2) == mp.end())
        mp[str2] = cnt++;
      // cout << mp[str] << ' ' << mp[str2] << ' ' << c << endl;
      G[mp[str]].push_back(edge(mp[str2], c));
      G[mp[str2]].push_back(edge(mp[str], c));
    }

    for (int i = 0; i < m; i++) {
      cin >> str;
      lpg[mp[str]] = true;
    }

    /*
for(int i=0;i<6;i++){
      for(int j=0;j<G[i].size();j++) cout << i << " -> " << G[i][j].n << " : "
<< G[i][j].c << endl;
    }
    */

    solve();
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

const int INF = (1 << 21);

class Data {
public:
  int d;
  short cap;
  string str;
  Data(int d = 0, short c = 0) : d(d), cap(c) {}
  bool operator<(const Data &a) const { return d > a.d; }
};

int main() {
  int n, n2;
  short cap;
  while (cin >> n >> n2 >> cap && n && n2 && cap) {
    cap *= 10;
    map<pair<string, string>, int> m;
    string src, dest;
    set<string> s, sta;
    map<string, int> d[2001];

    cin >> src >> dest;
    for (int i = 0; i < n; i++) {
      int a;
      pair<string, string> p;
      cin >> p.first >> p.second >> a;
      s.insert(p.first);
      s.insert(p.second);
      m[p] = a;
    }
    for (int i = 0; i < n2; i++) {
      string str;
      cin >> str;
      sta.insert(str);
    }

    set<string>::iterator it = s.begin();
    // for(;it != s.end();it++) for(int i=0;i<=cap;i++) d[i][*it] = INF;

    priority_queue<Data> Q;
    Data u, v;
    u.cap = cap;
    u.str = src;
    Q.push(u);
    d[cap][src] = 0;

    while (!Q.empty()) {
      u = Q.top();
      Q.pop();

      it = s.begin();
      for (; it != s.end(); it++) {
        v = u;
        if (*it == u.str)
          continue;
        pair<string, string> p;
        p.first = u.str;
        p.second = *it;

        pair<string, string> p2 = p;
        if (m[p2] == 0) {
          swap(p2.first, p2.second);
        }

        v.cap -= m[p2];
        if (v.cap < 0)
          continue;
        if (m[p2] == 0)
          continue;
        if (p.second == src)
          continue;

        if (d[v.cap][p.second] == 0)
          d[v.cap][p.second] = INF;

        if (sta.find(p.second) != sta.end()) {

          if (d[cap][p.second] == 0)
            d[cap][p.second] = INF;

          if (d[u.cap][p.first] + m[p2] < d[cap][p.second]) {
            d[cap][p.second] = d[u.cap][p.first] + m[p2];
            v.str = p.second;
            v.cap = cap;
            v.d = d[cap][p.second];
            if (p.second != dest)
              Q.push(v);
          }
        } else if (v.cap > 0 &&
                   d[u.cap][p.first] + m[p2] < d[v.cap][p.second]) {
          for (int i = 0; i <= v.cap; i++) {
            d[i][p.second] = d[u.cap][p.first] + m[p2];
          }
          v.str = p.second;
          v.d = d[v.cap][p.second];
          if (p.second != dest)
            Q.push(v);
        }
      }
    }

    int ans = INF;
    for (int i = 0; i <= cap; i++)
      if (d[i][dest] != 0)
        ans = min(ans, d[i][dest]);
    if (ans >= INF)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}