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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUGLOCAL
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    debug << *i << " ";
  debug << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; i--)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

int c, n, m, s, d;
int edge[1111][1111];
int cost[111][1111];

int main() {
  IL {
    cin >> c >> n >> m >> s >> d;
    --s, --d;
    if (c == 0)
      break;
    int fr, to, co;
    rep(i, 111) rep(j, 111) edge[i][j] = INF;
    rep(i, m) {
      cin >> fr >> to >> co;
      --fr, --to;
      chmin(edge[fr][to], co);
      chmin(edge[to][fr], co);
    }
    rep(i, 11) rep(j, 111) cost[i][j] = INF;
    priority_queue<pair<int, pint>, vector<pair<int, pint>>,
                   greater<pair<int, pint>>>
        que;
    que.push(MP(0, MP(0, s)));
    while (!que.empty()) {
      pair<int, pint> p;
      p = que.top();
      que.pop();
      if (p.second.first > c)
        continue;
      if (cost[p.second.first][p.second.second] <= p.first)
        continue;
      cost[p.second.first][p.second.second] = p.first;
      rep(i, n) {
        if (edge[p.second.second][i] == INF)
          continue;
        pair<int, pint> next = p;
        next.first += edge[p.second.second][i] / 2;
        ++next.second.first;
        next.second.second = i;
        que.push(next);
        next.first += edge[p.second.second][i] / 2;
        --next.second.first;
        que.push(next);
      }
    }
    int res = INF;
    repn(i, 0, c) { chmin(res, cost[i][d]); }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUGLOCAL
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    debug << *i << " ";
  debug << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; i--)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

int c, n, m, s, d;
int edge[1111][1111];
int cost[111][1111];

int main() {
  IL {
    cin >> c >> n >> m >> s >> d;
    --s, --d;
    if (c == 0)
      break;
    int fr, to, co;
    rep(i, 111) rep(j, 111) edge[i][j] = INF;
    rep(i, m) {
      cin >> fr >> to >> co;
      --fr, --to;
      chmin(edge[fr][to], co);
      chmin(edge[to][fr], co);
    }
    rep(i, 11) rep(j, 111) cost[i][j] = INF;
    priority_queue<pair<int, pint>> que;
    que.push(MP(0, MP(0, s)));
    while (!que.empty()) {
      pair<int, pint> p;
      p = que.top();
      que.pop();
      if (p.second.first > c)
        continue;
      if (cost[p.second.first][p.second.second] <= p.first)
        continue;
      if (p.second.first > c)
        continue;
      cost[p.second.first][p.second.second] = p.first;
      rep(i, n) {
        if (edge[p.second.second][i] == INF)
          continue;
        pair<int, pint> next = p;
        next.first += edge[p.second.second][i] / 2;
        ++next.second.first;
        next.second.second = i;
        que.push(next);
        next.first += edge[p.second.second][i] / 2;
        --next.second.first;
        que.push(next);
      }
    }
    int res = INF;
    repn(i, 0, n) { chmin(res, cost[i][d]); }
    cout << res << endl;
  }
  return 0;
}