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

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>

#define pb push_back
#define fs first
#define sc second
#define INF 500000000
using namespace std;

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

int n, m, l;
int num[110][110];
vector<int> to[110], d[110], e[110];

int main() {
  while (scanf("%d %d %d", &n, &m, &l) && (n || m || l)) {

    for (int i = 1; i <= n; i++) {
      to[i].clear();
      d[i].clear();
      e[i].clear();
    }

    for (int i = 0; i < m; i++) {
      int a, b, dis, en;
      scanf("%d %d %d %d", &a, &b, &dis, &en);
      to[a].pb(b);
      d[a].pb(dis);
      e[a].pb(en);
      to[b].pb(a);
      d[b].pb(dis);
      e[b].pb(en);
    }

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= l; j++) {
        num[i][j] = INF;
      }
    }

    num[1][l] = 0;
    priority_queue<P2, vector<P2>, greater<P2>> q;
    q.push(P2(0, P(1, l)));

    while (q.size()) {
      P2 p = q.top();
      q.pop();
      int en = p.fs, pos = p.sc.fs, mon = p.sc.sc;

      for (int i = 0; i < (int)to[pos].size(); i++) {
        int next = to[pos][i];
        if (d[pos][i] <= mon) {
          int rem = mon - d[pos][i];
          if (num[next][rem] > en) {
            num[next][rem] = en;
            q.push(P2(num[next][rem], P(next, rem)));
          }
        }
        int enemy = en + e[pos][i];
        if (num[next][mon] > enemy) {
          num[next][mon] = enemy;
          q.push(P2(enemy, P(next, mon)));
        }
      }
    }

    int ans = INF;
    for (int i = 0; i <= l; i++)
      ans = min(ans, num[n][i]);
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>

#define pb push_back
#define fs first
#define sc second
#define INF 500000000
using namespace std;

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

int main() {
  int n, m, l;
  int num[110][110];
  vector<int> to[100], d[100], e[100];

  while (scanf("%d %d %d", &n, &m, &l) && (n || m || l)) {

    for (int i = 1; i <= n; i++) {
      to[i].clear();
      d[i].clear();
      e[i].clear();
    }

    for (int i = 0; i < m; i++) {
      int a, b, dis, en;
      scanf("%d %d %d %d", &a, &b, &dis, &en);
      to[a].pb(b);
      d[a].pb(dis);
      e[a].pb(en);
      to[b].pb(a);
      d[b].pb(dis);
      e[b].pb(en);
    }

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= l; j++) {
        num[i][j] = INF;
      }
    }

    num[1][l] = 0;
    priority_queue<P2, vector<P2>, greater<P2>> q;
    q.push(P2(0, P(1, l)));

    while (q.size()) {
      P2 p = q.top();
      q.pop();
      int en = p.fs, pos = p.sc.fs, mon = p.sc.sc;

      for (int i = 0; i < (int)to[pos].size(); i++) {
        int next = to[pos][i];
        if (d[pos][i] <= mon) {
          int rem = mon - d[pos][i];
          if (num[next][rem] > en) {
            num[next][rem] = en;
            q.push(P2(num[next][rem], P(next, rem)));
          }
        }
        int enemy = en + e[pos][i];
        if (num[next][mon] > enemy) {
          num[next][mon] = enemy;
          q.push(P2(enemy, P(next, mon)));
        }
      }
    }

    int ans = INF;
    for (int i = 0; i <= l; i++)
      ans = min(ans, num[n][i]);
    printf("%d\\n", ans);
  }
}