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

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

using namespace std;

const int INF = 10000000;

int city[101][101];

int solve(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 = 1; j < 101; j++) {
      dist[i][j] = INF;
    }
  }

  for (int i = 1; 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][u] + 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;

  while (cin >> c >> n >> m >> s >> d && c != 0) {
    int a, b, f;
    for (int i = 1; i < 101; i++) {
      for (int j = 1; 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(s, d, c, n) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

const int INF = 10000000;

int city[101][101];

int solve(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 = 1; j < 101; j++) {
      dist[i][j] = INF;
    }
  }

  for (int i = 1; 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;

  while (cin >> c >> n >> m >> s >> d && c != 0) {
    int a, b, f;
    for (int i = 1; i < 101; i++) {
      for (int j = 1; 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(s, d, c, n) << endl;
  }

  return 0;
}