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

#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define N 101
#define INF 200000000
using namespace std;

int dist[N];
typedef pair<int, int> pii;

std::vector<pii> e1[N];
std::vector<pii> e2[N];

int main(void) {
  int a, b, c, d;
  int n, m;
  while (cin >> n >> m, n || m) {
    int dp[m + 1][m + 1];
    int dp1[m + 1][m + 1];
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= m; j++) {
        if (i == j)
          dp[i][j] = 0;
        else
          dp[i][j] = INF;
      }
    }
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= m; j++) {
        if (i == j)
          dp1[i][j] = 0;
        else
          dp1[i][j] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> d;
      // e1[a].push_back(make_pair(c,b));
      // e2[a].push_back(make_pair(d,b));
      dp[b][a] = c;
      dp[a][b] = c;
      dp1[b][a] = d;
      dp1[a][b] = d;
    }

    for (int k = 1; k <= m; k++) {
      for (int u = 1; u <= m; u++) {
        for (int v = 1; v <= m; v++) {
          dp[u][v] = min(dp[u][v], dp[u][k] + dp[k][v]);
        }
      }
    }

    for (int k = 1; k <= m; k++) {
      for (int u = 1; u <= m; u++) {
        for (int v = 1; v <= m; v++) {
          dp1[u][v] = min(dp1[u][v], dp1[u][k] + dp1[k][v]);
        }
      }
    }

    int k;
    cin >> k;
    while (k--) {
      cin >> a >> b >> c;
      if (c)
        cout << dp1[a][b] << endl;
      else
        cout << dp[a][b] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define N 101
#define INF 2000000000
using namespace std;

int dist[N];
typedef pair<int, int> pii;

std::vector<pii> e1[N];
std::vector<pii> e2[N];

int dfs(int now, int c) {
  if (dist[now] >= c)
    dist[now] = c;
  else
    return 0;
  for (int i = 0; i < e1[now].size(); i++) {
    dfs(e1[now][i].second, c + e1[now][i].first);
  }
  return 0;
}

int dijk1(int a, int b) {
  dist[a] = 0;
  std::priority_queue<pii> pq;
  for (int i = 0; i < e2[a].size(); i++) {
    pq.push(e2[a][i]);
  }
  while (!pq.empty()) {
    pii temp = pq.top();
    int now = temp.second;
    int c = temp.first;
    pq.pop();
    if (dist[now] != INF)
      continue;
    dist[now] = c;
    for (int i = 0; i < e2[now].size(); i++) {
      pq.push(make_pair(c + e2[now][i].first, e2[now][i].second));
    }
  }
  return -dist[b];
}

int dijk(int a, int b) {
  dist[a] = 0;
  std::priority_queue<pii> pq;
  for (int i = 0; i < e1[a].size(); i++) {
    pq.push(e1[a][i]);
  }
  while (!pq.empty()) {
    pii temp = pq.top();
    int now = temp.second;
    int c = temp.first;
    pq.pop();
    if (dist[now] != INF)
      continue;
    dist[now] = c;
    for (int i = 0; i < e1[now].size(); i++) {
      pq.push(make_pair(c + e1[now][i].first, e1[now][i].second));
    }
  }
  return -dist[b];
}

int main(void) {
  int a, b, c, d;
  int n, m;
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> d;
      e1[a].push_back(make_pair(-c, b));
      e2[a].push_back(make_pair(-d, b));
    }
    int k;
    cin >> k;
    while (k--) {
      for (int i = 1; i <= m; i++)
        dist[i] = INF;
      cin >> a >> b >> c;
      if (c)
        cout << dijk1(a, b) << endl;
      else
        cout << dijk(a, b) << endl;
      // for(int i=1;i<=m;i++)printf("dist[%d]:%d\\n",i,dist[i]);
    }
  }
  return 0;
}