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

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define inf 1 << 30
using namespace std;

class State {
public:
  int n, a, dis;
  State(int n, int a, int dis) : n(n), a(a), dis(dis) {}
  bool operator<(State s) const { return s.dis < dis; }
};

int n, m, cap;
int s = 0, d = 1, c;
map<string, int> M;
vector<pair<int, int>> v[6000];
bool sta[6000];
int dp[6000][2002];

void init() {
  M.clear();
  for (int i = 0; i < n * 2; i++) {
    for (int j = 0; j <= 2001; j++) {
      if (i == 0)
        dp[i][j] = 0;
      else
        dp[i][j] = inf;
    }
    sta[i] = false;
    v[i].clear();
  }
}

int main() {
  string a, b;

  while (1) {
    cin >> n >> m >> cap;
    if (n + m + cap == 0)
      break;
    cap *= 10;
    init();
    cin >> a >> b;
    M[a] = s;
    M[b] = d;
    int num = 2;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c;
      if (M.find(a) == M.end()) {
        M[a] = num;
        num++;
      }
      if (M.find(b) == M.end()) {
        M[b] = num;
        num++;
      }
      v[M[a]].push_back(mp(M[b], c));
      v[M[b]].push_back(mp(M[a], c));
    }
    for (int i = 0; i < m; i++) {
      cin >> a;
      sta[M[a]] = true;
    }

    priority_queue<State> pq;
    pq.push(State(0, cap, 0));
    int ans = inf;

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

      if (u.n == 1) {
        ans = u.dis;
        break;
      }

      if (sta[u.n])
        u.a = cap;

      bool flag = true;
      for (int i = u.a; i <= cap; i++) {
        if (dp[u.n][i] < u.dis) {
          flag = false;
          break;
        }
      }
      if (!flag)
        continue;

      for (int i = 0; i < v[u.n].size(); i++) {
        if (v[u.n][i].s <= u.a) {
          int x = u.dis + v[u.n][i].s, y = u.a - v[u.n][i].s, z = v[u.n][i].f;
          if (x < dp[z][y]) {
            dp[z][y] = x;
            pq.push(State(z, y, x));
          }
        }
      }
    }
    if (ans == inf)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define inf 1 << 30
using namespace std;

class State {
public:
  int n, a, dis;
  State(int n, int a, int dis) : n(n), a(a), dis(dis) {}
  bool operator<(State s) const { return s.dis < dis; }
};

int n, m, cap;
int s = 0, d = 1, c;
map<string, int> M;
vector<pair<int, int>> v[6001];
bool sta[6001];
int dp[6001][2002];

void init() {
  M.clear();
  for (int i = 0; i < n * 2 + 1; i++) {
    for (int j = 0; j <= 2001; j++) {
      if (i == 0)
        dp[i][j] = 0;
      else
        dp[i][j] = inf;
    }
    sta[i] = false;
    v[i].clear();
  }
}

int main() {
  string a, b;

  while (1) {
    cin >> n >> m >> cap;
    if (n + m + cap == 0)
      break;
    cap *= 10;
    init();
    cin >> a >> b;
    M[a] = s;
    M[b] = d;
    int num = 2;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c;
      if (M.find(a) == M.end()) {
        M[a] = num;
        num++;
      }
      if (M.find(b) == M.end()) {
        M[b] = num;
        num++;
      }
      v[M[a]].push_back(mp(M[b], c));
      v[M[b]].push_back(mp(M[a], c));
    }
    for (int i = 0; i < m; i++) {
      cin >> a;
      sta[M[a]] = true;
    }

    priority_queue<State> pq;
    pq.push(State(0, cap, 0));
    int ans = inf;

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

      if (u.n == 1) {
        ans = u.dis;
        break;
      }

      if (sta[u.n])
        u.a = cap;
      if (dp[u.n][u.a] < u.dis)
        continue;
      /*
      bool flag=true;
      for(int i=c;i<=cap;i++){
        if(dp[u.n][i]<u.dis)flag=false;
      }
      if(!flag)continue;
      */
      for (int i = 0; i < v[u.n].size(); i++) {
        if (v[u.n][i].s <= u.a) {
          int x = u.dis + v[u.n][i].s, y = u.a - v[u.n][i].s, z = v[u.n][i].f;
          if (x < dp[z][y]) {
            dp[z][y] = x;
            pq.push(State(z, y, x));
          }
        }
      }
    }
    if (ans == inf)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}