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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int INF = 1e9;

int C, N, M, S, T;

class Cl {
public:
  int n, c, cost;
  Cl() {}
  Cl(int nn, int cc, int co) {
    n = nn;
    c = cc;
    cost = co;
  }
  bool operator<(const Cl &tmp) const { return cost > tmp.cost; }
};

int solve(vector<vector<P>> &v) {
  priority_queue<Cl> open;
  open.push(Cl(S, C, 0));
  vector<vector<int>> closed(N, vector<int>(C + 1, INF));
  closed[S][C] = 0;
  while (!open.empty()) {
    Cl cl = open.top();
    open.pop();
    if (closed[cl.n][cl.c] < cl.cost)
      continue;
    REP(i, v[cl.n].size()) {
      P nx = v[cl.n][i];
      if (closed[nx.first][cl.c] > cl.cost + nx.second) {
        closed[nx.first][cl.c] = cl.cost + nx.second;
        open.push(Cl(nx.first, cl.c, cl.cost + nx.second));
      }
      if (cl.c > 0 && closed[nx.first][cl.c - 1] > cl.cost + nx.second / 2) {
        closed[nx.first][cl.c - 1] = cl.cost + nx.second / 2;
        open.push(Cl(nx.first, cl.c - 1, cl.cost + nx.second / 2));
      }
    }
  }
  int res = INF;
  REP(i, C) res = min(res, closed[T][i]);
  return res;
}

int main() {
  while (cin >> C >> N >> M >> S >> T && C && N && M && S && T) {
    --T;
    --S;
    vector<vector<P>> v(N);
    REP(i, M) {
      int a, b, f;
      cin >> a >> b >> f;
      --a;
      --b;
      v[a].push_back(P(b, f));
      v[b].push_back(P(a, f));
    }
    cout << solve(v) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int INF = 1e9;

int C, N, M, S, T;

class Cl {
public:
  int n, c, cost;
  Cl() {}
  Cl(int nn, int cc, int co) {
    n = nn;
    c = cc;
    cost = co;
  }
  bool operator>(const Cl &tmp) const { return cost > tmp.cost; }
  bool operator<(const Cl &tmp) const { return cost < tmp.cost; }
};

int solve(vector<vector<P>> &v) {
  priority_queue<Cl> open;
  open.push(Cl(S, C, 0));
  vector<vector<int>> closed(N, vector<int>(C + 1, INF));
  while (!open.empty()) {
    Cl cl = open.top();
    open.pop();
    if (closed[cl.n][cl.c] <= cl.cost)
      continue;
    closed[cl.n][cl.c] = cl.cost;
    REP(i, v[cl.n].size()) {
      P nx = v[cl.n][i];
      open.push(Cl(nx.first, cl.c, cl.cost + nx.second));
      if (cl.c > 0)
        open.push(Cl(nx.first, cl.c - 1, cl.cost + nx.second / 2));
    }
  }
  int res = INF;
  REP(i, C) res = min(res, closed[T][i]);
  return res;
}

int main() {
  while (cin >> C >> N >> M >> S >> T && C && N && M && S && T) {
    --T;
    --S;
    vector<vector<P>> v(N);
    REP(i, M) {
      int a, b, f;
      cin >> a >> b >> f;
      --a;
      --b;
      v[a].push_back(P(b, f));
      v[b].push_back(P(a, f));
    }
    cout << solve(v) << endl;
  }
  return 0;
}