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

// AOJ 0212
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)

typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;

const int INF = 1 << 30;

int c, n, m, s, d;
std::vector<P> G[100];
int dist[100][11];

int dijkstra() {
  std::fill((int *)dist, (int *)(dist + 100), INF);
  dist[s][0] = 0;
  std::priority_queue<PP, std::vector<PP>, std::greater<PP>> pque;
  pque.push(PP(P(s, 0), 0));
  while (!pque.empty()) {
    PP p = pque.top();
    pque.pop();
    int v = p.first.first, cost = p.first.second, tic = p.second;

    if (dist[v][tic] < cost)
      continue;

    rep(i, G[v].size()) {
      P &e = G[v][i];

      if (tic < c && dist[e.first][tic + 1] > dist[v][tic] + e.second / 2) {
        dist[e.first][tic + 1] = dist[v][tic] + e.second / 2;
        pque.push(PP(P(e.first, dist[e.first][tic + 1]), tic + 1));
      }

      if (dist[e.first][tic] > dist[v][tic] + e.second) {
        dist[e.first][tic] = dist[v][tic] + e.second;
        pque.push(PP(P(e.first, dist[e.first][tic]), tic));
      }
    }
  }

  return *std::min_element(dist[d], dist[d] + 11);
}

int main() {
  while (scanf("%d%d%d%d%d", &c, &n, &m, &s, &d), c | n | m | s | d) {
    --s;
    --d;
    rep(i, 100) G[i].clear();

    rep(i, m) {
      int a, b, f;
      scanf("%d%d%d", &a, &b, &f);
      --a;
      --b;
      G[a].push_back(P(b, f));
      G[b].push_back(P(a, f));
    }

    printf("%d\\n", dijkstra());
  }

  return 0;
}

Error: 0

Fixed submission:

// AOJ 0212
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)

typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;

const int INF = 1 << 30;

int c, n, m, s, d;
std::vector<P> G[100];
int dist[100][11];

int dijkstra() {
  std::fill((int *)dist, (int *)(dist + 100), INF);
  dist[s][0] = 0;
  std::priority_queue<PP, std::vector<PP>, std::greater<PP>> pque;
  pque.push(PP(P(s, 0), 0));
  while (!pque.empty()) {
    PP p = pque.top();
    pque.pop();
    int v = p.first.first, cost = p.first.second, tic = p.second;

    if (dist[v][tic] < cost)
      continue;

    rep(i, G[v].size()) {
      P &e = G[v][i];

      if (tic < c && dist[e.first][tic + 1] > dist[v][tic] + e.second / 2) {
        dist[e.first][tic + 1] = dist[v][tic] + e.second / 2;
        pque.push(PP(P(e.first, dist[e.first][tic + 1]), tic + 1));
      }

      if (dist[e.first][tic] > dist[v][tic] + e.second) {
        dist[e.first][tic] = dist[v][tic] + e.second;
        pque.push(PP(P(e.first, dist[e.first][tic]), tic));
      }
    }
  }

  return *std::min_element(dist[d], dist[d] + 11);
}

int main() {
  while (scanf("%d%d%d%d%d", &c, &n, &m, &s, &d), c | n | m | s | d) {
    --s;
    --d;
    rep(i, 101) G[i].clear();

    rep(i, m) {
      int a, b, f;
      scanf("%d%d%d", &a, &b, &f);
      --a;
      --b;
      G[a].push_back(P(b, f));
      G[b].push_back(P(a, f));
    }

    printf("%d\\n", dijkstra());
  }

  return 0;
}