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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fo(i, n) for (int i = 0; i < (int)n; i++)
#define rep(it, s)                                                             \\
  for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++)
#define mp(a, b) make_pair(a, b)
#define pb(x) push_back(x)
#define pii pair<int, int>

int n, m, c;
vector<int> e[110];
vector<int> w[110];
int best[110][110];

int main() {

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  while (scanf("%d%d%d", &n, &m, &c) && n != 0) {
    for (int i = 0; i < n; i++)
      e[i].clear();
    for (int i = 0; i < n; i++)
      w[i].clear();
    for (int i = 0; i < m; i++) {
      int a, b, t;
      scanf("%d%d%d", &a, &b, &t);
      a--;
      b--;
      e[a].push_back(b);
      w[a].push_back(t);
    }
    int lo = 0;
    int hi = n - 1;
    while (lo < hi) {
      int mi = (lo + hi) >> 1;
      for (int i = 0; i < n; i++)
        for (int j = 0; j <= mi; j++)
          best[i][j] = 1000000000;
      priority_queue<pair<pair<int, int>, int>> q;
      best[0][mi] = 0;
      q.push(make_pair(make_pair(0, mi), 0));
      bool good = 0;
      while (!q.empty()) {
        int h = q.top().first.second;
        int p = q.top().second;
        int d = best[p][h];
        q.pop();
        // if (d<-q.top().first.first) continue;
        if (p == n - 1) {
          good = 1;
          break;
        }
        for (int i = 0; i < e[p].size(); i++) {
          int x = e[p][i];
          int t = w[p][i];

          if (best[x][h] > d + t && d + t <= c) {
            best[x][h] = d + t;
            q.push(make_pair(make_pair(-d - t, h), x));
          }
          if (h > 0 && best[x][h - 1] > d) {
            best[x][h - 1] = d;
            q.push(make_pair(make_pair(-d, h - 1), x));
          }
        }
      }
      if (good) {
        hi = mi;
      } else
        lo = mi + 1;
    }
    cout << lo << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fo(i, n) for (int i = 0; i < (int)n; i++)
#define rep(it, s)                                                             \\
  for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++)
#define mp(a, b) make_pair(a, b)
#define pb(x) push_back(x)
#define pii pair<int, int>

int n, m, c;
vector<int> e[110];
vector<int> w[110];
int best[110][100010];
bool vis[110][100010];
set<pair<pair<int, int>, int>> q;

int main() {

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  while (scanf("%d%d%d", &n, &m, &c) && n != 0) {
    for (int i = 0; i < n; i++)
      e[i].clear();
    for (int i = 0; i < n; i++)
      w[i].clear();
    for (int i = 0; i < m; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      a--;
      b--;
      e[a].push_back(b);
      w[a].push_back(c);
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= c; j++)
        best[i][j] = n + 10;
    // priority_queue<pair<pair<int,int>, int> > q;
    q.clear();
    // q.push(make_pair(make_pair(0,0),0));
    q.insert(make_pair(make_pair(0, 0), 0));
    best[0][0] = 0;
    while (!q.empty()) {
      // int d = -q.top().first.second;
      // int p = q.top().second;

      // q.pop();

      int d = (*q.begin()).first.second;
      int p = (*q.begin()).second;

      q.erase(q.begin());

      if (d > c)
        continue;
      int h = best[p][d];
      if (p == n - 1 && d <= c) {
        cout << h << endl;
        break;
      }
      vis[p][d] = 0;

      for (int i = 0; i < e[p].size(); i++) {
        int x = e[p][i];
        if (d + w[p][i] < c && best[x][d + w[p][i]] > h) {
          // if (!vis[x][d+w[p][i]])
          // q.push(make_pair(make_pair(-h,-d-w[p][i]),x));
          if (vis[x][d + w[p][i]])
            q.erase(make_pair(make_pair(best[x][d + w[p][i]], d + w[p][i]), x));
          q.insert(make_pair(make_pair(h, d + w[p][i]), x));
          best[x][d + w[p][i]] = h;
          vis[x][d + w[p][i]] = 1;
        }
        if (best[x][d] > h + 1) {
          // if (!vis[x][d]) q.push(make_pair(make_pair(-h-1,-d),x));
          if (vis[x][d])
            q.erase(make_pair(make_pair(best[x][d], d), x));
          q.insert(make_pair(make_pair(h + 1, d), x));
          best[x][d] = h + 1;
          vis[x][d] = 1;
        }
      }
    }
  }

  return 0;
}