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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll INF = (1LL << 31);

typedef pair<ll, ll> Pii;
typedef pair<ll, string> Pis;
typedef pair<Pii, Pis> PP;

ll N, M, K;
ll G[32][32];
ll g[32][32];

set<PP> u[32][32];

ll mem[32][32];

ll search(ll from, ll to) {
  if (mem[from][to] != -1)
    return mem[from][to];
  ll mini = INF, res = -1;
  for (int i = 0; i < N; i++) {
    if (from == i || g[from][i] == INF)
      continue;
    if (mini > g[from][i] + G[i][to]) {
      mini = g[from][i] + G[i][to];
      res = i;
    }
  }
  assert(res != -1);
  return mem[from][to] = res;
}

void init() {
  for (int i = 0; i < 32; i++) {
    for (int j = 0; j < 32; j++) {
      g[i][j] = (i == j ? 0 : INF);
      G[i][j] = (i == j ? 0 : INF);
      mem[i][j] = -1;
      u[i][j].clear();
    }
  }
}

int main() {
  ll cnt = 0;
  while (1) {

    init();
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    if (cnt)
      cout << endl;
    cnt++;

    for (int i = 0; i < M; i++) {
      ll a, b, c;
      cin >> a >> b >> c;
      a--, b--;
      G[a][b] = G[b][a] = c;
    }

    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        g[i][j] = G[i][j];

    for (int k = 0; k < N; k++)
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          G[i][j] = min(G[i][j], G[i][k] + G[k][j]);

    cin >> K;

    map<ll, vector<PP>> mp;
    set<ll> st;

    for (int i = 0; i < K; i++) {
      ll from, to, ti;
      string name;
      cin >> from >> to >> ti >> name;
      from--, to--;
      mp[ti].push_back(PP(Pii(from, to), Pis(0, name)));
      st.insert(ti);
    }

    vector<Pis> ans;
    vector<ll> v(N, 0);
    ll last = -1;

    st.insert(0);
    while (!st.empty()) {

      ll p = *st.begin();
      assert(last < p);
      last = p;

      if (mp.count(p)) {
        for (int i = 0; i < (int)mp[p].size(); i++) {
          PP pp = mp[p][i];
          ll from = pp.first.first;
          ll to = pp.first.second;
          string name = pp.second.second;
          ll key = search(from, to);
          PP tmp = PP(Pii(p, key), Pis(to, name));
          u[from][key].insert(tmp);
        }
        //       mp[p].clear();
      }

      for (int i = 0; i < N; i++) {
        if (v[i] > p)
          continue;

        PP target = PP(Pii(INF, 0), Pis(0, ""));

        for (int j = 0; j < N; j++) {
          if (u[i][j].empty())
            continue;
          PP pp = *u[i][j].begin();
          target = min(target, pp);
        }
        if (target.first.first == INF)
          continue;

        ll nex = target.first.second;

        set<PP>::iterator it;
        for (it = u[i][nex].begin(); it != u[i][nex].end(); it++) {
          PP pp = *it;
          ll to = pp.second.first;
          string name = pp.second.second;

          if (to == nex) {
            ans.push_back(Pis(p + g[i][nex], name));
          } else {
            mp[p + g[i][nex]].push_back(PP(Pii(nex, to), Pis(0, name)));
          }
        } // iterator

        u[i][nex].clear();
        v[i] = p + g[i][nex] * 2;
        st.insert(p + g[i][nex]);
        st.insert(p + g[i][nex] * 2);
      } // i

      st.erase(p);
    } // while ( st.empty() )

    sort(ans.begin(), ans.end());
    for (int i = 0; i < (int)ans.size(); i++) {
      Pis p = ans[i];
      cout << p.second << ' ' << p.first << endl;
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF (1 << 26)

typedef pair<int, int> Pii;
typedef pair<int, string> Pis;
typedef pair<Pii, Pis> PP;

int N, M, K;
int G[32][32];
int g[32][32];
int mem[32][32];

set<PP> t[32];
set<PP> u[32][32];

int search(int from, int to) {
  int mini = INF, res = -1;
  if (mem[from][to] != -1)
    return mem[from][to];

  for (int i = 0; i < N; i++) {
    if (from != i && mini > g[from][i] + G[i][to]) {
      mini = g[from][i] + G[i][to];
      res = i;
    }
  }

  mem[from][to] = res;
  return res;
}

void init() {
  for (int i = 0; i < 32; i++) {
    assert(t[i].empty());
    t[i].clear();
    for (int j = 0; j < 32; j++) {
      g[i][j] = (i == j ? 0 : INF);
      G[i][j] = (i == j ? 0 : INF);
      assert(u[i][j].empty());
      u[i][j].clear();
      mem[i][j] = -1;
    }
  }
}

int main() {
  int cnt = 0;
  while (1) {

    init();
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    if (cnt)
      cout << endl;
    cnt++;

    for (int i = 0; i < M; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--, b--;
      G[a][b] = min(G[a][b], c);
      G[b][a] = min(G[b][a], c);
    }

    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        g[i][j] = G[i][j];

    for (int k = 0; k < N; k++)
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          G[i][j] = min(G[i][j], G[i][k] + G[k][j]);

    cin >> K;

    map<int, vector<PP>> mp;
    set<int> st;

    for (int i = 0; i < K; i++) {
      int from, to, ti;
      string name;
      cin >> from >> to >> ti >> name;
      from--, to--;
      mp[ti].push_back(PP(Pii(from, to), Pis(0, name)));
      st.insert(ti);
    }

    vector<Pis> ans;
    vector<int> v(N, 0);

    st.insert(0);
    while (!st.empty()) {
      int p = *st.begin();
      if (mp.count(p)) {
        for (int i = 0; i < (int)mp[p].size(); i++) {
          PP pp = mp[p][i];
          int from = pp.first.first;
          int to = pp.first.second;
          string name = pp.second.second;
          int key = search(from, to);

          PP tmp = PP(Pii(p, key), Pis(to, name));
          t[from].insert(tmp);
          u[from][key].insert(tmp);
        }
      }

      for (int i = 0; i < N; i++) {
        if (v[i] > p)
          continue;
        if (t[i].empty())
          continue;

        PP target = *t[i].begin();

        int nex = target.first.second;
        set<PP>::iterator it;
        for (it = u[i][nex].begin(); it != u[i][nex].end(); it++) {
          PP pp = *it;
          int to = pp.second.first;
          string name = pp.second.second;

          assert(t[i].count(pp));
          t[i].erase(pp);

          if (to == nex) {
            ans.push_back(Pis(p + g[i][nex], name));
            continue;
          }

          mp[p + g[i][nex]].push_back(PP(Pii(nex, to), Pis(0, name)));

        } // iterator

        u[i][nex].clear();
        v[i] = p + g[i][nex] * 2;
        st.insert(v[i]);
      } // i

      st.erase(st.begin());
    } // st.empty()

    sort(ans.begin(), ans.end());
    for (int i = 0; i < (int)ans.size(); i++) {
      Pis p = ans[i];
      cout << p.second << ' ' << p.first << endl;
    }
  }

  return 0;
}