Problem p00200 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int MAX_INT = 10000000;
int n, m;
int p, q, r;

int costMap[101][101];
int timeMap[101][101];
int visited[101];

class Node {
public:
  int cost, pos;
  Node(int p) {
    cost = 0;
    pos = p;
  }
  Node(int p, int c, const Node &pre) {
    cost = c + pre.cost;
    pos = p;
  }
  bool operator<(const Node &o) const { return cost > o.cost; }
};

int memo[101][101][2];

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i <= m; i++) {
      fill(costMap[i], costMap[i] + m + 1, MAX_INT);
      fill(timeMap[i], timeMap[i] + m + 1, MAX_INT);
    }
    for (int i = 0; i < n; i++) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;
      costMap[a][b] = costMap[b][a] = cost;
      timeMap[a][b] = timeMap[b][a] = time;
    }
    int k;
    for (int i = 0; i <= m; i++) {
      for (int j = 0; j <= m; j++) {
        memo[i][j][0] = 0x7fffffff;
        memo[i][j][1] = 0x7fffffff;
      }
    }
    for (cin >> k; k; k--) {
      cin >> p >> q >> r;
      if (memo[p][q][r] != 0x7fffffff) {
        cout << memo[p][q][r] << endl;
        continue;
      }
      fill(visited, visited + m + 1, MAX_INT);
      priority_queue<Node> que;
      que.push(Node(p));
      visited[p] = 0;
      while (!que.empty()) {
        Node current = que.top();
        que.pop();
        if (current.pos == q) {
          cout << current.cost << endl;
          memo[p][q][r] = memo[q][p][r] = current.cost;
          break;
        }
        for (int i = 1; i <= m; i++) {
          if (r) {
            if (timeMap[current.pos][i] != MAX_INT &&
                timeMap[current.pos][i] + current.cost < visited[i]) {
              visited[i] = timeMap[current.pos][i] + current.cost;
              que.push(Node(i, timeMap[current.pos][i], current));
              // cout << current.pos << " to " << i << endl;
            }
          } else {
            if (costMap[current.pos][i] != MAX_INT &&
                costMap[current.pos][i] + current.cost < visited[i]) {
              visited[i] = costMap[current.pos][i] + current.cost;
              que.push(Node(i, costMap[current.pos][i], current));
              // cout << current.pos << " to " << i << endl;
            }
          }
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int MAX_INT = 10000;
int n, m;
int p, q, r;

int costMap[101][101];
int timeMap[101][101];
int visited[101];

class Node {
public:
  int cost, pos;
  Node(int p) {
    cost = 0;
    pos = p;
  }
  Node(int p, int c, const Node &pre) {
    cost = c + pre.cost;
    pos = p;
  }
  bool operator<(const Node &o) const { return cost > o.cost; }
};

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i <= m; i++) {
      fill(costMap[i], costMap[i] + m + 1, MAX_INT);
      fill(timeMap[i], timeMap[i] + m + 1, MAX_INT);
    }
    for (int i = 0; i < n; i++) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;
      costMap[a][b] = costMap[b][a] = cost;
      timeMap[a][b] = timeMap[b][a] = time;
    }
    int k;
    for (cin >> k; k; k--) {
      cin >> p >> q >> r;
      fill(visited, visited + m + 1, MAX_INT);
      priority_queue<Node> que;
      que.push(Node(p));
      visited[p] = 0;
      while (!que.empty()) {
        Node current = que.top();
        que.pop();
        if (current.pos == q) {
          cout << current.pos << ", " << current.cost << endl;
          break;
        }
        for (int i = 1; i <= m; i++) {
          if (r) {
            if (timeMap[current.pos][i] != MAX_INT &&
                timeMap[current.pos][i] + current.cost < visited[i]) {
              visited[i] = timeMap[current.pos][i] + current.cost;
              que.push(Node(i, timeMap[current.pos][i], current));
              cout << current.pos << " to " << i << endl;
            }
          } else {
            if (costMap[current.pos][i] != MAX_INT &&
                costMap[current.pos][i] + current.cost < visited[i]) {
              visited[i] = costMap[current.pos][i] + current.cost;
              que.push(Node(i, costMap[current.pos][i], current));
              cout << current.pos << " to " << i << endl;
            }
          }
        }
      }
    }
  }
  return 0;
}