Problem p00212 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <queue>

#define mp make_pair

using namespace std;

typedef pair<int, int> pii;

const int inf = 1 << 29;
int adj[100][100], memo[100][11];
bool visited[100][11];

int main() {
  for (int c, m, n, src, dst; scanf("%d%d%d%d%d", &c, &m, &n, &src, &dst), c;) {
    src--, dst--;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        adj[i][j] = (i == j ? 0 : inf);
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= c; j++)
        memo[i][j] = inf, visited[i][j] = false;
    for (int i = 0; i < m; i++) {
      int a, b, fee;
      scanf("%d%d%d", &a, &b, &fee);
      a--, b--;
      adj[a][b] = adj[b][a] = fee;
    }

    memo[src][0] = 0;
    priority_queue<pair<int, pii>> pq;
    pq.push(mp(0, mp(src, 0)));
    while (!pq.empty()) {
      pair<int, pii> a = pq.top();
      pq.pop();
      int fee = -a.first;
      int u = a.second.first;
      int tic = a.second.second;
      if (u == dst)
        break;

      if (visited[u][tic])
        continue;
      visited[u][tic] = true;

      for (int v = 0; v < n; v++) {
        if (!visited[v][tic] && fee + adj[u][v] < memo[v][tic]) {
          memo[v][tic] = fee + adj[u][v];
          pq.push(mp(-memo[v][tic], mp(v, tic)));
        }
        if (tic < c && !visited[v][tic + 1] &&
            fee + adj[u][v] / 2 < memo[v][tic + 1]) {
          memo[v][tic + 1] = fee + adj[u][v] / 2;
          pq.push(mp(-memo[v][tic + 1], mp(v, tic + 1)));
        }
      }
    }

    int feemin = inf;
    for (int i = 0; i <= c; i++)
      feemin = min(feemin, memo[dst][i]);
    printf("%d\n", feemin);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <queue>

#define mp make_pair

using namespace std;

typedef pair<int, int> pii;

const int inf = 1 << 29;
int adj[100][100], memo[100][11];
bool visited[100][11];

int main() {
  for (int c, n, m, src, dst; scanf("%d%d%d%d%d", &c, &n, &m, &src, &dst), c;) {
    src--, dst--;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        adj[i][j] = (i == j ? 0 : inf);
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= c; j++)
        memo[i][j] = inf, visited[i][j] = false;
    for (int i = 0; i < m; i++) {
      int a, b, fee;
      scanf("%d%d%d", &a, &b, &fee);
      a--, b--;
      adj[a][b] = adj[b][a] = fee;
    }

    memo[src][0] = 0;
    priority_queue<pair<int, pii>> pq;
    pq.push(mp(0, mp(src, 0)));
    while (!pq.empty()) {
      pair<int, pii> a = pq.top();
      pq.pop();
      int fee = -a.first;
      int u = a.second.first;
      int tic = a.second.second;
      if (u == dst)
        break;

      if (visited[u][tic])
        continue;
      visited[u][tic] = true;

      for (int v = 0; v < n; v++) {
        if (!visited[v][tic] && fee + adj[u][v] < memo[v][tic]) {
          memo[v][tic] = fee + adj[u][v];
          pq.push(mp(-memo[v][tic], mp(v, tic)));
        }
        if (tic < c && !visited[v][tic + 1] &&
            fee + adj[u][v] / 2 < memo[v][tic + 1]) {
          memo[v][tic + 1] = fee + adj[u][v] / 2;
          pq.push(mp(-memo[v][tic + 1], mp(v, tic + 1)));
        }
      }
    }

    int feemin = inf;
    for (int i = 0; i <= c; i++)
      feemin = min(feemin, memo[dst][i]);
    printf("%d\n", feemin);
  }

  return 0;
}