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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define INF 1000000000;
#define dis first
#define mati second.first
#define sta second.second
using namespace std;
typedef pair<int, pair<int, int>> P;
vector<P> mp[102];
int c, n, m, start, goal;

int bit_count(int a) {
  a = (a & 0x55555555) + ((a & 0xAAAAAAAA) >> 1);
  a = (a & 0x33333333) + ((a & 0xCCCCCCCC) >> 2);
  a = (a & 0x0F0F0F0F) + ((a & 0xF0F0F0F0) >> 4);
  a = (a & 0x00FF00FF) + ((a & 0xFF00FF00) >> 8);
  a = (a & 0x0000FFFF) + ((a & 0xFFFF0000) >> 16);
  return a;
}
int dijkstra() {
  int flg[12][102] = {}, dis[12][102];
  for (int j = 0; j <= c; j++)
    for (int i = 0; i <= n; i++)
      dis[j][i] = -INF;

  for (int j = 1; j <= c; j++)
    flg[j][start] = 1;

  priority_queue<P> Q;
  P t;
  t.dis = 0, t.mati = start, t.sta = 0;
  Q.push(t);

  while (!Q.empty()) {
    t = Q.top(), Q.pop();
    if (flg[t.sta][t.mati] == 1)
      continue;
    flg[t.sta][t.mati] = 1;
    for (int i = 0; i < mp[t.mati].size(); i++) {
      int ndis = mp[t.mati][i].dis;
      int nmati = mp[t.mati][i].mati;
      int nsta = t.sta + 1;
      P tt;
      if (dis[t.sta][nmati] < t.dis - ndis && flg[t.sta][nmati] == 0) {
        tt.dis = t.dis - ndis, tt.mati = nmati, tt.sta = t.sta;
        dis[t.sta][nmati] = t.dis - ndis;
        Q.push(tt);
      }
      if (nsta <= c && dis[nsta][nmati] < t.dis - ndis / 2 &&
          flg[nsta][nmati] == 0) {
        tt.dis = t.dis - ndis / 2, tt.mati = nmati, tt.sta = nsta;
        dis[nsta][nmati] = t.dis - ndis / 2;
        Q.push(tt);
      }
    }
  }

  int ans = -INF;
  for (int j = 0; j <= c; j++)
    ans = max(ans, dis[j][goal]);

  return -ans;
}

int main() {

  while (1) {
    cin >> c >> n >> m >> start >> goal;
    if (c == 0 && n == 0 && m == 0 && start == 0 && goal == 0)
      break;

    for (int i = 0; i <= n; i++)
      mp[i].clear();
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      P t;
      t.dis = f, t.mati = b, t.sta = 0;
      mp[a].push_back(t);
      t.mati = a;
      mp[b].push_back(t);
    }

    cout << dijkstra() << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define INF 1000000000;
#define dis first
#define mati second.first
#define sta second.second
using namespace std;
typedef pair<int, pair<int, int>> P;
vector<P> mp[102];
int c, n, m, start, goal;

int bit_count(int a) {
  a = (a & 0x55555555) + ((a & 0xAAAAAAAA) >> 1);
  a = (a & 0x33333333) + ((a & 0xCCCCCCCC) >> 2);
  a = (a & 0x0F0F0F0F) + ((a & 0xF0F0F0F0) >> 4);
  a = (a & 0x00FF00FF) + ((a & 0xFF00FF00) >> 8);
  a = (a & 0x0000FFFF) + ((a & 0xFFFF0000) >> 16);
  return a;
}
int dijkstra() {
  int flg[11][102] = {}, dis[11][102];
  for (int j = 0; j <= c; j++)
    for (int i = 0; i <= n; i++)
      dis[j][i] = -INF;

  for (int j = 1; j <= n; j++)
    flg[j][start] = 1;

  priority_queue<P> Q;
  P t;
  t.dis = 0, t.mati = start, t.sta = 0;
  Q.push(t);

  while (!Q.empty()) {
    t = Q.top(), Q.pop();
    if (flg[t.sta][t.mati] == 1)
      continue;
    flg[t.sta][t.mati] = 1;
    for (int i = 0; i < mp[t.mati].size(); i++) {
      int ndis = mp[t.mati][i].dis;
      int nmati = mp[t.mati][i].mati;
      int nsta = t.sta + 1;
      P tt;
      if (dis[t.sta][nmati] < t.dis - ndis && flg[t.sta][nmati] == 0) {
        tt.dis = t.dis - ndis, tt.mati = nmati, tt.sta = t.sta;
        dis[t.sta][nmati] = t.dis - ndis;
        Q.push(tt);
      }
      if (nsta <= c && dis[nsta][nmati] < t.dis - ndis / 2 &&
          flg[nsta][nmati] == 0) {
        tt.dis = t.dis - ndis / 2, tt.mati = nmati, tt.sta = nsta;
        dis[nsta][nmati] = t.dis - ndis / 2;
        Q.push(tt);
      }
    }
  }

  int ans = -INF;
  for (int j = 0; j <= c; j++)
    ans = max(ans, dis[j][goal]);

  return -ans;
}

int main() {

  while (1) {
    cin >> c >> n >> m >> start >> goal;
    if (c == 0 && n == 0 && m == 0 && start == 0 && goal == 0)
      break;

    for (int i = 0; i <= n; i++)
      mp[i].clear();
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      P t;
      t.dis = f, t.mati = b, t.sta = 0;
      mp[a].push_back(t);
      t.mati = a;
      mp[b].push_back(t);
    }

    cout << dijkstra() << endl;
  }

  return 0;
}