Problem p01269 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define INF (1 << 9)

struct P {
  int from, money, damage;

  bool operator>(const P &p) const { return damage > p.damage; }
};

struct edge {
  int from, cost, damage;
};

int main() {
  int town_n, road_n, money;
  while (cin >> town_n >> road_n >> money, town_n | road_n | money) {
    vector<vector<edge>> E(town_n + 1);
    REP(i, road_n) {
      int to, from, cost, damage;
      cin >> to >> from >> cost >> damage;
      E[to].push_back({from, cost, damage});
      E[from].push_back({to, cost, damage});
    }

    bool G[town_n + 1][money + 1];
    fill_n((int *)G, (town_n + 1) * (money + 1), false);

    priority_queue<P, vector<P>, greater<P>> que;
    que.push({1, money, 0});
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      if (p.from == town_n) {
        cout << p.damage << endl;
        break;
      }

      if (!G[p.from][p.money]) {
        G[p.from][p.money] = true;
        for (edge e : E[p.from]) {
          if (p.money >= e.cost) {
            que.push({e.from, p.money - e.cost, p.damage});
          }
          que.push({e.from, p.money, p.damage + e.damage});
        }
      }
    }
  }
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define INF (1 << 9)

struct P {
  int from, money, damage;

  bool operator>(const P &p) const { return damage > p.damage; }
};

struct edge {
  int from, cost, damage;
};

int main() {
  int town_n, road_n, money;
  while (cin >> town_n >> road_n >> money, town_n | road_n | money) {
    vector<vector<edge>> E(town_n + 1);
    REP(i, road_n) {
      int to, from, cost, damage;
      cin >> to >> from >> cost >> damage;
      E[to].push_back({from, cost, damage});
      E[from].push_back({to, cost, damage});
    }

    bool G[town_n + 1][money + 1];
    fill_n((bool *)G, (town_n + 1) * (money + 1), false);

    priority_queue<P, vector<P>, greater<P>> que;
    que.push({1, money, 0});
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      if (p.from == town_n) {
        cout << p.damage << endl;
        break;
      }

      if (!G[p.from][p.money]) {
        G[p.from][p.money] = true;
        for (edge e : E[p.from]) {
          if (p.money >= e.cost) {
            que.push({e.from, p.money - e.cost, p.damage});
          }
          que.push({e.from, p.money, p.damage + e.damage});
        }
      }
    }
  }
}