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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

struct edge {
  int to, cost;
};
struct state {
  int cur, card, cost;
  bool operator<(const state &lhs) const { return cost > lhs.cost; }
};

typedef pair<int, int> P;

const int kInf = 1 << 28;
int C, N, M, S, D;
vector<edge> es[102];
int d[102][12];

int solve() {
  // init
  for (int i = 0; i < 102; ++i)
    for (int j = 0; j < 12; ++j)
      d[i][j] = kInf;
  // dijkstra
  priority_queue<state> Q;
  d[S][C] = 0;
  Q.push((state){S, C, 0});
  while (!Q.empty()) {
    state s = Q.top();
    Q.pop();
    if (d[s.cur][s.card] < s.cost)
      continue;
    rep(i, es[s.cur].size()) {
      const edge &e = es[s.cur][i];
      // without ticket
      if (d[e.to][s.card] > s.cost + e.cost) {
        d[e.to][s.card] = s.cost + e.cost;
        Q.push((state){e.to, s.card, s.cost + e.cost});
      }
      // use ticket
      if (s.card > 0 && d[e.to][s.card - 1] > s.cost + (e.cost / 2)) {
        d[e.to][s.card - 1] = s.cost + (e.cost / 2);
        Q.push((state){e.to, s.card - 1, s.cost + (e.cost / 2)});
      }
    }
  }
  return *min_element(d[D], d[D] + 12);
}

int main() {
  while (scanf("%d%d%d%d%d", &C, &N, &M, &S, &D), C) {
    --S;
    --D;
    rep(i, N) { es[i].clear(); }
    rep(i, M) {
      int a, b, f;
      scanf("%d%d%d", &a, &b, &f);
      --a;
      --b;
      es[a].push_back((edge){b, f});
      es[b].push_back((edge){a, f});
    }
    printf("%d\\n", solve());
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

struct edge {
  int to, cost;
};
struct state {
  int cur, card, cost;
  bool operator<(const state &lhs) const { return cost > lhs.cost; }
};

typedef pair<int, int> P;

const int kInf = 1 << 28;
int C, N, M, S, D;
vector<edge> es[102];
int d[102][12];

int solve() {
  // init
  for (int i = 0; i < 102; ++i)
    for (int j = 0; j < 12; ++j)
      d[i][j] = kInf;
  // dijkstra
  priority_queue<state> Q;
  d[S][C] = 0;
  Q.push((state){S, C, 0});
  while (!Q.empty()) {
    state s = Q.top();
    Q.pop();
    if (d[s.cur][s.card] < s.cost)
      continue;
    rep(i, es[s.cur].size()) {
      const edge &e = es[s.cur][i];
      // without ticket
      if (d[e.to][s.card] > s.cost + e.cost) {
        d[e.to][s.card] = s.cost + e.cost;
        Q.push((state){e.to, s.card, s.cost + e.cost});
      }
      // use ticket
      if (s.card > 0 && d[e.to][s.card - 1] > s.cost + (e.cost / 2)) {
        d[e.to][s.card - 1] = s.cost + (e.cost / 2);
        Q.push((state){e.to, s.card - 1, s.cost + (e.cost / 2)});
      }
    }
  }
  return *min_element(d[D], d[D] + 12);
}

int main() {
  while (scanf("%d%d%d%d%d", &C, &N, &M, &S, &D), C) {
    --S;
    --D;
    rep(i, N) { es[i].clear(); }
    rep(i, N) {
      int a, b, f;
      scanf("%d%d%d", &a, &b, &f);
      --a;
      --b;
      es[a].push_back((edge){b, f});
      es[b].push_back((edge){a, f});
    }
    printf("%d\\n", solve());
  }
  return 0;
}