Problem p00212 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<pair<int, int>, int> NODE;
#define cost first.first
#define ticket first.second
#define pos second
#define ppp(a, b, c) make_pair(make_pair(a, b), c)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF (1 << 21)

int info[100][100];
int n;

int dijk(int a, int b, int c) {
  priority_queue<NODE, vector<NODE>, greater<NODE>> Q;
  int memo[100][11];
  rep(i, 100) rep(j, 11) memo[i][j] = INF;
  rep(j, 11) memo[a][j] = 0;

  Q.push(ppp(0, c, a));
  while (!Q.empty()) {
    NODE q = Q.top();
    Q.pop();
    for (int i = 0; i < n; i++) {
      if (info[q.pos][i] + q.cost < memo[i][q.ticket]) {
        memo[i][q.ticket] = info[q.pos][i] + q.cost;
        Q.push(ppp(info[q.pos][i] + q.cost, q.ticket, i));
      }
      if (q.ticket && info[q.pos][i] / 2 + q.cost < memo[i][q.ticket - 1]) {
        Q.push(ppp(info[q.pos][i] / 2 + q.cost, q.ticket - 1, i));
        memo[i][q.ticket - 1] = info[q.pos][i] / 2 + q.cost;
      }
    }
  }
  int mi = (1 << 21);
  rep(j, 11) mi = min(memo[b][j], mi);
  return mi;
}
int main() {
  int c, m, s, d, a, b, f;
  while (cin >> c >> n >> m >> s >> d, c) {
    s--;
    d--;
    rep(i, 100) rep(j, 100) info[i][j] = INF * 2;

    for (int i = 0; i < m; i++) {
      cin >> a >> b >> f;
      a--;
      b--;
      info[a][b] = info[b][a] = f;
    }
    cout << dijk(s, d, c) << endl;
  }
}

Error: TLE

Fixed submission:

#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int INF = 10000000;

int solve(int city[101][101], int s, int d, int c, int n) {
  int dist[11][101];
  int history[101];

  for (int i = 0; i < 11; i++) {
    for (int j = 0; j < 101; j++) {
      dist[i][j] = INF;
    }
  }

  for (int i = 0; i < 101; i++) {
    history[i] = 0;
  }

  for (int i = 0; i < 11; i++)
    dist[i][s] = 0;

  typedef pair<int, int> data;
  typedef pair<int, data> graph;
  priority_queue<graph, vector<graph>, greater<graph>> q;

  q.push(graph(0, data(s, c)));

  while (!q.empty()) {
    graph a = q.top();
    int u = a.second.first;
    int uc = a.second.second;
    q.pop();
    history[u] = 1;

    for (int i = 1; i <= n; i++) {

      if (dist[uc][i] > dist[uc][u] + city[u][i]) {
        dist[uc][i] = dist[uc][u] + city[u][i];
        q.push(graph(dist[uc][i], data(i, uc)));
      }

      if (uc && dist[uc - 1][i] > dist[uc][i] + city[u][i] / 2) {
        dist[uc - 1][i] = dist[uc][u] + city[u][i] / 2;
        q.push(graph(dist[uc - 1][i], data(i, uc - 1)));
      }
    }
  }
  int answer = INF;
  for (int i = 0; i < 11; i++)
    answer = min(dist[i][d], answer);
  return answer;
}

int main(int argc, char const *argv[]) {
  int c, n, m, s, d;
  int city[101][101];

  while (cin >> c >> n >> m >> s >> d && c != 0) {
    int a, b, f;
    for (int i = 0; i < 101; i++) {
      for (int j = 0; j < 101; j++) {
        city[i][j] = INF * 2;
      }
    }
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> f;
      city[a][b] = f;
      city[b][a] = f;
    }

    cout << solve(city, s, d, c, n) << endl;
  }

  return 0;
}