Problem p01269 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};

const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int INF = 500000000;

struct Edge {
  int from, to, cost, danger;
  Edge(int from, int to, int cost, int danger)
      : from(from), to(to), cost(cost), danger(danger) {}
};

int solve(vector<vector<Edge>> graph, int L) {
  int n = graph.size();
  vector<vector<int>> dp(n, vector<int>(L + 1, INF));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < graph[j].size(); k++) {
        const Edge &edge = graph[j][k];
        for (int l = 0; l <= L; l++) {
          if (dp[j][l] >= INF)
            continue;
          if (l + edge.cost <= L) {
            int new_cost = l + edge.cost;
            int to = edge.to;
            int here = dp[j][l];
            dp[to][new_cost] = min(dp[to][new_cost], dp[j][l]);
          }
          dp[edge.to][l] = min(dp[edge.to][l], dp[j][l] + edge.danger);
        }
      }
    }
  }
  int ret = INF;
  for (int i = 0; i <= L; i++) {
    ret = min(ret, dp.back()[i]);
  }
  return ret;
}

int main() {
  while (true) {
    int n, m, l;
    cin >> n >> m >> l;
    if (n == 0)
      break;
    vector<vector<Edge>> edges(n);
    for (int i = 0; i < m; i++) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--;
      b--;
      edges[a].push_back(Edge(a, b, d, e));
      edges[b].push_back(Edge(b, a, d, e));
    }
    cout << solve(edges, l) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};

const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int INF = 500000000;

struct Edge {
  int from, to, cost, danger;
  Edge(int from, int to, int cost, int danger)
      : from(from), to(to), cost(cost), danger(danger) {}
};

int solve(vector<vector<Edge>> graph, int L) {
  int n = graph.size();
  vector<vector<int>> dp(n, vector<int>(L + 1, INF));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < graph[i].size(); k++) {
        const Edge &edge = graph[j][k];
        for (int l = 0; l <= L; l++) {
          if (dp[j][l] >= INF)
            continue;
          if (l + edge.cost <= L) {
            int new_cost = l + edge.cost;
            int to = edge.to;
            int here = dp[j][l];
            dp[to][new_cost] = min(dp[to][new_cost], dp[j][l]);
          }
          dp[edge.to][l] = min(dp[edge.to][l], dp[j][l] + edge.danger);
        }
      }
    }
  }
  int ret = INF;
  for (int i = 0; i <= L; i++) {
    ret = min(ret, dp.back()[i]);
  }
  return ret;
}

int main() {
  while (true) {
    int n, m, l;
    cin >> n >> m >> l;
    if (n == 0)
      break;
    vector<vector<Edge>> edges(n);
    for (int i = 0; i < m; i++) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--;
      b--;
      edges[a].push_back(Edge(a, b, d, e));
      edges[b].push_back(Edge(b, a, d, e));
    }
    cout << solve(edges, l) << endl;
  }
  return 0;
}