Problem p00200 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
typedef pair<int, int> P;
int D[440];
int n, m, x, y, cost, Time;
vector<P> matrix1[440], matrix2[440];

void djkstra1(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < m + 2; i++)
    D[i] = 10000000;
  D[s] = 0;
  que.push(P(s, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.first;
    if (D[v] < p.second)
      continue;
    for (int i = 0; i < matrix1[v].size(); i++) {
      P pp = matrix1[v][i];
      if (D[pp.first] > D[v] + pp.second) {
        D[pp.first] = D[v] + pp.second;
        que.push(P(pp.first, D[pp.first]));
      }
    }
  }
}

void djkstra2(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < m + 2; i++)
    D[i] = 10000000;
  D[s] = 0;
  que.push(P(s, 0));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.first;
    if (D[v] < p.second)
      continue;
    for (int i = 0; i < matrix2[v].size(); i++) {
      P pp = matrix2[v][i];
      if (D[pp.first] > D[v] + pp.second) {
        D[pp.first] = D[v] + pp.second;
        que.push(P(pp.first, D[pp.first]));
      }
    }
  }
}

int main() {
  int p, q, r;
  while (true) {
    cin >> n >> m;
    if (n + m == 0)
      break;

    for (int i = 0; i <= m; i++) {
      matrix1[i].clear();
      matrix2[i].clear();
    }
    for (int i = 0; i < n; i++) {
      cin >> x >> y >> cost >> Time;
      matrix1[x].push_back(P(y, cost));
      matrix2[x].push_back(P(y, Time));
      matrix1[y].push_back(P(x, cost));
      matrix2[y].push_back(P(x, Time));
    }

    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> p >> q >> r;
      if (r == 0) {
        djkstra1(p);
        cout << D[q] << endl;
      } else {
        djkstra2(p);
        cout << D[q] << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int D[440];
int n, m, x, y, cost, Time, p, q, r;
vector<P> matrix1[440], matrix2[440];
void djkstra1(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < n + 2; i++)
    D[i] = 10000000;
  D[s] = 0;
  que.push(P(s, 0));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.first;
    if (D[v] < p.second)
      continue;
    for (int i = 0; i < matrix1[v].size(); i++) {
      P pp = matrix1[v][i];
      if (D[pp.first] > D[v] + pp.second) {
        D[pp.first] = D[v] + pp.second;
        que.push(P(pp.first, D[pp.first]));
      }
    }
  }
}

void djkstra2(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < n + 2; i++)
    D[i] = 10000000;
  D[s] = 0;
  que.push(P(s, 0));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.first;
    if (D[v] < p.second)
      continue;
    for (int i = 0; i < matrix2[v].size(); i++) {
      P pp = matrix2[v][i];
      if (D[pp.first] > D[v] + pp.second) {
        D[pp.first] = D[v] + pp.second;
        que.push(P(pp.first, D[pp.first]));
      }
    }
  }
}

int main() {

  while (true) {
    cin >> n >> m;
    if (n + m == 0)
      break;

    for (int i = 0; i < n + 2; i++) {
      matrix1[i].clear();
      matrix2[i].clear();
    }
    for (int i = 0; i < n; i++) {
      cin >> x >> y >> cost >> Time;
      matrix1[x].push_back(P(y, cost));
      matrix2[x].push_back(P(y, Time));
      matrix1[y].push_back(P(x, cost));
      matrix2[y].push_back(P(x, Time));
    }

    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> p >> q >> r;
      if (r == 0) {
        djkstra1(p);
        cout << D[q] << endl;
      } else {
        djkstra2(p);
        cout << D[q] << endl;
      }
    }
  }
  return 0;
}