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

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;
int main(void) {
  for (;;) {
    int n, a, s, g;
    cin >> n >> a >> s >> g;
    if (!n)
      return 0;
    vector<pair<string, pair<int, int>>> e(a);
    REP(i, a) cin >> e[i].second.first >> e[i].second.second >> e[i].first;
    vector<vector<int>> wf(n, vector<int>(n, 1048576));
    REP(i, a) wf[e[i].second.first][e[i].second.second] = 1;
    REP(i, n) wf[i][i] = 0;
    REP(k, n) REP(i, n) REP(j, n) wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j]);
    if (wf[s][g] > 100) {
      cout << "NO" << endl;
      continue;
    }
    vector<vector<pair<string, pair<int, int>>>> us(
        n, vector<pair<string, pair<int, int>>>());
    REP(i, a)
    if (wf[e[i].second.second][g] < 100) us[e[i].second.first].push_back(e[i]);
    REP(i, n) SORT(us[i]);
    REP(i, n) {
      if (us[i].size() < 2)
        continue;
      auto j = us[i].begin();
      for (; j + 1 != us[i].end(); ++j) {
        string l = j->first, r = (j + 1)->first;
        int len = min(l.size(), r.size());
        if (string(l.begin(), l.begin() + len) <
            string(r.begin(), r.begin() + len))
          break;
      }
      us[i] = vector<pair<string, pair<int, int>>>(us[i].begin(), j + 1);
    }
    vector<string> sf(n, "");
    REP(i, n) {
      if (us[i].size() < 1)
        continue;
      sf[i] = us[i].back().first;
    }
    vector<vector<pair<int, int>>> ll(n, vector<pair<int, int>>());
    REP(i, n) {
      for (auto j : us[i])
        ll[i].push_back(make_pair(j.first.size(), j.second.second));
    }
    string sp(256, 'z' + 1);
    vector<set<int>> re(300, set<int>());
    re[0].insert(s);
    int anslen = 256;
    REP(i, 250) {
      for (int pos : re[i]) {
        if (pos == g) {
          anslen = i;
          break;
        }
        for (pair<int, int> mov : ll[pos]) {
          bool flag = true;
          int j;
          for (j = 0; j < mov.first; ++j) {
            if (sf[pos][j] > sp[i + j]) {
              flag = false;
              break;
            } else if (sf[pos][j] < sp[i + j]) {
              break;
            }
          }
          if (!flag)
            continue;
          if (j != mov.first)
            REP(k, 6) re[i + j + k + 1].clear();
          sp.replace(i, mov.first, sf[pos], 0, mov.first);
          re[i + mov.first].insert(mov.second);
        }
      }
      if (anslen < 256)
        break;
    }
    if (anslen < 256)
      cout << string(sp.begin(), sp.begin() + anslen) << endl;
    else
      cout << "NO" << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;
class Edge {
public:
  int x, y;
  string lab;
  Edge(int l, int r, string str) {
    x = l;
    y = r;
    lab = str;
  }
  Edge(void) { Edge(0, 0, ""); }
};
vector<vector<int>> wf;
vector<Edge> e;
vector<vector<Edge>> ei;
string sp;
int main(void) {
  for (;;) {
    int n, a, s, g;
    cin >> n >> a >> s >> g;
    if (!n)
      return 0;
    e.resize(a);
    REP(i, a) cin >> e[i].x >> e[i].y >> e[i].lab;
    wf.assign(n, vector<int>(n, 1048576));
    REP(i, a) wf[e[i].x][e[i].y] = 1;
    REP(i, n) wf[i][i] = 0;
    REP(k, n) REP(i, n) REP(j, n) wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j]);
    if (wf[s][g] > 100) {
      cout << "NO" << endl;
      continue;
    }
    ei.assign(n, vector<Edge>());
    for (Edge i : e)
      if (wf[i.x][g] < 100)
        ei[i.x].push_back(i);
    sp = "";
    queue<pair<string, int>> pq;
    pq.push(make_pair("", s));
    while (!pq.empty()) {
      string tmp = pq.front().first;
      int pos = pq.front().second;
      pq.pop();
      if (sp.size() > 250)
        continue;
      int len = min(sp.size(), tmp.size());
      if (string(sp.begin(), sp.begin() + len) <
          string(tmp.begin(), tmp.begin() + len))
        continue;
      sp = tmp;
      if (pos == g)
        break;
      for (Edge i : ei[pos]) {
        int slen = min(sp.size(), tmp.size() + i.lab.size());
        string stmp = tmp + i.lab;
        if (string(sp.begin(), sp.begin() + slen) <
            string(stmp.begin(), stmp.begin() + slen))
          continue;
        pq.push(make_pair(tmp + i.lab, i.y));
      }
    }
    if (sp.size() > 250)
      cout << "NO" << endl;
    else
      cout << sp << endl;
  }
}