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

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct P {
  int from, cost, coupon;

  bool operator>(const P &p) const { return cost > p.cost; }
};

int ecost[101][101];
int ccost[101][11];
int c, n, m, s, d;
int i;

int main() {
  while (cin >> c >> n >> m >> s >> d, c | n | m | s | d) {
    fill_n((int *)ecost, 101 * 101, -1);
    fill_n((int *)ccost, 101 * 11, 1e8);
    for (i = 0; i < 11; i++)
      ccost[s][i] = 0;
    for (i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      ecost[a][b] = f;
      ecost[b][a] = f;
    }
    priority_queue<P, vector<P>, greater<P>> q;
    q.push((P){s, 0, 0});
    int dcost;
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      // cout << "pos=" << p.from << endl;
      // goal
      if (p.from == d) {
        dcost = p.cost;
        break;
      }
      //
      for (i = 0; i < n + 1; i++) { // i:to
        if (ecost[p.from][i] != -1) {
          bool ifpush = true;
          for (int j = 0; j <= p.coupon; j++)
            ifpush = ifpush && p.cost + ecost[p.from][i] < ccost[i][j];
          if (ifpush) {
            // if(p.cost + ecost[p.from][i] < ccost[i][p.coupon]){
            ccost[i][p.coupon] = p.cost + ecost[p.from][i];
            q.push((P){i, p.cost + ecost[p.from][i], p.coupon});
          }
          if (p.coupon + 1 <= c) {
            bool ifpush = true;
            for (int j = 0; j <= p.coupon + 1; j++)
              ifpush = ifpush && p.cost + ecost[p.from][i] / 2 < ccost[i][j];
            if (ifpush) {
              // if(p.cost + ecost[p.from][i]/2 < ccost[i][p.coupon+1]){
              ccost[i][p.coupon + 1] = p.cost + ecost[p.from][i] / 2;
              q.push((P){i, p.cost + ecost[p.from][i] / 2, p.coupon + 1});
            }
          }
        }
      }
    }
    cout << dcost << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct P {
  int from, cost, coupon;

  bool operator>(const P &p) const { return cost > p.cost; }
};

int ecost[101][101];
int ccost[101][11];
int c, n, m, s, d;
int i;

int main() {
  while (cin >> c >> n >> m >> s >> d, c | n | m | s | d) {
    fill_n((int *)ecost, 101 * 101, -1);
    fill_n((int *)ccost, 101 * 11, 1e8);
    for (i = 0; i < 11; i++)
      ccost[s][i] = 0;
    for (i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      ecost[a][b] = f;
      ecost[b][a] = f;
    }
    priority_queue<P, vector<P>, greater<P>> q;
    q.push((P){s, 0, 0});
    int dcost;
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      // cout << "pos=" << p.from << endl;
      // goal
      if (p.from == d) {
        dcost = p.cost;
        break;
      }
      //
      for (i = 0; i < n + 1; i++) { // i:to
        if (ecost[p.from][i] != -1) {
          bool ifpush = true;
          for (int j = 0; j <= p.coupon; j++)
            ifpush = ifpush && p.cost + ecost[p.from][i] < ccost[i][j];
          if (ifpush) {
            // if(p.cost + ecost[p.from][i] < ccost[i][p.coupon]){
            q.push((P){i, p.cost + ecost[p.from][i], p.coupon});
          }
          if (p.coupon + 1 <= c) {
            bool ifpush = true;
            for (int j = 0; j <= p.coupon + 1; j++)
              ifpush = ifpush && p.cost + ecost[p.from][i] / 2 < ccost[i][j];
            if (ifpush) {
              // if(p.cost + ecost[p.from][i]/2 < ccost[i][p.coupon+1]){
              q.push((P){i, p.cost + ecost[p.from][i] / 2, p.coupon + 1});
            }
          }
        }
      }
    }
    cout << dcost << endl;
  }
  return 0;
}