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

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

class Edge {
public:
  int src, dst, cst;
  Edge(int src, int dst, int cst) : src(src), dst(dst), cst(cst) {}
};

class State {
public:
  int g, b, c, v;
  State(int g, int b, int c, int v) : g(g), b(b), c(c), v(v) {}

  bool operator<(const State &s) const { return c > s.c; }
};

typedef vector<vector<Edge>> Graph;

bool vis[50][50][1 << 10], same[50];
int d[50], E[50], id[50], N, M;

int solve(Graph &graph, Graph &revgraph) {
  memset(vis, 0, sizeof(vis));
  priority_queue<State> q;
  q.push(State(0, 0, 0, 0));

  while (!q.empty()) {
    State s = q.top();
    q.pop();
    if (s.g == N - 1 && s.b == N - 1)
      return s.c;
    if (vis[s.g][s.b][s.v])
      continue;
    vis[s.g][s.b][s.v] = 1;

    if (E[s.g] <= E[s.b]) {
      for (int i = 0; i < graph[s.g].size(); i++) {
        Edge &e = graph[s.g][i];
        if (E[e.dst] < E[s.g])
          continue;

        int nv = s.v;
        int nc = s.c + e.cst;
        int h = max(E[s.g], max(E[s.b], E[e.dst]));
        if (max(E[s.g], E[s.b]) < E[e.dst])
          nv = 0;

        if (same[e.dst] && h == E[e.dst]) {
          if (!((nv >> id[e.dst]) & 1))
            nc += d[e.dst];
          nv |= (1 << id[e.dst]);

          if (vis[e.dst][s.b][nv])
            continue;
          q.push(State(e.dst, s.b, nc, nv));
        } else {
          if (e.dst != s.b)
            nc += d[e.dst];
          if (vis[e.dst][s.b][nv])
            continue;

          q.push(State(e.dst, s.b, nc, nv));
        }
      }
    }

    if (E[s.b] <= E[s.g]) {
      for (int i = 0; i < revgraph[s.b].size(); i++) {
        Edge &e = revgraph[s.b][i];
        if (E[e.dst] < E[s.b])
          continue;

        int nv = s.v;
        int nc = s.c + e.cst;
        int h = max(E[s.g], max(E[s.b], E[e.dst]));
        if (max(E[s.g], E[s.b]) < E[e.dst])
          nv = 0;

        if (same[e.dst] && h == E[e.dst]) {
          if (!((nv >> id[e.dst]) & 1))
            nc += d[e.dst];
          nv |= (1 << id[e.dst]);

          if (vis[s.g][e.dst][nv])
            continue;
          q.push(State(s.g, e.dst, nc, nv));
        } else {
          if (e.dst != s.g)
            nc += d[e.dst];
          if (vis[s.g][e.dst][nv])
            continue;

          q.push(State(s.g, e.dst, nc, nv));
        }
      }
    }
  }

  return -1;
}

int main() {
  while (cin >> N >> M, N || M) {
    E[0] = 0;
    E[N - 1] = 1000;
    d[0] = d[N - 1] = 0;

    for (int i = 1; i < N - 1; i++)
      cin >> d[i] >> E[i];

    Graph g(N), rg(N);
    for (int i = 0; i < M; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;

      g[a].push_back(Edge(a, b, c));
      rg[b].push_back(Edge(b, a, c));
    }

    int cnt = 0;
    memset(same, 0, sizeof(same));
    for (int i = 0; i < N - 1; i++) {
      if (E[i] == E[i + 1]) {
        if (!same[i])
          cnt = 0;

        same[i] = same[i + 1] = true;
        id[i] = cnt;
        id[i + 1] = ++cnt;
      }
    }

    cout << solve(g, rg) << endl;
  }
}

Error: MLE

Fixed submission:

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

class Edge {
public:
  int src, dst, cst;
  Edge(int src, int dst, int cst) : src(src), dst(dst), cst(cst) {}
};

class State {
public:
  int g, b, c, v;
  State(int g, int b, int c, int v) : g(g), b(b), c(c), v(v) {}

  bool operator<(const State &s) const { return c > s.c; }
};

typedef vector<vector<Edge>> Graph;

bool vis[50][50][1 << 10], same[50];
int d[50], E[50], id[50], N, M;

int solve(Graph &graph, Graph &revgraph) {
  memset(vis, 0, sizeof(vis));
  priority_queue<State> q;
  q.push(State(0, 0, 0, 0));

  while (!q.empty()) {
    State s = q.top();
    q.pop();
    if (s.g == N - 1 && s.b == N - 1)
      return s.c;
    if (vis[s.g][s.b][s.v])
      continue;
    vis[s.g][s.b][s.v] = 1;

    for (int i = 0; i < graph[s.g].size(); i++) {
      Edge &e = graph[s.g][i];
      if (E[e.dst] < E[s.g])
        continue;

      int nv = s.v;
      int nc = s.c + e.cst;
      int h = max(E[s.g], max(E[s.b], E[e.dst]));
      if (max(E[s.g], E[s.b]) < E[e.dst])
        nv = 0;

      if (same[e.dst] && h == E[e.dst]) {
        if (!((nv >> id[e.dst]) & 1))
          nc += d[e.dst];
        nv |= (1 << id[e.dst]);

        if (vis[e.dst][s.b][nv])
          continue;
        q.push(State(e.dst, s.b, nc, nv));
      } else {
        if (e.dst != s.b)
          nc += d[e.dst];
        if (vis[e.dst][s.b][nv])
          continue;

        q.push(State(e.dst, s.b, nc, nv));
      }
    }

    for (int i = 0; i < revgraph[s.b].size(); i++) {
      Edge &e = revgraph[s.b][i];
      if (E[e.dst] < E[s.b])
        continue;

      int nv = s.v;
      int nc = s.c + e.cst;
      int h = max(E[s.g], max(E[s.b], E[e.dst]));
      if (max(E[s.g], E[s.b]) < E[e.dst])
        nv = 0;

      if (same[e.dst] && h == E[e.dst]) {
        if (!((nv >> id[e.dst]) & 1))
          nc += d[e.dst];
        nv |= (1 << id[e.dst]);

        if (vis[s.g][e.dst][nv])
          continue;
        q.push(State(s.g, e.dst, nc, nv));
      } else {
        if (e.dst != s.g)
          nc += d[e.dst];
        if (vis[s.g][e.dst][nv])
          continue;

        q.push(State(s.g, e.dst, nc, nv));
      }
    }
  }

  return -1;
}

int main() {
  while (cin >> N >> M, N || M) {
    E[0] = 0;
    E[N - 1] = 1000;
    d[0] = d[N - 1] = 0;

    for (int i = 1; i < N - 1; i++)
      cin >> d[i] >> E[i];

    Graph g(N), rg(N);
    for (int i = 0; i < M; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;

      g[a].push_back(Edge(a, b, c));
      rg[b].push_back(Edge(b, a, c));
    }

    int cnt = 0;
    memset(same, 0, sizeof(same));
    for (int i = 0; i < N - 1; i++) {
      if (E[i] == E[i + 1]) {
        if (!same[i])
          cnt = 0;

        same[i] = same[i + 1] = true;
        id[i] = cnt;
        id[i + 1] = ++cnt;
      }
    }

    cout << solve(g, rg) << endl;
  }
}