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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
using namespace std;
typedef pair<int, int> P; // F -> to, S -> cost
typedef pair<P, int> PP;  // F->P(to,cost), S->discount
int c, n, m, s, d;
vector<P> vec[1000]; // F -> pos , S -> cost
int MD[1000][13];
const int INF = 1 << 29;

void dijkstra() {
  priority_queue<PP, vector<PP>, greater<PP>> que;
  rep(i, n + 1) rep(j, 13) MD[i][j] = INF;
  // syokika
  MD[s][c] = 0;
  que.push(PP(P(0, s), c)); // dis,start,discount

  while (!que.empty()) {
    PP p = que.top();
    que.pop(); // priority_queのPPと
    int v = p.F.S;

    for (int i = 0; i < vec[v].size(); i++) {
      P po = vec[v][i]; // vectorのPPは意味が違うので注意！！！
      // if(MD[v][po.S] < po.F.S)continue;
      if (MD[po.F][p.S] > p.F.F + po.S) {
        MD[po.F][p.S] = p.F.F + po.S;
        que.push(PP(P(MD[po.F][p.S], po.F), p.S));
      }
      if (p.S > 0 && MD[po.F][p.S - 1] > p.F.F + po.S / 2) {
        MD[po.F][p.S - 1] = p.F.F + po.S / 2;
        que.push(PP(P(MD[po.F][p.S - 1], po.F), p.S - 1));
      }
    }
  }
}

int main() {
  int x, y, co;
  while (true) {
    cin >> c >> n >> m >> s >> d;
    if (c + n + m + s + d == 0)
      break;
    rep(i, 1000) vec[i].clear();
    rep(i, m) {
      cin >> x >> y >> co;
      vec[x].push_back(P(y, co));
      vec[y].push_back(P(x, co));
    }
    dijkstra();
    int ans = INF;
    for (int i = 0; i <= c; i++) {
      ans = min(ans, MD[d][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

// Version.1

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
using namespace std;
typedef pair<int, int> P; // F -> to, S -> cost
typedef pair<P, int> PP;  // F->P(to,cost), S->discount
int c, n, m, s, d;
vector<PP> vec[1000];
int MD[1000][13];
const int INF = 1 << 29;

void dijkstra() {
  priority_queue<PP, vector<PP>, greater<PP>> que;
  rep(i, n + 1) rep(j, 13) MD[i][j] = INF;
  // syokika
  MD[s][c] = 0;
  que.push(PP(P(0, s), c)); // dis,start,discount

  while (!que.empty()) {
    PP p = que.top();
    que.pop(); // priority_queのPPと
    int v = p.F.S;

    for (int i = 0; i < vec[v].size(); i++) {
      PP po = vec[v][i]; // vectorのPPは意味が違うので注意！！！
      // if(MD[v][po.S] < po.F.S)continue;
      if (MD[po.F.F][p.S] > p.F.F + po.F.S) {
        MD[po.F.F][p.S] = p.F.F + po.F.S;
        que.push(PP(P(MD[po.F.F][p.S], po.F.F), p.S));
      }
      if (po.S > 0 && MD[po.F.F][p.S - 1] > p.F.F + po.F.S / 2) {
        MD[po.F.F][p.S - 1] = p.F.F + po.F.S / 2;
        que.push(PP(P(MD[po.F.F][p.S - 1], po.F.F), p.S - 1));
        vec[v][i].S--;
      }
    }
  }
}

int main() {
  int x, y, co;
  while (true) {
    cin >> c >> n >> m >> s >> d;
    if (c + n + m + s + d == 0)
      break;
    rep(i, 1000) vec[i].clear();
    rep(i, m) {
      cin >> x >> y >> co;
      vec[x].push_back(PP(P(y, co), c));
      vec[y].push_back(PP(P(x, co), c));
    }
    dijkstra();
    int ans = INF;
    for (int i = 0; i <= c; i++) {
      ans = min(ans, MD[d][i]);
    }
    cout << ans << endl;
  }
  return 0;
}