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

#include <iostream>
#include <queue>

using namespace std;

struct edge {
  int t, c, rev;
};

bool u[52][52][1 << 10];

struct S {
  int g, r, c, b;
  bool operator<(S s) const { return c > s.c; };
};

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    int d[52] = {}, e[52];
    e[1] = 0;
    e[n] = 1000;
    int nalt[1234] = {};
    int nth[52] = {};
    for (int i = 2; i < n; i++) {
      cin >> d[i] >> e[i];
      nth[i] = nalt[e[i]]++;
    }
    vector<edge> G[52];
    while (m--) {
      int a, b, c;
      cin >> a >> b >> c;
      if (e[a] <= e[b]) {
        G[a].push_back({b, c, false});
      }
      if (e[b] <= e[a]) {
        G[b].push_back({a, c, true});
      }
    }
    fill(**begin(u), **end(u), false);
    priority_queue<S> que;
    que.push({1, 1, 0, 0});
    while (!que.empty()) {
      auto c = que.top();
      if (c.g == n && c.r == n)
        break;
      que.pop();
      if (u[c.g][c.r][c.b]++)
        continue;
      for (auto f : G[c.g]) {
        if (f.rev)
          continue;
        int nb = (e[f.t] > e[c.r]) ? 0 : c.b;
        int nc = c.c + d[f.t] + f.c;
        if (e[f.t] >= e[c.r]) {
          if (!(nb >> nth[f.t] & 1)) {
            nb |= 1 << nth[f.t];
          } else {
            nc -= d[f.t];
          }
        }
        if (!u[f.t][c.r][nb]) {
          que.push({f.t, c.r, nc, nb});
        }
      }
      for (auto f : G[c.r]) {
        if (!f.rev)
          continue;
        int nb = (e[f.t] > e[c.g]) ? 0 : c.b;
        int nc = c.c + d[f.t] + f.c;
        if (e[f.t] >= e[c.g]) {
          if (!(nb >> nth[f.t] & 1)) {
            nb |= 1 << nth[f.t];
          } else {
            nc -= d[f.t];
          }
        }
        if (!u[c.g][f.t][nb]) {
          que.push({c.g, f.t, nc, nb});
        }
      }
    }
    cout << (que.empty() ? -1 : que.top().c) << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

struct edge {
  int t, c, rev;
};

bool u[52][52][1 << 10];

struct S {
  int g, r, c, b;
  bool operator<(S s) const { return c > s.c; };
};

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    int d[52] = {}, e[52];
    e[1] = 0;
    e[n] = 1000;
    int nalt[1234] = {};
    int nth[52] = {};
    for (int i = 2; i < n; i++) {
      cin >> d[i] >> e[i];
      nth[i] = nalt[e[i]]++;
    }
    vector<edge> G[52];
    while (m--) {
      int a, b, c;
      cin >> a >> b >> c;
      if (e[a] <= e[b]) {
        G[a].push_back({b, c, false});
      }
      if (e[b] <= e[a]) {
        G[b].push_back({a, c, true});
      }
    }
    fill(**begin(u), **end(u), false);
    priority_queue<S> que;
    que.push({1, 1, 0, 0});
    while (!que.empty()) {
      auto c = que.top();
      if (c.g == n && c.r == n)
        break;
      que.pop();
      if (u[c.g][c.r][c.b]++)
        continue;
      for (auto f : G[c.g]) {
        if (f.rev)
          continue;
        int nb = (e[f.t] > e[c.r]) ? 0 : c.b;
        int nc = c.c + d[f.t] + f.c;
        if (e[f.t] >= e[c.r]) {
          if (!(nb >> nth[f.t] & 1)) {
            nb |= 1 << nth[f.t];
          } else {
            nc -= d[f.t];
          }
        }
        que.push({f.t, c.r, nc, nb});
      }
      for (auto f : G[c.r]) {
        if (!f.rev)
          continue;
        int nb = (e[f.t] > e[c.g]) ? 0 : c.b;
        int nc = c.c + d[f.t] + f.c;
        if (e[f.t] >= e[c.g]) {
          if (!(nb >> nth[f.t] & 1)) {
            nb |= 1 << nth[f.t];
          } else {
            nc -= d[f.t];
          }
        }
        que.push({c.g, f.t, nc, nb});
      }
    }
    cout << (que.empty() ? -1 : que.top().c) << endl;
  }
}