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

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

#define reep(i, n, m) for (int i = (n); i < (m); i++)
#define rep(i, n) reep(i, 0, n)

using namespace std;

class Edge {
public:
  int to;
  int cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

class Node {
public:
  int distance;
  int num;
  int ticket;
  Node() {}
  Node(int distance, int num, int ticket)
      : distance(distance), num(num), ticket(ticket) {}
  bool operator<(const Node &b) const { return distance > b.distance; }
};

vector<Edge> edge[101];
bool flg[101][11];

int c, n, m, s, d;

int solve() {

  priority_queue<Node> Q;
  Q.push(Node(0, s, 0));
  int ans = 1000000;
  while (!Q.empty()) {
    Node node = Q.top();
    Q.pop();
    if (flg[node.num][node.ticket]) {
      continue;
    }
    flg[node.num][node.ticket] = true;
    // printf("num->%d cost->%d
    // ticket->%d\\n",node.num,node.distance,node.ticket);
    if (node.num == d) {
      ans = min(ans, node.distance);
    }

    rep(i, edge[node.num].size()) {
      Edge e = edge[node.num][i];
      int cost = node.distance + e.cost;
      Q.push(Node(cost, e.to, node.ticket));
      if (node.ticket < c) {
        int cost = node.distance + e.cost / 2;
        Q.push(Node(cost, e.to, node.ticket + 1));
      }
    }
  }
  return ans;
}

void init() {
  rep(i, 101) {
    edge[i].clear();
    rep(k, 11) { flg[i][k] = 0; }
  }
}

int main() {

  while (cin >> c >> n >> m >> s >> d, !m == 0) {
    init();
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      edge[a].push_back(Edge(b, c));
      edge[b].push_back(Edge(a, c));
    }
    cout << solve() << endl;
  }
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

#define reep(i, n, m) for (int i = (n); i < (m); i++)
#define rep(i, n) reep(i, 0, n)

using namespace std;

class Edge {
public:
  int to;
  int cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

class Node {
public:
  int distance;
  int num;
  int ticket;
  Node() {}
  Node(int distance, int num, int ticket)
      : distance(distance), num(num), ticket(ticket) {}
  bool operator<(const Node &b) const { return distance > b.distance; }
};

vector<Edge> edge[101];
bool flg[101][11];

int c, n, m, s, d;

int solve() {

  priority_queue<Node> Q;
  Q.push(Node(0, s, 0));
  int ans = 1000000;
  while (!Q.empty()) {
    Node node = Q.top();
    Q.pop();
    if (flg[node.num][node.ticket]) {
      continue;
    }
    flg[node.num][node.ticket] = true;
    // printf("num->%d cost->%d
    // ticket->%d\\n",node.num,node.distance,node.ticket);
    if (node.num == d) {
      ans = min(ans, node.distance);
    }

    rep(i, edge[node.num].size()) {
      Edge e = edge[node.num][i];
      int cost = node.distance + e.cost;
      Q.push(Node(cost, e.to, node.ticket));
      if (node.ticket < c) {
        int cost = node.distance + e.cost / 2;
        Q.push(Node(cost, e.to, node.ticket + 1));
      }
    }
  }
  return solve();
}

void init() {
  rep(i, 101) {
    edge[i].clear();
    rep(k, 11) { flg[i][k] = 0; }
  }
}

int main() {

  while (cin >> c >> n >> m >> s >> d, !m == 0) {
    init();
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      edge[a].push_back(Edge(b, c));
      edge[b].push_back(Edge(a, c));
    }
    cout << solve() << endl;
  }
}