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

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

using namespace std;

int main() {
  int N, M, L;
  int a, b;
  int now;
  cin >> N >> M >> L;
  for (; N != 0;) {
    vector<vector<bool>> graph(N, vector<bool>(N, false));
    vector<vector<int>> distance(N, vector<int>(N)), evect(N, vector<int>(N));
    // vector<vector<int>> dp(N,vector<int>(L+1,1000000000));
    vector<vector<int>> dp_old(N, vector<int>(L + 1, 1000000000));
    queue<int> vertex;
    for (int i = 0; i < M; i++) {
      cin >> a >> b;
      a--;
      b--;
      graph[a][b] = true;
      graph[b][a] = true;
      cin >> distance[a][b] >> evect[a][b];
      distance[b][a] = distance[a][b];
      evect[b][a] = evect[a][b];
    }
    for (int i = 0; i <= L; i++) {
      dp_old[0][i] = 0;
      // dp[0][i] = 0;
    }
    for (int itr = 0; itr < N / 2 + 1; itr++) {
      vector<bool> reached(N, false);
      reached[0] = true;
      if (itr == 0) {
        for (int i = 0; i < N; i++) {
          if (graph[0][i]) {
            vertex.push(i);
            // graph[0][i] = false;
            // graph[i][0] = false;
            dp_old[i][L] = evect[0][i];
            // dp[i][L] = evect[0][i];
            if (L >= distance[0][i]) {
              dp_old[i][L - distance[0][i]] = 0;
              // dp[i][L-distance[0][i]] = 0;
            }
          }
        }
      } else {
        for (int i = 0; i < N; i++) {
          if (graph[0][i]) {
            vertex.push(i);
          }
        }
      }
      for (; !vertex.empty();) {
        now = vertex.front();
        reached[now] = true;
        vertex.pop();
        for (int i = 1; i < N; i++) {
          if (graph[now][i]) {
            if (!reached[i])
              vertex.push(i);
            for (int j = 0; j <= L; j++) {
              if (j + distance[now][i] <= L) {
                dp_old[now][j] =
                    min(dp_old[now][j], min(dp_old[i][j] + evect[now][i],
                                            dp_old[i][j + distance[now][i]]));
                // dp_old[i][j] =
                // min(dp_old[i][j],min(dp_old[now][j]+evect[now][i],dp_old[now][j+distance[now][i]]));
                // dp_old[now][j] = dp[now][j];
                // dp_old[i][j] = dp[i][j];
              } else {
                dp_old[now][j] =
                    min(dp_old[i][j] + evect[now][i], dp_old[now][j]);
                // dp_old[i][j] =
                // min(dp_old[now][j]+evect[now][i],dp_old[i][j]);
                // dp_old[now][j] = dp[now][j];
                // dp_old[i][j] = dp[i][j];
              }
            }
          }
        }
        // swap(dp,dp_old);
      }
    }
    // swap(dp,dp_old);
    int ans = dp_old[N - 1][L];
    for (int i = 0; i < L; i++) {
      ans = min(ans, dp_old[N - 1][i]);
    }
    cout << ans << endl;

    cin >> N >> M >> L;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int N, M, L;
  int a, b;
  int now;
  cin >> N >> M >> L;
  for (; N != 0;) {
    vector<vector<bool>> graph(N, vector<bool>(N, false));
    vector<vector<int>> distance(N, vector<int>(N)), evect(N, vector<int>(N));
    // vector<vector<int>> dp(N,vector<int>(L+1,1000000000));
    vector<vector<int>> dp_old(N, vector<int>(L + 1, 1000000000));
    queue<int> vertex;
    for (int i = 0; i < M; i++) {
      cin >> a >> b;
      a--;
      b--;
      graph[a][b] = true;
      graph[b][a] = true;
      cin >> distance[a][b] >> evect[a][b];
      distance[b][a] = distance[a][b];
      evect[b][a] = evect[a][b];
    }
    for (int i = 0; i <= L; i++) {
      dp_old[0][i] = 0;
      // dp[0][i] = 0;
    }
    for (int itr = 0; itr < N; itr++) {
      vector<bool> reached(N, false);
      reached[0] = true;
      if (itr == 0) {
        for (int i = 0; i < N; i++) {
          if (graph[0][i]) {
            vertex.push(i);
            // graph[0][i] = false;
            // graph[i][0] = false;
            dp_old[i][L] = evect[0][i];
            // dp[i][L] = evect[0][i];
            if (L >= distance[0][i]) {
              dp_old[i][L - distance[0][i]] = 0;
              // dp[i][L-distance[0][i]] = 0;
            }
          }
        }
      } else {
        for (int i = 0; i < N; i++) {
          if (graph[0][i]) {
            vertex.push(i);
          }
        }
      }
      for (; !vertex.empty();) {
        now = vertex.front();
        reached[now] = true;
        vertex.pop();
        for (int i = 1; i < N; i++) {
          if (graph[now][i]) {
            if (!reached[i])
              vertex.push(i);
            for (int j = 0; j <= L; j++) {
              if (j + distance[now][i] <= L) {
                dp_old[now][j] =
                    min(dp_old[now][j], min(dp_old[i][j] + evect[now][i],
                                            dp_old[i][j + distance[now][i]]));
                // dp_old[i][j] =
                // min(dp_old[i][j],min(dp_old[now][j]+evect[now][i],dp_old[now][j+distance[now][i]]));
                // dp_old[now][j] = dp[now][j];
                // dp_old[i][j] = dp[i][j];
              } else {
                dp_old[now][j] =
                    min(dp_old[i][j] + evect[now][i], dp_old[now][j]);
                // dp_old[i][j] =
                // min(dp_old[now][j]+evect[now][i],dp_old[i][j]);
                // dp_old[now][j] = dp[now][j];
                // dp_old[i][j] = dp[i][j];
              }
            }
          }
        }
        // swap(dp,dp_old);
      }
    }
    // swap(dp,dp_old);
    int ans = dp_old[N - 1][L];
    for (int i = 0; i < L; i++) {
      ans = min(ans, dp_old[N - 1][i]);
    }
    cout << ans << endl;

    cin >> N >> M >> L;
  }

  return 0;
}