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

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int c, n, m, s, d;

int field[101][101];
int visited[11][101];

int main() {
  while (cin >> c >> n >> m >> s >> d, c || n || m || s || d) {
    memset(field, 0x3, sizeof(field));
    memset(visited, 0x3, sizeof(visited));
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      field[a][b] = field[b][a] = f / 10;
    }
    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                   greater<pair<int, pair<int, int>>>>
        q;
    q.push(make_pair(0, make_pair(c, s)));
    while (!q.empty()) {
      pair<int, pair<int, int>> current = q.top();
      q.pop();
      if (current.second.second == d) {
        cout << current.first * 10 << endl;
        break;
      }
      for (int i = 1; i <= n; i++) {
        if (current.first + field[current.second.second][i] <
            visited[current.second.first][i]) {
          int next = current.first + field[current.second.second][i];
          q.push(make_pair(next, make_pair(current.second.first, i)));
          visited[current.second.first][i] = next;
        }
        if (current.second.first > 0 &&
            current.first + field[current.second.second][i] / 2 <
                visited[current.second.first - 1][i]) {
          int next = current.first + field[current.second.second][i] / 2;
          q.push(make_pair(next, make_pair(current.second.first - 1, i)));
          visited[current.second.first - 1][i] = next;
        }
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int c, n, m, s, d;

int field[101][101];
int visited[11][101];

int main() {
  while (cin >> c >> n >> m >> s >> d, c || n || m || s || d) {
    memset(field, 0, sizeof(field));
    memset(visited, 0x3, sizeof(visited));
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      field[a][b] = field[b][a] = f / 10;
    }
    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                   greater<pair<int, pair<int, int>>>>
        q;
    q.push(make_pair(0, make_pair(c, s)));
    while (!q.empty()) {
      pair<int, pair<int, int>> current = q.top();
      q.pop();
      if (current.second.second == d) {
        cout << current.first * 10 << endl;
        break;
      }
      visited[current.second.first][current.second.second] = current.first;
      for (int i = 1; i <= n; i++) {
        if (field[current.second.second][i] > 0) {
          if (current.first + field[current.second.second][i] <
              visited[current.second.first][i]) {
            q.push(make_pair(current.first + field[current.second.second][i],
                             make_pair(current.second.first, i)));
          }
          if (current.second.first > 0 &&
              current.first + field[current.second.second][i] / 2 <
                  visited[current.second.first - 1][i]) {
            q.push(
                make_pair(current.first + field[current.second.second][i] / 2,
                          make_pair(current.second.first - 1, i)));
          }
        }
      }
    }
  }
  return 0;
}