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

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

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int dpt[111][111];
int dpc[111][111];
int n, m, a, b, cost, t, k, p, q, r;

int main() {
  while (cin >> n >> m && n + m) {
    REP(i, 111) {
      REP(j, 111) {
        dpt[i][j] = INF;
        dpc[i][j] = INF;
      }
    }
    REP(i, n) {
      cin >> a >> b >> cost >> t;
      dpt[a][b] = min(dpt[a][b], t);
      dpt[b][a] = min(dpt[a][b], t);
      dpc[a][b] = min(dpc[a][b], cost);
      dpc[b][a] = min(dpc[a][b], cost);
    }
    FOR(l, 1, m + 1) {
      FOR(i, 1, m + 1) {
        FOR(j, 1, m + 1) {
          dpt[i][j] = min(dpt[i][j], dpt[i][l] + dpt[l][j]);
          dpc[i][j] = min(dpc[i][j], dpc[i][l] + dpc[l][j]);
        }
      }
    }
    cin >> k;
    REP(i, k) {
      cin >> p >> q >> r;
      if (r == 0)
        cout << dpc[p][q] << endl;
      else
        cout << dpt[p][q] << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

struct edge {
  ll to, cost, time;
  edge(ll _to, ll _cost, ll _time) : to(_to), cost(_cost), time(_time){};
};

ll n, m, a, b, COST, TIME, k, s, g, f;

ll mincost[1000], mintime[1000];
vector<edge> G[1000];

void dijkstra0(ll start) {
  fill(mincost, mincost + 1000, INF);
  mincost[start] = 0;
  priority_queue<P> que;
  que.push(P(0, start));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (mincost[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (mincost[e.to] > mincost[v] + e.cost) {
        mincost[e.to] = mincost[v] + e.cost;
        que.push(P(mincost[e.to], e.to));
      }
    }
  }
}

void dijkstra1(ll start) {
  fill(mintime, mintime + 1000, INF);
  mintime[start] = 0;
  priority_queue<P> que;
  que.push(P(0, start));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (mincost[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (mintime[e.to] > mintime[v] + e.time) {
        mintime[e.to] = mintime[v] + e.time;
        que.push(P(mintime[e.to], e.to));
      }
    }
  }
}

int main() {
  while (cin >> n >> m && n + m) {
    REP(i, n) {
      cin >> a >> b >> COST >> TIME;
      G[a].push_back(edge(b, COST, TIME));
      G[b].push_back(edge(a, COST, TIME));
    }
    cin >> k;
    REP(i, k) {
      cin >> s >> g >> f;
      if (f == 0) {
        dijkstra0(s);
        cout << mincost[g] << endl;
      } else {
        dijkstra1(s);
        cout << mintime[g] << endl;
      }
    }
  }
}