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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

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

const int MAX_SIZE = 10000;
const int MAX_PRIME = 1000000;

int prime[MAX_PRIME + 1];
bool isPrime[MAX_PRIME + 1];
int p;

void erats() {
  // primeツδ環スツトツづーツ渉可甘コツ可サ
  fill(isPrime, isPrime + MAX_PRIME, true);
  p = 0;
  isPrime[0] = isPrime[1] = false;

  for (int i = 2; i <= MAX_PRIME; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
    } else
      continue;
    // ツづ督づゥツつ「ツづ可つゥツつッツづゥ
    for (int j = 2 * i; j <= MAX_PRIME; j += i) {
      isPrime[j] = false;
    }
  }
}

int main() {

  int N, P;
  erats();
  while (cin >> N >> P && !(N == -1 && P == -1)) {
    int *it = upper_bound(prime, prime + p, N);
    int prv = it - prime;
    int init = prv;
    vector<int> v;
    for (int i = init; i <= init + 100; i++) {
      for (int j = i; j <= init + 100; j++) {
        v.push_back(prime[i] + prime[j]);
      }
    }
    sort(v.begin(), v.end());
    cout << v[P - 1] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

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

const int MAX_SIZE = 10000;
const int MAX_PRIME = 1000000;

int prime[MAX_PRIME + 1];
bool isPrime[MAX_PRIME + 1];
int p;

void erats() {
  // primeツδ環スツトツづーツ渉可甘コツ可サ
  fill(isPrime, isPrime + MAX_PRIME, true);
  p = 0;
  isPrime[0] = isPrime[1] = false;

  for (int i = 2; i <= MAX_PRIME; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
    } else
      continue;
    // ツづ督づゥツつ「ツづ可つゥツつッツづゥ
    for (int j = 2 * i; j <= MAX_PRIME; j += i) {
      isPrime[j] = false;
    }
  }
}

int main() {

  int N, P;
  erats();
  while (cin >> N >> P && !(N == -1 && P == -1)) {
    int *it = upper_bound(prime, prime + p, N);
    int prv = it - prime;
    int init = prv;
    vector<int> v;
    for (int i = init; i <= init + 1000; i++) {
      for (int j = i; j <= init + 1000; j++) {
        v.push_back(prime[i] + prime[j]);
      }
    }
    sort(v.begin(), v.end());
    cout << v[P - 1] << endl;
  }

  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

bool f[1000002];

int main() {
  for (int i = 3; i < 1000001; i += 2) {
    f[i] = true;
    for (int j = 3; j * j <= i; j += 2)
      if (i % j == 0)
        f[i] = false;
  }
  f[2] = true;
  int n, p;
  while (1) {
    cin >> n >> p;
    if (n == -1 && n == p)
      break;

    int r = 0;
    int q[10001] = {};
    int x = 0;
    int y = n + 1;
    while (1) {
      if (f[y] == true)
        x++;
      if (x == p)
        break;
      y++;
    }
    n++;
    vector<int> anslist;
    for (int i = n; i <= y; i++)
      for (int j = i; j <= y; j++) {
        if (f[i] == true && f[j] == true) {
          anslist.push_back(i + j);

          // cout<<i<<" "<<j<<endl;
        }
      }
    sort(anslist.begin(), anslist.end());
    /*
                    for(int i=n;i<=y;i++)
                            for(int j=i;j<=y;j++)
                            {
                                    if(f[i]==true&&f[j]==true){
                                    q[r]=i+j;r++;
                                    }
                            }

                    /*
                    sort(q,q+r);
                    for(int i=0;i<r;i++)
                    cout<<q[i]<<endl;

                    cout<<q[p-1]<<endl;*/
    cout << anslist[p - 1] << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

bool f[1000002];

int main() {
  for (int i = 3; i < 1000001; i += 2) {
    f[i] = true;
    for (int j = 3; j * j <= i; j += 2)
      if (i % j == 0)
        f[i] = false;
  }
  f[2] = true;
  int n, p;
  while (1) {
    cin >> n >> p;
    if (n == -1 && n == p)
      break;

    int r = 0;
    int q[10001] = {};
    int x = 0;
    int y = n;
    while (1) {
      if (f[y] == true)
        x++;
      if (x == p)
        break;
      y++;
    }
    n++;
    vector<int> anslist;
    for (int i = n; i <= y; i++)
      for (int j = i; j <= y; j++) {
        if (f[i] == true && f[j] == true) {
          anslist.push_back(i + j);

          // cout<<i<<" "<<j<<endl;
        }
      }
    sort(anslist.begin(), anslist.end());
    /*
                    for(int i=n;i<=y;i++)
                            for(int j=i;j<=y;j++)
                            {
                                    if(f[i]==true&&f[j]==true){
                                    q[r]=i+j;r++;
                                    }
                            }

                    /*
                    sort(q,q+r);
                    for(int i=0;i<r;i++)
                    cout<<q[i]<<endl;

                    cout<<q[p-1]<<endl;*/
    cout << anslist[p - 1] << endl;
  }

  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

bool is_prime(int n, int k = 2) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 1);
}

int main() {
  vector<int> prime;

  for (int i = 2; i < 150000; i++) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    cin >> n >> m;

    if (n == -1 && m == -1) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;

    for (int i = ptr; i < ptr + m; i++) {
      for (int j = i; j < ptr + m; j++) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

    cout << c[m - 1] << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

bool is_prime(int n, int k = 2) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 1);
}

int main() {
  vector<int> prime;

  for (int i = 2; i < 150000; i++) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    cin >> n >> m;

    if (n == 0 && m == 0) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;

    for (int i = ptr; i < ptr + m; i++) {
      for (int j = i; j < ptr + m; j++) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

    cout << c[m - 1] << endl;
  }

  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

bool is_prime(int n, int k = 3) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 2);
}

int main() {
  vector<int> prime = {2};

  for (int i = 3; i < 101300; i += 2) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);

    if (n == -1 && m == -1) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;

    for (int i = ptr + m - 1; i >= ptr; i--) {
      for (int j = ptr + m - 1; j >= i; j--) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

    printf("%d\\n", c[m - 1]);
  }

  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

bool is_prime(int n, int k = 3) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 2);
}

int main() {
  vector<int> prime = {2};

  for (int i = 3; i < 101300; i += 2) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);

    if (n == -1 && m == -1) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;

    for (int i = ptr; i < ptr + m / 2; i++) {
      for (int j = i; j < ptr + m / 2; j++) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

    printf("%d\\n", c[m - 1]);
  }

  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

bool is_prime(int n, int k = 3) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 2);
}

int main() {
  vector<int> prime = {2};

  for (int i = 3; i < 101300; i += 2) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);

    if (n == -1 && m == -1) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;
    int r = min(22, max(m / 2, 12)) - 1;

    for (int i = ptr + r; i >= ptr; i--) {
      for (int j = ptr + r; j >= i; j--) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

    printf("%d\\n", c[m - 1]);
  }

  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

bool is_prime(int n, int k = 3) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 2);
}

int main() {
  vector<int> prime = {2};

  for (int i = 3; i < 101300; i += 2) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);

    if (n == -1 && m == -1) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;
    int r = min(22, m / 2) - 1;

    for (int i = ptr + r; i >= ptr; i--) {
      for (int j = ptr + r; j >= i; j--) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

    printf("%d\\n", c[m - 1]);
  }

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

#include <bits/stdc++.h>
using namespace std;

void print(vector<int> V) {
  for (vector<int>::iterator p = V.begin(); p != V.end(); p++) {
    cout << *p << " ";
  }
  cout << endl;
}

int sosu(int n) {
  if (n == 2)
    return 1;
  if (n == 1 || n == 0 || n % 2 == 0)
    return 0;
  for (int i = 3; i <= sqrt(n); i += 2) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}

bool a[300000] = {};

int main() {

  for (int i = 0; i < 300000; i++) {
    if (sosu(i))
      a[i] = true;
  }

  vector<int> V;
  for (int i = 2; i < 300000; i++) {
    if (a[i])
      V.push_back(i);
  }

  while (1) {
    int N, P;
    cin >> N >> P;
    if (N == -1 && P == -1)
      break;
    // print(V);
    int p;
    for (int i = 0; i < V.size(); i++) {
      if (N < V[i]) {
        p = i;
        break;
      }
    }
    // cout << p << endl;

    vector<int> V2;
    for (int i = p; i < p + 100; i++) {
      for (int j = p; j <= i; j++) {
        V2.push_back(V[i] + V[j]);
      }
    }
    sort(V2.begin(), V2.end());
    // print(V2);
    cout << V2[P - 1] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

void print(vector<int> V) {
  for (vector<int>::iterator p = V.begin(); p != V.end(); p++) {
    cout << *p << " ";
  }
  cout << endl;
}

int sosu(int n) {
  if (n == 2)
    return 1;
  if (n == 1 || n == 0 || n % 2 == 0)
    return 0;
  for (int i = 3; i <= sqrt(n); i += 2) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}

int main() {

  while (1) {
    int N, P;
    cin >> N >> P;
    if (N == -1 && P == -1)
      break;

    vector<int> V;

    for (int i = 1; i < 10000; i++) {
      if (sosu(N + i) == 1) {
        V.push_back(N + i);
      }
    }

    // print(V);

    vector<int> V2;

    for (int i = 0; i < V.size(); i++) {
      for (int j = 0; j <= i; j++) {
        V2.push_back(V[i] + V[j]);
      }
    }
    sort(V2.begin(), V2.end());
    // print(V2);
    cout << V2[P - 1] << endl;
  }

  return 0;
}
Problem p01269 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int V, E, L;

int const INF = 1 << 28;

struct Edge {
  int src, dst, dist, ene;
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

Graph G;

struct State {
  int money, attacked, pos;
  bool operator<(const State &s) const { return attacked > s.attacked; }
};

int dijkstra(int st) {

  int dist[110][110]; // [node][money]

  fill(dist[0], dist[0] + 110 * 110, INF);
  dist[st][L] = 0;

  priority_queue<State> pq;
  pq.push((State){L, 0, st});
  while (!pq.empty()) {
    const State stt = pq.top();
    pq.pop();
    if (stt.attacked > dist[stt.pos][stt.money])
      continue;

    for (int i = 0; i < G[stt.pos].size(); i++) {
      int next = G[stt.pos][i].dst;
      int nmoney = stt.money - G[stt.pos][i].dist;

      if (nmoney < 0)
        continue;
      if (dist[next][nmoney] > stt.attacked) {
        dist[next][nmoney] = stt.attacked;
        pq.push((State){nmoney, stt.attacked, next});
      }
    }

    for (int i = 0; i < G[stt.pos].size(); i++) {
      int next = G[stt.pos][i].dst;
      int nattacked = stt.attacked + G[stt.pos][i].ene;
      if (dist[next][stt.money] > nattacked) {
        dist[next][stt.money] = nattacked;
        pq.push((State){stt.money, nattacked, next});
      }
    }
  }

  int mn = INF;
  for (int i = 0; i <= L; i++) {
    mn = min(mn, dist[V - 1][i]);
  }

  return mn;
}

int main() {

  while (cin >> V >> E >> L && (V | E | L)) {
    G.resize(V);
    for (int i = 0; i < V; i++) {
      G[i].clear();
    }
    for (int i = 0; i < E; i++) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--, b--;
      G[a].push_back((Edge){a, b, d, e});
      G[b].push_back((Edge){b, a, d, e});
    }

    cout << dijkstra(0) << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

// テ、ツクツ?ヲツ卍づ、ツソツ敕・ツュツ佚ァツ板ィ

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

#define INF 1 << 20
#define MAX_V 101
#define MAX_L 101

struct Edge {
  int to, d, e;
};

typedef pair<int, int> Pii;
typedef pair<int, Pii> P;

int V, M, L;
int d[MAX_V][MAX_L];
vector<Edge> G[MAX_V];

int dijkstra(int s, int t) {
  priority_queue<P> Q;
  fill(d[0], d[MAX_V] + MAX_L, INF);
  d[s][L] = 0;
  Q.push(P(0, Pii(-L, s)));

  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    int c = p.first;
    int l = -p.second.first;
    int v = p.second.second;

    if (v == t)
      return c;

    for (int i = 0; i < G[v].size(); i++) {
      Edge e = G[v][i];
      if (l + e.d < 0)
        continue;
      if (d[e.to][e.d + l] < d[v][l]) {
        d[e.to][e.d + l] = d[v][l];
        Q.push(P(d[e.to][l + e.d], Pii(-(e.d + l), e.to)));
      }
    }
  }

  return -1;
}

int main() {

  cin >> V >> M >> L;
  char dd;

  for (int i = 0; i < M; i++) {
    int a, b, d, e;
    cin >> a >> dd >> b >> dd >> d >> dd >> e;
    a--, b--;
    G[a].push_back((Edge){b, d, e});
    G[b].push_back((Edge){a, d, e});
  }

  cout << dijkstra(0, V - 1) << endl;
  return 0;
}
Problem p01269 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

typedef struct {
  int to;
  int D;
  int E;
} Edge;

class State {
public:
  int now;
  int money;
  int damaged;

  State(int now, int money, int damaged) {
    this->now = now;
    this->money = money;
    this->damaged = damaged;
  }
};

const int INF = 999999;

vector<Edge> G[101];

int d[101][101];

int N, M, L;

int toV(int n, int l) { return n * L + l; }

int main() {
  while (cin >> N >> M >> L, N + M + L != 0) {
    queue<State> q;
    for (int i = 0; i < 101; i++) {
      G[i].clear();
      for (int j = 0; j < 101; j++) {
        d[i][j] = INF;
      }
    }
    for (int i = 0; i < M; i++) {
      int A, B, D, E;
      cin >> A >> B >> D >> E;
      Edge e{B, D, E};
      Edge e2{A, D, E};
      G[A].push_back(e);
      G[B].push_back(e2);
    }
    q.push(State(1, L, 0));
    while (!q.empty()) {
      State s = q.front();
      q.pop();
      if (s.damaged >= d[s.now][s.money])
        continue;
      d[s.now][s.money] = s.damaged;

      for (Edge e : G[s.now]) {
        q.push(State(e.to, s.money, s.damaged + e.E));
        if (s.money - e.D >= 0) {
          q.push(State(e.to, s.money - e.D, s.damaged));
        }
      }
    }
    int m = INF;
    for (int i = 0; i <= L; i++) {
      m = min(m, d[N][i]);
    }
    cout << m << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

typedef struct {
  int to;
  int D;
  int E;
} Edge;

class State {
public:
  int now;
  int money;
  int damaged;

  State(int now, int money, int damaged) {
    this->now = now;
    this->money = money;
    this->damaged = damaged;
  }
};

const int INF = 999999;

vector<Edge> G[101];

int d[101][101];

int N, M, L;

int toV(int n, int l) { return n * L + l; }

int main() {
  while (cin >> N >> M >> L, N + M + L != 0) {
    queue<State> q;
    for (int i = 0; i < 101; i++) {
      G[i].clear();
      for (int j = 0; j < 101; j++) {
        d[i][j] = INF;
      }
    }
    for (int i = 0; i < M; i++) {
      int A, B, D, E;
      cin >> A >> B >> D >> E;
      Edge e{B, D, E};
      Edge e2{A, D, E};
      G[A].push_back(e);
      G[B].push_back(e2);
    }
    q.push(State(1, L, 0));
    while (!q.empty()) {
      State s = q.front();
      q.pop();
      if (s.damaged > d[s.now][s.money])
        continue;
      d[s.now][s.money] = s.damaged;

      for (Edge e : G[s.now]) {
        q.push(State(e.to, s.money, s.damaged + e.E));
        if (s.money - e.D >= 0) {
          q.push(State(e.to, s.money - e.D, s.damaged));
        }
      }
    }
    int m = INF;
    for (int i = 0; i <= L; i++) {
      m = min(m, d[N][i]);
    }
    cout << m << endl;
  }
  return 0;
}
Problem p01269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define inf 1 << 30
using namespace std;

int main() {
  int N, M, L, a, b, c, d;
  int dis[101][101], th[101][101];
  int dp[101][101];

  while (1) {
    cin >> N >> M >> L;
    if (N + M + L == 0)
      break;
    for (int i = 0; i < 101; i++) {
      for (int j = 0; j < 101; j++) {
        dis[i][j] = th[i][j] = dp[i][j] = inf;
      }
    }
    for (int i = 0; i < M; i++) {
      cin >> a >> b >> c >> d;
      a--;
      b--;
      dis[a][b] = c;
      dis[b][a] = c;
      th[a][b] = d;
      th[b][a] = d;
    }
    int ans = inf;
    dp[0][L] = 0;
    priority_queue<pair<int, pair<int, int>>> pq;
    pq.push(mp(0, mp(0, L)));
    while (!pq.empty()) {
      pair<int, pair<int, int>> u = pq.top();
      pq.pop();
      u.f = (-1) * u.f;
      if (dp[u.s.f][u.s.s] < u.f)
        continue;
      if (u.s.f == N - 1) {
        ans = min(ans, u.f);
        continue;
      }

      for (int i = 0; i < N; i++) {
        if (dis[u.s.f][i] == inf)
          continue;
        int c = u.s.s - dis[u.s.f][i];
        if (0 <= c) {
          if (u.f < dp[i][c]) {
            dp[i][c] = u.f;
            pq.push(mp(u.f * (-1), mp(i, c)));
          }
        }
        if (u.f + th[u.s.f][i] < dp[i][u.s.s]) {
          dp[i][u.s.s] = u.f + th[u.s.f][i];
          pq.push(mp((u.f + th[u.s.f][i]) * (-1), mp(i, u.s.s)));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define inf 1 << 30
using namespace std;

int main() {
  int N, M, L, a, b, c, d;
  int dis[101][101], th[101][101];
  int dp[101][101];

  while (1) {
    cin >> N >> M >> L;
    if (N + M + L == 0)
      break;
    for (int i = 0; i < 101; i++) {
      for (int j = 0; j < 101; j++) {
        dis[i][j] = th[i][j] = dp[i][j] = inf;
      }
    }
    for (int i = 0; i < M; i++) {
      cin >> a >> b >> c >> d;
      a--;
      b--;
      dis[a][b] = c;
      dis[b][a] = c;
      th[a][b] = d;
      th[b][a] = d;
    }
    int ans = inf;
    dp[0][L] = 0;
    priority_queue<pair<int, pair<int, int>>> pq;
    pq.push(mp(0, mp(0, L)));
    while (!pq.empty()) {
      pair<int, pair<int, int>> u = pq.top();
      pq.pop();

      if (dp[u.s.f][u.s.s] < u.f)
        continue;
      if (u.s.f == N - 1) {
        ans = min(ans, u.f);
        continue;
      }

      for (int i = 0; i < N; i++) {
        if (dis[u.s.f][i] == inf)
          continue;
        int c = u.s.s - dis[u.s.f][i];
        if (0 <= c) {
          if (u.f < dp[i][c]) {
            dp[i][c] = u.f;
            pq.push(mp(u.f, mp(i, c)));
          }
        }
        if (u.f + th[u.s.f][i] < dp[i][u.s.s]) {
          dp[i][u.s.s] = u.f + th[u.s.f][i];
          pq.push(mp(u.f + th[u.s.f][i], mp(i, u.s.s)));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}