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

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
// int in(){int x;cin>>x;return x;}

int main() {
  int c, n, m, s, d;
  while (cin >> c >> n >> m >> s >> d && c && n && m && s && d) {
    int flg[110][20] = {};
    for (int i = 0; i < 110; i++)
      for (int j = 0; j < 20; j++)
        flg[i][j] = 9999999;
    int ans = -1;
    priority_queue<PP, vector<PP>, greater<PP>> q;
    vector<P> way[110];
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      way[a].push_back(P(b, f));
      way[b].push_back(P(a, f));
    }
    PP next;
    q.push(PP(0, P(s, c)));
    while (!q.empty()) {
      PP now = q.top();
      q.pop();
      if (flg[now.second.first][now.second.second] != 9999999) {
        continue;
      }
      flg[now.second.first][now.second.second] = now.first;
      if (now.second.first == d) {
        ans = now.first;
        break;
      }

      for (int i = 0; i < way[now.second.first].size(); i++) {
        if (flg[way[now.second.first][i].first][now.second.second] >=
            now.first + way[now.second.first][i].second) {
          next.first = now.first + way[now.second.first][i].second;
          next.second.first = way[now.second.first][i].first;
          next.second.second = now.second.second;
          q.push(next);
        }
        if (now.second.second == 0)
          continue;
        if (flg[way[now.second.first][i].first][now.second.second - 1] >=
            now.first + way[now.second.first][i].second / 2) {
          next.first = now.first + way[now.second.first][i].second / 2;
          next.second.first = way[now.second.first][i].first;
          next.second.second = now.second.second - 1;
          q.push(next);
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
// int in(){int x;cin>>x;return x;}

int main() {
  int c, n, m, s, d;
  while (cin >> c >> n >> m >> s >> d && c && n && m && s && d) {
    int flg[110] = {};
    int ans = 0;
    priority_queue<PP, vector<PP>, greater<PP>> q;
    vector<pair<int, int>> way[110];
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      way[a].push_back(pair<int, int>(b, f));
      way[b].push_back(pair<int, int>(a, f));
    }
    pair<int, pair<int, int>> next;
    next.first = 0;
    next.second.first = s;
    next.second.second = c;
    q.push(next);
    while (!q.empty()) {
      pair<int, pair<int, int>> now = q.top();
      q.pop();
      flg[now.second.first] = 0;
      if (now.second.first == d) {
        ans = now.first;
        break;
      }

      for (int i = 0; i < way[now.second.first].size(); i++) {
        if (flg[way[now.second.first][i].first] == 1)
          continue;
        next.first = now.first + way[now.second.first][i].second;
        next.second.first = way[now.second.first][i].first;
        q.push(next);
        next.first = now.first + way[now.second.first][i].second / 2;
        next.second.first = way[now.second.first][i].first;
        next.second.second = now.second.second - 1;
        q.push(next);
      }
    }
    cout << ans << endl;
  }

  return 0;
}