alexjercan/bugnet huggingface dataset:
Problem p00200 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define mod 1000000007
#define lol(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int main() {
  int n, m;
  while (cin >> m >> n && n + m) {
    priority_queue<P, vector<P>, greater<P>> Q;
    vector<PP> v[110];
    int dis[110];
    lol(i, m) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      v[a].push_back(make_pair(b, make_pair(c, d)));
      v[b].push_back(make_pair(a, make_pair(c, d)));
    }
    cin >> m;
    lol(u, m) {
      lol(i, n) dis[i + 1] = mod;
      int s, t, c;
      cin >> s >> t >> c;
      Q.push(make_pair(0, s));
      while (!Q.empty()) {
        int b = Q.top().first, a = Q.top().second;
        Q.pop();
        if (dis[a] < mod)
          continue;
        dis[a] = b;
        lol(i, v[a].size()) {
          int to = v[a][i].first,
              co = b + (c ? v[a][i].second.second : v[a][i].second.first);
          if (dis[to] == mod)
            Q.push(make_pair(co, to));
        }
      }
      cout << dis[t] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define mod 1000000007
#define lol(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int main() {
  int n, m;
  while (cin >> m >> n && n + m) {
    priority_queue<P, vector<P>, greater<P>> Q;
    vector<PP> v[110];
    int dis[110];
    lol(i, m) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      v[a].push_back(make_pair(b, make_pair(c, d)));
      v[b].push_back(make_pair(a, make_pair(c, d)));
    }
    cin >> m;
    lol(u, m) {
      lol(i, n) dis[i + 1] = mod;
      int s, t, c;
      cin >> s >> t >> c;
      Q.push(make_pair(s, 0));
      while (!Q.empty()) {
        int a = Q.top().first, b = Q.top().second;
        Q.pop();
        if (dis[a] < mod)
          continue;
        dis[a] = b;
        lol(i, v[a].size()) {
          int to = v[a][i].first,
              co = b + (c ? v[a][i].second.second : v[a][i].second.first);
          Q.push(make_pair(to, co));
        }
      }
      cout << dis[t] << endl;
    }
  }
  return 0;
}
Problem p00200 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  int a, b, time, cost, querys;
  int i, j, k, p, q, r, n, m;
  int times[100][100], costs[100][100];

  while (true) {
    cin >> n >> m;
    if (!n && !m)
      break;

    for (i = 0; i < m; i++) {
      for (j = 0; j < m; j++) {
        times[i][j] = (i == j) ? 0 : 1e6;
        costs[j][i] = (i == j) ? 0 : 1e6;
      }
    }

    for (i = 0; i < n; i++) {
      cin >> a >> b >> cost >> time;
      times[a - 1][b - 1] = time;
      times[b - 1][a - 1] = time;
      costs[a - 1][b - 1] = cost;
      costs[b - 1][a - 1] = cost;
    }

    for (k = 0; k < m; k++) {
      for (i = 0; i < m; i++) {
        for (j = 0; j < m; j++) {
          times[i][j] = min(times[i][j], times[i][k] + times[k][j]);
          costs[i][j] = min(costs[i][j], costs[i][k] + costs[k][j]);
        }
      }
    }

    cin >> querys;
    for (i = 0; i < querys; i++) {
      cin >> p >> q >> r;
      cout << (r ? times[p - 1][q - 1] : costs[p - 1][q - 1]) << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int a, b, time, cost, querys;
  int i, j, k, l, p, q, r, n, m;
  int times[100][100], costs[100][100], temp[100][100];

  while (true) {
    cin >> n >> m;
    if (!n && !m)
      break;

    for (i = 0; i < m; i++) {
      for (j = 0; j < m; j++) {
        times[i][j] = (i == j) ? 0 : 1e9;
        costs[i][j] = (i == j) ? 0 : 1e9;
      }
    }

    for (i = 0; i < n; i++) {
      cin >> a >> b >> cost >> time;
      times[a - 1][b - 1] = time;
      costs[a - 1][b - 1] = cost;
    }

    cin >> querys;
    for (l = 0; l < querys; l++) {
      cin >> p >> q >> r;

      for (i = 0; i < m; i++)
        for (j = 0; j < m; j++)
          temp[i][j] = r ? times[i][j] : costs[i][j];

      for (k = 0; k < m; k++)
        for (i = 0; i < m; i++)
          for (j = 0; j < m; j++)
            temp[i][j] = min(temp[i][j], temp[i][k] + temp[k][j]);

      cout << temp[p - 1][q - 1] << endl;
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;
const double PI = acos(-1.0);
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using edge = struct {
  int to;
  int cost;
};

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int costs[100][100];
int times[100][100];

void Warshall_Floyd(int N, int (&cost)[100][100]) {
  rep(k, N) rep(i, N) rep(j, N) cost[i][j] =
      min(cost[i][j], cost[i][k] + cost[k][j]);
}

int main() {
  int N, M;
  while (cin >> M >> N && N) {
    rep(i, 100) rep(j, 100) costs[i][j] = iINF;
    rep(i, 100) rep(j, 100) times[i][j] = iINF;

    rep(i, M) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      a--;
      b--;

      costs[a][b] = min(costs[a][b], c);
      costs[b][a] = min(costs[b][a], c);

      times[a][b] = min(times[a][b], t);
      times[b][a] = min(times[b][a], t);
    }

    Warshall_Floyd(N, costs);
    Warshall_Floyd(N, times);

    int K;
    cin >> K;

    int p, q, r;

    rep(i, K) {
      cin >> p >> q >> r;
      p--;
      q--;

      cout << (r == 1 ? times[p][q] : costs[p][q]) << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;
const double PI = acos(-1.0);
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using edge = struct {
  int to;
  int cost;
};

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int costs[100][100];
int times[100][100];

void Warshall_Floyd(int N, int (&cost)[100][100]) {
  rep(k, N) rep(i, N) rep(j, N) cost[i][j] =
      min(cost[i][j], cost[i][k] + cost[k][j]);
}

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    rep(i, 100) rep(j, 100) costs[i][j] = iINF;
    rep(i, 100) rep(j, 100) times[i][j] = iINF;

    rep(i, N) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      a--;
      b--;

      costs[a][b] = min(costs[a][b], c);
      costs[b][a] = min(costs[b][a], c);

      times[a][b] = min(times[a][b], t);
      times[b][a] = min(times[b][a], t);
    }

    Warshall_Floyd(N, costs);
    Warshall_Floyd(N, times);

    int K;
    cin >> K;

    int p, q, r;

    rep(i, K) {
      cin >> p >> q >> r;
      p--;
      q--;

      cout << (r == 1 ? times[p][q] : costs[p][q]) << endl;
    }
  }

  return 0;
}
Problem p00200 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

#define inf 1000000000

int n, m;
int C[101][101];
int T[101][101];

class S {
public:
  int iti, now;
  S() {}
  S(int a, int b) {
    iti = a;
    now = b;
  }
  bool operator<(const S &a) const { return now > a.now; }
};
int dike(int st, int en, int va) {
  priority_queue<S> state;
  int sima[101];
  int visit[101] = {0};
  for (int i = 0; i < 101; i++) {
    sima[i] = inf;
    visit[i] = 0;
  }
  state.push(S(st, 0));
  sima[st] = 0;
  while (1) {
    S u;
    u = state.top();
    state.pop();
    visit[u.iti] = 1;
    if (u.iti == en) {
      return u.now;
    }

    for (int i = 1; i <= m; i++) {
      if (C[u.iti][i] == inf || visit[i] == 1) {
        continue;
      }
      int cost = C[u.iti][i];
      if (va == 1) {
        cost = T[u.iti][i];
      }
      if (sima[u.iti] + cost < sima[i]) {
        sima[i] = sima[u.iti] + cost;
        state.push(S(i, sima[i]));
      }
    }
  }
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    for (int i = 0; i < 101; i++) {
      for (int j = 0; j < 101; j++) {
        C[i][j] = inf;
        T[i][j] = inf;
      }
    }
    for (int i = 0; i < n; i++) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      C[a][b] = C[b][a] = c;
      T[a][b] = T[b][a] = d;
    }

    int l;
    cin >> l;
    for (int i = 0; i < l; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      printf("%d\\n", dike(a, b, c));
    }
  }
}

Error: MLE

Fixed submission:

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

using namespace std;

#define inf 1000000000

int n, m;
int C[101][101];
int T[101][101];

class S {
public:
  int iti, now;
  S() {}
  S(int a, int b) {
    iti = a;
    now = b;
  }
  bool operator<(const S &a) const { return now > a.now; }
};
int dike(int st, int en, int va) {
  priority_queue<S> state;
  int sima[101];
  for (int i = 0; i < 101; i++) {
    sima[i] = inf;
  }
  state.push(S(st, 0));
  sima[st] = 0;
  while (1) {
    S u;
    u = state.top();
    state.pop();

    if (u.iti == en) {
      return u.now;
    }

    for (int i = 1; i <= m; i++) {

      if (C[u.iti][i] == inf) {

        continue;
      }

      if (u.now + C[u.iti][i] < sima[i]) {
        int cost = C[u.iti][i];
        if (va == 1) {
          cost = T[u.iti][i];
        }
        sima[u.iti] = u.now + cost;
        state.push(S(i, sima[u.iti]));
      }
    }
  }
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    for (int i = 0; i < 101; i++) {
      for (int j = 0; j < 101; j++) {
        C[i][j] = inf;
        T[i][j] = inf;
      }
    }
    for (int i = 0; i < n; i++) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      C[a][b] = C[b][a] = c;
      T[a][b] = T[b][a] = d;
    }

    int l;
    cin >> l;
    for (int i = 0; i < l; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      printf("%d\\n", dike(a, b, c));
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define sintttt(i, j, k, m) scanf("%d%d%d%d", &i, &j, &k, &m);

#define INF 1010000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  while (1) {
    int n, m;
    int time[101][101];
    int cost[101][101];

    rep(i, 101) rep(j, 101) time[i][j] = cost[i][j] = INF;

    sintt(n, m);
    if (n == 0)
      break;
    rep(i, n) {
      int a, b, c, d;
      sintttt(a, b, c, d);

      time[a][b] = d;
      time[b][a] = d;

      cost[a][b] = c;
      cost[b][a] = c;
    }

    reps(k, m) {
      reps(i, m) {
        reps(j, m) {
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
          time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
        }
      }
    }

    int q;
    sint(q);
    rep(i, q) {
      int a, b, c;
      sinttt(a, b, c);
      if (c == 1) {
        printf("%d\\n", time[a][b]);
      } else {
        printf("%d\\n", cost[a][b]);
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define sintttt(i, j, k, m) scanf("%d%d%d%d", &i, &j, &k, &m);

#define INF 1010000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  while (1) {
    int n, m;
    int time[101][101];
    int cost[101][101];

    rep(i, 101) rep(j, 101) time[i][j] = cost[i][j] = INF;

    sintt(n, m);
    if (n == 0)
      break;
    rep(i, n) {
      int a, b, c, d;
      sintttt(a, b, c, d);

      time[a][b] = d;
      time[b][a] = d;

      cost[a][b] = c;
      cost[b][a] = c;
    }

    reps(k, n) {
      reps(i, n) {
        reps(j, n) {
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
          time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
        }
      }
    }

    int q;
    sint(q);
    rep(i, q) {
      int a, b, c;
      sinttt(a, b, c);
      if (c == 1) {
        printf("%d\\n", time[a][b]);
      } else {
        printf("%d\\n", cost[a][b]);
      }
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)

int main() {
  while (1) {
    int n, m;

    cin >> n >> m;
    if (n == 0)
      break;

    int froid[111][111][2];
    rep(i, 111) rep(j, 111) rep(k, 2) froid[i][j][k] = 1010000000;

    rep(i, n) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;

      froid[a][b][0] = c;
      froid[b][a][0] = c;
      froid[a][b][1] = d;
      froid[b][a][1] = d;
    }

    rep(p, 2) {
      reps(k, m) {
        reps(i, m) {
          reps(j, m) {
            froid[i][j][p] =
                min(froid[i][j][p], froid[i][k][p] + froid[k][j][p]);
          }
        }
      }
    }

    int k;
    cin >> k;

    rep(i, k) {
      int a, b, c;
      cin >> a >> b >> c;

      printf("%d\\n", froid[a][b][c]);
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)

int main() {
  while (1) {
    int n, m;
    vector<pair<int, int>> a[111][2];

    cin >> n >> m;
    if (n == 0)
      break;

    int froid[111][111][2];
    rep(i, 111) rep(j, 111) rep(k, 2) froid[i][j][k] = 1010000000;

    rep(i, n) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;

      froid[a][b][0] = c;
      froid[b][a][0] = c;
      froid[a][b][1] = d;
      froid[b][a][1] = d;
    }

    rep(p, 2) {
      reps(k, n) {
        reps(i, n) {
          reps(j, n) {
            froid[i][j][p] =
                min(froid[i][j][p], froid[i][k][p] + froid[k][j][p]);
          }
        }
      }
    }

    int k;
    cin >> k;

    rep(i, k) {
      int a, b, c;
      cin >> a >> b >> c;

      printf("%d\\n", froid[a][b][c]);
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 211;
int dpcost[N][N];
int dptime[N][N];

int main() {

A:;
  int n, m;
  cin >> n >> m;
  if (n == 0)
    return 0;

  const int INF = 1000000000;

  rep(i, N) rep(j, N) dpcost[i][j] = dptime[i][j] = INF;
  rep(i, N) dpcost[i][i] = dptime[i][i] = 0;

  rep(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    dpcost[a][b] = c;
    dpcost[b][a] = c;
    dptime[a][b] = d;
    dptime[b][a] = d;
  }

  reps(k, 1, m + 1) reps(i, 1, m + 1) reps(j, 1, m + 1) {
    dpcost[i][j] = min(dpcost[i][j], dpcost[i][k] + dpcost[k][j]);
    dptime[i][j] = min(dptime[i][j], dptime[i][k] + dptime[k][j]);
  }

  int r;
  cin >> r;

  rep(i, r) {
    int a, b, c;
    cin >> a >> b >> c;
    if (c == 0)
      printf("%d\\n", dpcost[a][b]);
    if (c == 1)
      printf("%d\\n", dptime[a][b]);
  }

  goto A;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 211;
int dpcost[N][N];
int dptime[N][N];

int main() {

A:;
  int n, m;
  cin >> n >> m;
  if (n == 0)
    return 0;

  const int INF = 1000000000;

  rep(i, N) rep(j, N) dpcost[i][j] = dptime[i][j] = INF;
  rep(i, N) dpcost[i][i] = dptime[i][i] = 0;

  rep(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    dpcost[a][b] = c;
    dpcost[b][a] = c;
    dptime[a][b] = d;
    dptime[b][a] = d;
  }

  reps(k, 1, n + 1) reps(i, 1, n + 1) reps(j, 1, n + 1) {
    dpcost[i][j] = min(dpcost[i][j], dpcost[i][k] + dpcost[k][j]);
    dptime[i][j] = min(dptime[i][j], dptime[i][k] + dptime[k][j]);
  }

  int r;
  cin >> r;

  rep(i, r) {
    int a, b, c;
    cin >> a >> b >> c;
    if (c == 0)
      printf("%d\\n", dpcost[a][b]);
    if (c == 1)
      printf("%d\\n", dptime[a][b]);
  }

  goto A;
}
Problem p00200 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstdio>
using namespace std;
int main() {
  int time[101][101], cost[101][101], n, m, i, j, k, a, b, c, d;
  while (scanf("%d%d", &n, &m), m) {
    for (i = 0; i < 101; ++i) {
      for (j = 0; j < 101; ++j) {
        time[i][j] = cost[i][j] = INT_MAX / 2;
      }
    }
    for (i = 0; i < n; ++i) {
      scanf("%d%d%d%d", &a, &b, &c, &d), cost[a][b] = cost[b][a] = c,
                                         time[a][b] = time[b][a] = d;
    }
    for (k = 1; k <= m; ++k) {
      for (i = 1; i <= m; ++i) {
        for (j = 1; j <= m; ++j) {
          time[i][j] = min(time[i][j], time[i][k] + time[k][j]),
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
        }
      }
    }
    scanf("%d", &k);
    for (i = 0; i < k; ++i) {
      scanf("%d%d%d", &a, &b, &c);
      printf("%d\\n", c ? time[a][b] : cost[a][b]);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

void open(int max[101], int name[101][101][2], int m, int basic, int r, int to,
          vector<int> got[101]) {
  unsigned int i;
  for (i = 0; i < got[basic].size(); i++) {
    if (max[got[basic][i]] == -1 ||
        max[got[basic][i]] > max[basic] + name[basic][got[basic][i]][r]) {
      max[got[basic][i]] = max[basic] + name[basic][got[basic][i]][r];
      if (got[basic][i] != to)
        open(max, name, m, got[basic][i], r, to, got);
    }
  }
}

int main(void) {
  while (1) {
    int n, m;
    int a, b, cost, time;
    int k;
    int p, q, r;
    int mintime;
    int mincost;
    int i, j, x, y;

    int name[101][101][2] = {}; // OÍ¿à
    int max[101] = {};
    vector<int> got[101];

    cin >> n >> m;

    if (n == 0 && m == 0)
      return 0;

    for (i = 0; i < n; i++) {
      cin >> a >> b >> cost >> time;
      name[a][b][0] = cost;
      name[b][a][0] = cost;
      name[a][b][1] = time;
      name[b][a][1] = time;
      got[a].push_back(b);
      got[b].push_back(a);
    }

    cin >> k;

    for (i = 0; i < k; i++) {
      cin >> p >> q >> r;

      fill(max, max + 101, -1);

      max[p] = 0;

      open(max, name, m, p, r, q, got);

      cout << max[q] << endl;
    }
  }
  return 0;
}
Problem p00200 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> P;

struct station {
  station(int to, int cost, int time) {
    this->to = to;
    this->cost = cost;
    this->time = time;
  }

  int to;
  int cost;
  int time;
};

void dijkstra(int from, int mode);

vector<station> route[1000];
int mini[1000];
int n, m;

int main() {
  while (scanf("%d %d", &n, &m) * n != 0) {
    for (int i = 0; i < m; i++) {
      route[i].clear();
    }
    for (int i = 0; i < n; i++) {
      int a, b;
      int cost, time;

      scanf("%d %d %d %d", &a, &b, &cost, &time);
      a--, b--;
      route[a].push_back(station(b, cost, time));
      route[b].push_back(station(a, cost, time));
    }

    int k;
    scanf("%d", &k);
    while (k-- > 0) {
      int p, q, r;
      scanf("%d %d %d", &p, &q, &r);
      p--, q--;
      mini[p] = 0;
      dijkstra(p, r);
      printf("%d\\n", mini[q]);
    }
  }

  return 0;
}

void dijkstra(int from, int mode) {
  priority_queue<P, vector<P>, greater<P>> que;

  fill(mini, mini + m, 1000000000);
  mini[from] = 0;
  que.push(P(0, from));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;

    if (mini[v] < p.first) {
      continue;
    }
    for (int i = 0; i < route[v].size(); i++) {
      station s = route[v][i];
      if (mode == 0 && mini[s.to] > mini[v] + s.cost) {
        mini[s.to] = mini[v] + s.cost;
        que.push(P(mini[s.to], s.to));
      } else if (mode == 1 && mini[s.to] > mini[v] + s.time) {
        mini[s.to] = mini[v] + s.time;
        que.push(P(mini[s.to], s.to));
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

void dijkstra(int from, int mode);

pair<int, int> route[1000][1000];
int mini[1000];
int n, m;
int k;

int main() {
  while (scanf("%d %d", &n, &m) * n != 0) {
    for (int i = 0; i < n; i++) {
      int a, b;
      int cost, time;

      scanf("%d %d %d %d", &a, &b, &cost, &time);
      a--, b--;
      route[a][b] = make_pair(cost, time);
      route[b][a] = make_pair(cost, time);
    }

    scanf("%d", &k);
    while (k-- > 0) {
      fill(mini, mini + m, 100000000);
      int p, q, r;
      scanf("%d %d %d", &p, &q, &r);
      p--, q--;
      mini[p] = 0;
      dijkstra(p, r);
      printf("%d\\n", mini[q]);
    }
  }

  return 0;
}

void dijkstra(int from, int mode) {
  queue<int> to;

  for (int i = 0; i < m; i++) {
    if (route[from][i].first + route[from][i].second == 0) {
      continue;
    }
    if (mode == 0 && mini[i] > mini[from] + route[from][i].first) {
      mini[i] = mini[from] + route[from][i].first;
      to.push(i);
    } else if (mode == 1 && mini[i] > mini[from] + route[from][i].second) {
      mini[i] = mini[from] + route[from][i].second;
      to.push(i);
    }
    if (k == 0) {
      route[from][i] = make_pair(0, 0);
    }
  }
  while (!to.empty()) {
    dijkstra(to.front(), mode);
    to.pop();
  }
}