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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
vector<tuple<int, int, int>> x[12000];
int n, m, a, b, c, d, dist[12000];
bool used[12000];
void dijkstra() {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      Q;
  for (int i = 0; i < 12000; i++)
    dist[i] = 999999999;
  Q.push(make_pair(0, 1));
  dist[1] = 0;
  while (!Q.empty()) {
    int a1 = Q.top().first, a2 = Q.top().second;
    Q.pop();
    for (int i = 0; i < x[a2].size(); i++) {
      int to = get<0>(x[a2][i]), cost = get<1>(x[a2][i]);
      if (dist[to] > a1 + cost) {
        dist[to] = a1 + cost;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
}
int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < 12000; i++) {
      x[i].clear();
      used[i] = false;
    }
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c >> d;
      x[a].push_back(make_tuple(b, c, d));
      x[b].push_back(make_tuple(a, c, d));
    }
    dijkstra();
    vector<pair<int, int>> vec2;
    for (int i = 2; i <= n; i++)
      vec2.push_back(make_pair(dist[i], i));
    sort(vec2.begin(), vec2.end());
    int sum = 0;
    used[1] = true;
    for (int i = 0; i < vec2.size(); i++) {
      int a1 = vec2[i].second;
      vector<int> vec3;
      for (int j = 0; j < x[a1].size(); j++) {
        int to = get<0>(x[a1][j]), cost = get<1>(x[a1][j]),
            cost2 = get<2>(x[a1][j]);
        if (used[to] == false)
          continue;
        if (dist[to] + cost == dist[a1])
          vec3.push_back(cost2);
      }
      sort(vec3.begin(), vec3.end());
      sum += vec3[0];
      used[a1] = true;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MOD 1000000000LL
vector<pair<long long, long long>> X[200000];
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
               greater<pair<long long, long long>>>
    Q;
long long dist[200000], N, M, A, B, C, D;
void dijkstra(int V, int E, int s) {
  // E???????????° s?????????????????°??? X?????°???????????£??\\?????????
  for (int i = 0; i < V; i++) {
    dist[i] = 1LL << 60;
  }
  dist[s] = 0;
  Q.push(make_pair(0, s));
  int cnt = 0;
  while (!Q.empty()) {
    if (cnt > E) {
      return;
    } // NEGATIVE_CYCLE
    pair<int, int> p = Q.top();
    Q.pop();
    for (int i = 0; i < X[p.second].size(); i++) {
      int to = X[p.second][i].first;
      int leng = X[p.second][i].second;
      if (dist[to] > dist[p.second] + leng) {
        dist[to] = dist[p.second] + leng;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
}
int main() {
  while (true) {
    cin >> N >> M;
    if (N == 0)
      break;
    for (int i = 0; i < 200000; i++)
      X[i].clear();
    for (int i = 0; i < M; i++) {
      cin >> A >> B >> C >> D;
      X[A].push_back(make_pair(B, C * MOD + D));
      X[B].push_back(make_pair(A, C * MOD + D));
    }
    dijkstra(N, M, 1);
    long long sum = 0;
    for (int i = 1; i <= N; i++)
      sum += dist[i] % MOD;
    cout << sum << endl;
  }
}