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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};

const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int NO = 1000000000;
int main() {
  while (true) {
    // miti kazu
    int n;
    cin >> n;
    if (n eq 0)
      break;
    // miti
    vector<vector<int>> R(10, vector<int>(10, NO));
    // distance
    vector<vector<int>> D(10, vector<int>(10, NO));

    int m = -1;
    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      R[a][b] = R[b][a] = c;
      m = max(m, max(a, b));
    }

    for (int i = 0; i < 10; i++) {
      R[i][i] = 0;
      D[i][i] = 0;
    }

    m++;
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {

        // distance where
        priority_queue<pair<int, int>, vector<pair<int, int>>,
                       greater<pair<int, int>>>
            Q;
        Q.push(mp(0, i));
        vector<char> A(10, false);

        while (!Q.empty()) {
          int dis = Q.top().first;
          int where = Q.top().second;
          Q.pop();
          if (A[where])
            continue;
          A[where] = true;

          if (where eq j) {
            D[i][j] = D[j][i] = dis;
            break;
          }
          for (int k = 0; k < 10; k++) {
            if (where eq k)
              continue;
            if (R[where][k] not_eq NO) {
              Q.push(mp(R[where][k] + dis, k));
            }
          }
        }
      }
    }

    int index_i = -1;
    int sum = NO;
    for (int i = 0; i < m; i++) {
      int s = 0;
      for (int j = 0; j < m; j++) {
        s += D[i][j];
      }
      if (sum > s) {
        sum = s;
        index_i = i;
      }
    }

    cout << index_i << " " << sum << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};

const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int NO = 1000000000;
int main() {
  while (true) {
    // miti kazu
    int n;
    cin >> n;
    if (n eq 0)
      break;
    // miti
    vector<vector<int>> R(10, vector<int>(10, NO));
    // distance
    vector<vector<int>> D(10, vector<int>(10, NO));

    int m = -1;
    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      R[a][b] = R[b][a] = c;
      m = max(m, max(a, b));
    }

    for (int i = 0; i < 10; i++) {
      R[i][i] = 0;
      D[i][i] = 0;
    }

    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        Q;
    m++;
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {

        // distance where
        Q = priority_queue<pair<int, int>, vector<pair<int, int>>,
                           greater<pair<int, int>>>();
        Q.push(mp(0, i));

        while (!Q.empty()) {
          int dis = Q.top().first;
          int where = Q.top().second;
          Q.pop();

          if (where eq j) {
            D[i][j] = D[j][i] = dis;
            break;
          }
          for (int k = 0; k < 10; k++) {
            if (where eq k)
              continue;
            if (R[where][k] not_eq NO) {
              Q.push(mp(R[where][k] + dis, k));
            }
          }
        }
      }
    }

    int index_i = -1;
    int sum = NO;
    for (int i = 0; i < m; i++) {
      int s = 0;
      for (int j = 0; j < m; j++) {
        s += D[i][j];
      }
      if (sum > s) {
        sum = s;
        index_i = i;
      }
    }

    cout << index_i << " " << sum << endl;
  }
  return 0;
}