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

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

vector<pair<int, string>> edge[40];
string cost[40][500];

int main() {
  while (true) {
    int n, a, s, g;
    cin >> n >> a >> s >> g;
    if (n == 0 && a == 0 && s == 0 && g == 0)
      break;
    rep(i, 40) edge[i].clear();
    rep(i, a) {
      int x, y;
      string s;
      cin >> x >> y >> s;
      edge[x].push_back(make_pair(y, s));
    }
    string INF;
    rep(i, 10000) INF += 'z';
    rep(i, n) rep(j, 500) cost[i][j] = INF;
    priority_queue<pair<string, int>, vector<pair<string, int>>,
                   greater<pair<string, int>>>
        que;
    que.push(make_pair("", s));
    while (!que.empty()) {
      pair<string, int> now = que.top();
      que.pop();
      if (now.second == g) {
        if (now.first.size() >= 250u)
          cout << "NO" << endl;
        else
          cout << now.first << endl;
        goto next;
      }
      if (now.first.size() >= 490u ||
          cost[now.second][now.first.size()] <= now.first)
        continue;
      cost[now.second][now.first.size()] = now.first;
      rep(i, edge[now.second].size()) {
        if (now.first.size() + edge[now.second][i].second.size() >= 490u ||
            cost[edge[now.second][i].first]
                [now.first.size() + edge[now.second][i].second.size()] <=
                now.first + edge[now.second][i].second)
          continue;
        que.push(make_pair(now.first + edge[now.second][i].second,
                           edge[now.second][i].first));
      }
    }
    cout << "NO" << endl;
  next:;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

vector<pair<int, string>> edge[40];
string cost[40][500];

int main() {
  while (true) {
    int n, a, s, g;
    cin >> n >> a >> s >> g;
    if (n == 0 && a == 0 && s == 0 && g == 0)
      break;
    rep(i, 40) edge[i].clear();
    rep(i, a) {
      int x, y;
      string s;
      cin >> x >> y >> s;
      edge[x].push_back(make_pair(y, s));
    }
    string INF;
    rep(i, 10000) INF += 'z';
    rep(i, n) rep(j, 500) cost[i][j] = INF;
    priority_queue<pair<string, int>, vector<pair<string, int>>,
                   greater<pair<string, int>>>
        que;
    que.push(make_pair("", s));
    while (!que.empty()) {
      pair<string, int> now = que.top();
      que.pop();
      if (now.first.size() >= 490u ||
          cost[now.second][now.first.size()] <= now.first)
        continue;
      cost[now.second][now.first.size()] = now.first;
      rep(i, edge[now.second].size()) {
        if (now.first.size() + edge[now.second][i].second.size() >= 490u ||
            cost[edge[now.second][i].first]
                [now.first.size() + edge[now.second][i].second.size()] <=
                now.first + edge[now.second][i].second)
          continue;
        que.push(make_pair(now.first + edge[now.second][i].second,
                           edge[now.second][i].first));
      }
    }
    string res = INF;
    rep(i, 500) res = min(res, cost[g][i]);
    if (res.size() >= 250u)
      cout << "NO" << endl;
    else
      cout << res << endl;
  }
}