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

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, pair<int, int>> P;

struct Edge {
  int to, cost;
  Edge(int _to, int _cost) : to(_to), cost(_cost){};
};

int minCost[110][12];
vector<Edge> edge[110];
static const int INF = INT_MAX;

int c, n, m, s, d;

int dijkstra(void) {
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(make_pair(0, make_pair(s, c)));

  for (int i = 0; i < 110; i++)
    for (int j = 0; j < 12; j++)
      minCost[i][j] = INF;

  while (!que.empty()) {
    P p = que.top();
    que.pop();

    for (int i = 0; i < edge[p.second.first].size(); i++) {
      Edge e = edge[p.second.first][i];
      if (p.first + e.cost < minCost[e.to][p.second.second]) {
        minCost[e.to][p.second.second] = p.first + e.cost;
        que.push(make_pair(p.first + e.cost, make_pair(e.to, p.second.second)));
      }
    }

    if (p.second.second == 0)
      continue;

    for (int j = 0; j < edge[p.second.first].size(); j++) {
      Edge e = edge[p.second.first][j];
      if (p.first + e.cost / 2 < minCost[e.to][p.second.second - 1]) {
        minCost[e.to][p.second.second - 1] = p.first + e.cost / 2;
        que.push(make_pair(p.first + e.cost / 2,
                           make_pair(e.to, p.second.second - 1)));
      }
    }
  }
  int ans = INT_MAX;
  for (int i = 0; i < 12; i++)
    ans = min(minCost[d][i], ans);

  return ans;
}

int main(void) {
  int a, b, f;

  while (cin >> c >> n >> m >> s >> d, c | n | m | s | d) {

    for (int i = 0; i <= n; i++)
      edge[i].clear();

    for (int i = 0; i < m; i++) {
      cin >> a >> b >> f;
      edge[a].push_back(Edge(b, f));
      edge[b].push_back(Edge(a, f));
    }

    cout << dijkstra() << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, pair<int, int>> P;

struct Edge {
  int to, cost;
  Edge(int _to, int _cost) : to(_to), cost(_cost){};
};

int minCost[102][12];
vector<Edge> edge[102];
static const int INF = INT_MAX;

int c, n, m, s, d;

int dijkstra(void) {
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(make_pair(0, make_pair(s, c)));

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      minCost[i][j] = INF;

  while (!que.empty()) {
    P p = que.top();
    que.pop();

    if (p.second.first == d)
      return p.first;

    for (int i = 0; i < edge[p.second.first].size(); i++) {
      Edge e = edge[p.second.first][i];
      if (p.first + e.cost < minCost[e.to][p.second.second]) {
        minCost[e.to][p.second.second] = p.first + e.cost;
        que.push(make_pair(p.first + e.cost, make_pair(e.to, p.second.second)));
      }
    }

    if (p.second.second == 0)
      continue;

    for (int j = 0; j < edge[p.second.first].size(); j++) {
      Edge e = edge[p.second.first][j];
      if (p.first + e.cost / 2 < minCost[e.to][p.second.second - 1]) {
        minCost[e.to][p.second.second - 1] = p.first + e.cost / 2;
        que.push(make_pair(p.first + e.cost / 2,
                           make_pair(e.to, p.second.second - 1)));
      }
    }
  }
  return -1;
}

int main(void) {
  int a, b, f;

  while (cin >> c >> n >> m >> s >> d, c | n | m | s | d) {

    for (int i = 0; i <= n; i++)
      edge[i].clear();

    for (int i = 0; i < m; i++) {
      cin >> a >> b >> f;
      edge[a].push_back(Edge(b, f));
      edge[b].push_back(Edge(a, f));
    }

    cout << dijkstra() << endl;
  }

  return 0;
}