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

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

class edge {
public:
  int to, cost;
  edge();
  edge(int _to, int _cost) {
    to = _to;
    cost = _cost;
  }
};
class state {
public:
  int id, cost, ticket;
  state();
  state(int _id, int _cost, int _ticket) {
    id = _id;
    cost = _cost;
    ticket = _ticket;
  }
  bool operator<(const state st) const { return cost > st.cost; }
};
const int INF = 1000000000;

int main() {
  int c, n, m, s, g;
  while (cin >> c >> n >> m >> s >> g) {
    if (!c && !n && !m && !s && !g)
      break;
    vector<edge> t[101];
    int d[101][11];
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      a--;
      b--;
      t[a].push_back(edge(b, cost));
      t[b].push_back(edge(a, cost));
    }
    s--;
    g--;

    int ans = INF;
    for (int j = 0; j < 101; j++) {
      for (int i = 0; i < 11; i++) {
        d[j][i] = INF;
      }
    }
    d[s][c] = 0;
    priority_queue<state> que;
    que.push(state(s, 0, c));
    while (!que.empty()) {
      state st = que.top();
      que.pop();
      int v = st.id;
      if (st.id == g)
        ans = min(ans, st.cost);
      for (unsigned i = 0; i < t[v].size(); i++) {
        edge e = t[v][i];
        if (d[e.to][st.ticket] > d[v][st.ticket] + e.cost) {
          d[e.to][st.ticket] = d[v][st.ticket] + e.cost;
          que.push(state(e.to, d[e.to][st.ticket], st.ticket));
        }
        if (st.ticket > 0) {
          if (d[e.to][st.ticket - 1] > d[v][st.ticket] + (e.cost) / 2) {
            d[e.to][st.ticket - 1] = d[v][st.ticket] + (e.cost) / 2;
            que.push(state(e.to, d[e.to][st.ticket - 1], st.ticket - 1));
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

class edge {
public:
  int to, cost;
  edge();
  edge(int _to, int _cost) {
    to = _to;
    cost = _cost;
  }
};

class state {
public:
  int id, cost, ticket;
  state();
  state(int _id, int _cost, int _ticket) {
    id = _id;
    cost = _cost;
    ticket = _ticket;
  }
  bool operator<(const state st) const { return cost > st.cost; }
};
const int INF = 1000000;

int main() {
  int c, n, m, s, g;
  while (cin >> c >> n >> m >> s >> g) {
    if (!c && !n && !m && !s && !g)
      break;
    vector<edge> t[100];
    int d[100][11];
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      a--;
      b--;
      t[a].push_back(edge(b, cost));
      t[b].push_back(edge(a, cost));
    }
    s--;
    g--;

    int ans = INF;
    for (int i = 0; i < 11; i++) {
      fill(d[i], d[i] + 100, INF);
    }
    d[s][c] = 0;
    priority_queue<state> que;
    que.push(state(s, 0, c));
    while (!que.empty()) {
      state st = que.top();
      que.pop();
      int v = st.id;
      if (d[v][st.ticket] < st.cost)
        continue;
      if (st.id == g)
        ans = min(ans, st.cost);
      for (unsigned i = 0; i < t[v].size(); i++) {
        edge e = t[v][i];

        if (d[e.to][st.ticket] > d[v][st.ticket] + e.cost) {
          d[e.to][st.ticket] = d[v][st.ticket] + e.cost;
          que.push(state(e.to, d[e.to][st.ticket], st.ticket));
        }
        if (st.ticket > 0) {
          d[e.to][st.ticket - 1] = d[v][st.ticket] + (e.cost) / 2;
          que.push(state(e.to, d[e.to][st.ticket - 1], st.ticket - 1));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}