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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct tEdge {
  short to, val;
  tEdge(short t, short v) {
    to = t;
    val = v;
  }
};

bool operator<(const tEdge &l, const tEdge &r) { return (l.val > r.val); }

int main() {
  vector<vector<short>> cost, time;
  priority_queue<tEdge> edges;
  vector<bool> checked;
  int n, m;
  while (cin >> n >> m, n || m) {
    cost.resize(m + 1);
    time.resize(m + 1);
    checked.resize(m + 1);
    for (int i = 1; i <= m; i++) {
      cost[i].resize(m + 1);
      time[i].resize(m + 1);
      for (int j = 1; j <= m; j++) {
        cost[i][j] = time[i][j] = -1;
      }
    }
    for (int i = 0; i < n; i++) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      cost[a][b] = cost[b][a] = c;
      time[a][b] = time[b][a] = t;
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      if (p == q) {
        cout << 0 << endl;
        continue;
      }
      while (!edges.empty())
        edges.pop();
      for (int j = 1; j <= m; j++) {
        if (r == 0 && cost[p][j] != -1)
          edges.push(tEdge(j, cost[p][j]));
        else if (r == 1 && time[p][j] != -1)
          edges.push(tEdge(j, time[p][j]));
        checked[j] = false;
      }
      checked[p] = true;
      while (1) {
        tEdge e(0, 0);
        do {
          e = edges.top();
          edges.pop();
        } while (checked[e.to]);
        if (e.to == q) {
          cout << e.val << endl;
          break;
        }
        checked[e.to] = true;
        for (int x = 1; x <= m; x++) {
          if (!checked[x]) {
            if (r == 0 && cost[e.to][x] != -1) {
              edges.push(tEdge(x, e.val + cost[e.to][x]));
            } else if (r == 1 && time[e.to][x] != -1) {
              edges.push(tEdge(x, e.val + time[e.to][x]));
            }
          }
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<vector<short>> cost, time;
  vector<int> station;
  int n, m;
  while (cin >> n >> m, n || m) {
    cost.resize(m + 1);
    time.resize(m + 1);
    station.resize(m + 1);
    for (int i = 1; i <= m; i++) {
      cost[i].resize(m + 1);
      time[i].resize(m + 1);
      for (int j = 1; j <= m; j++) {
        cost[i][j] = time[i][j] = -1;
      }
    }
    station.resize(m);
    for (int i = 0; i < n; i++) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      cost[a][b] = cost[b][a] = c;
      time[a][b] = time[b][a] = t;
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      for (int j = 1; j <= m; j++) {
        station[j] = -1;
      }
      station[p] = 0;
      for (int z = 0; z < m; z++) {
        int ny = -1, nextVal = 1000000;
        for (int x = 1; x <= m; x++) {
          if (station[x] != -1) {
            for (int y = 1; y <= m; y++) {
              if ((station[y] == -1) && (cost[x][y] != -1)) {
                int val = station[x];
                if (r == 0) // cost
                  val += cost[x][y];
                else if (r == 1) // time
                  val += time[x][y];
                if (val < nextVal) {
                  nextVal = val;
                  ny = y;
                }
              }
            }
          }
        }
        station[ny] = nextVal;
        if (ny == q)
          break;
      }
      cout << station[q] << endl;
    }
  }
  return 0;
}