Problem p00212 (C++) — buggy submission, verdict: 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 1e8

struct edge {
  int to, cost;
};

struct P {
  int from, cost, ticket;

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int ticket_n, node_n, edge_n, start, goal;

int main() {
  while (cin >> ticket_n >> node_n >> edge_n >> start >> goal,
         ticket_n | node_n | edge_n | start | goal) {
    vector<vector<edge>> E(node_n + 1);
    REP(i, edge_n) {
      int from, to, cost;
      cin >> from >> to >> cost;
      E[from].push_back({to, cost});
      E[to].push_back({from, cost});
    }

    vvi G(node_n + 1, vi(ticket_n + 1, INF));
    priority_queue<P, vector<P>, greater<P>> q;
    q.push({start, 0, ticket_n});
    while (!q.empty()) {
      P p = q.top();
      q.pop();

      if (p.from == goal) {
        cout << p.cost << endl;
        break;
      }

      int from = p.from, cost = p.cost, ticket = p.ticket;
      for (edge e : E[from]) {
        // use ticket
        if (G[e.to][ticket] > cost + e.cost) {
          G[e.to][ticket] = cost + e.cost;
          q.push({e.to, cost + e.cost, ticket});
        }
        // don't use ticket
        if (ticket - 1 >= 0) {
          if (G[e.to][ticket - 1] > cost + e.cost / 2) {
            G[e.to][ticket - 1] = cost + e.cost / 2;
            q.push({e.to, cost + e.cost / 2, ticket - 1});
          }
        }
      }
    }
  }
}

Error: 0

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 1e8

struct edge {
  int to, cost;
};

struct P {
  int from, cost, ticket;

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int ticket_n, node_n, edge_n, start, goal;

int main() {
  while (cin >> ticket_n >> node_n >> edge_n >> start >> goal,
         ticket_n | node_n | edge_n | start | goal) {
    vector<vector<edge>> E(node_n + 1);
    REP(i, edge_n) {
      int from, to, cost;
      cin >> from >> to >> cost;
      E[from].push_back({to, cost});
      E[to].push_back({from, cost});
    }

    vvi G(edge_n + 1, vi(ticket_n + 1, INF));
    priority_queue<P, vector<P>, greater<P>> q;
    q.push({start, 0, ticket_n});
    while (!q.empty()) {
      P p = q.top();
      q.pop();

      if (p.from == goal) {
        cout << p.cost << endl;
        break;
      }

      int from = p.from, cost = p.cost, ticket = p.ticket;
      for (edge e : E[from]) {
        // use ticket
        if (G[e.to][ticket] > cost + e.cost) {
          G[e.to][ticket] = cost + e.cost;
          q.push({e.to, cost + e.cost, ticket});
        }
        // don't use ticket
        if (ticket - 1 >= 0) {
          if (G[e.to][ticket - 1] > cost + e.cost / 2) {
            G[e.to][ticket - 1] = cost + e.cost / 2;
            q.push({e.to, cost + e.cost / 2, ticket - 1});
          }
        }
      }
    }
  }
}