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

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <random>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef __int128_t ll;
typedef __int128_t ll128;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

static uint MD = mod;
struct ModInt {
  using M = ModInt;
  uint v;
  ModInt(ll _v = 0) { set_v(uint(_v % MD + MD)); }
  M &set_v(uint _v) {
    v = (_v < MD) ? _v : _v - MD;
    return *this;
  }
  explicit operator bool() const { return v != 0; }
  M operator-() const { return M() - *this; }
  M operator+(const M &r) const { return M().set_v(v + r.v); }
  M operator-(const M &r) const { return M().set_v(v + MD - r.v); }
  M operator*(const M &r) const { return M().set_v(uint(ull(v) * r.v % MD)); }
  M operator/(const M &r) const { return *this * r.inv(); }
  M &operator+=(const M &r) { return *this = *this + r; }
  M &operator-=(const M &r) { return *this = *this - r; }
  M &operator*=(const M &r) { return *this = *this * r; }
  M &operator/=(const M &r) { return *this = *this / r; }
  bool operator==(const M &r) const { return v == r.v; }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1)
        r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const { return pow(MD - 2); }
  friend ostream &operator<<(ostream &os, const M &r) { return os << r.v; }
};
using Mint = ModInt;
// head
//  cin >> t;
//  for (int cas = 1; cas <= t; ++cas)
//  {
//  	cout << "Case #" << cas << ": ";
//  }
long long tn, a, b, c, d;

ll getCost(ll num, ll md, ll &ret) {
  ll cc = (num % md) * d;
  ll nn = num - (num % md);
  ll xx = a;
  if (md == 3)
    xx = b;
  if (md == 5)
    xx = c;
  if (xx < d * (nn - nn / md)) {
    cc += xx;
  } else {
    cc += d * (nn - nn / md);
  }
  ret = nn / md;
  return cc;
}

ll getCost2(ll num, ll md, ll &ret) {
  ll tt = (num % md);
  if (tt == 0) {
    ret = num;
    return 0;
  }
  ll cc = (md - tt) * d;
  ll nn = num + (md - tt);
  ll xx = a;
  if (md == 3)
    xx = b;
  if (md == 5)
    xx = c;
  if (xx < d * (nn - nn / md)) {
    cc += xx;
  } else {
    cc += d * (nn - nn / md);
  }
  ret = nn / md;
  return cc;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> tn >> a >> b >> c >> d;

    queue<ll> q;
    q.push(tn);

    long long ans = LONG_LONG_MAX;
    unordered_map<ll, ll> mmap;
    mmap[tn] = 0;

    long long cnt = 0;
    while (!q.empty()) {
      ll tmp = q.front();
      q.pop();

      ll num = tmp;
      ll cost = mmap[tmp];

      cnt++;

      if (num == 0) {
        if (cost < ans)
          ans = cost;
        continue;
      }

      ll ret;
      ll cc = getCost(num, 2, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(ret);
      }

      cc = getCost(num, 3, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(ret);
      }

      cc = getCost(num, 5, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(ret);
      }

      cc = getCost2(num, 2, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(ret);
      }

      cc = getCost2(num, 3, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(ret);
      }

      cc = getCost2(num, 5, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(ret);
      }
    }

    cout << ans << endl;
    // cout<<"cnt:"<<cnt<<endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <random>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef __int128_t ll;
typedef __int128_t ll128;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

static uint MD = mod;
struct ModInt {
  using M = ModInt;
  uint v;
  ModInt(ll _v = 0) { set_v(uint(_v % MD + MD)); }
  M &set_v(uint _v) {
    v = (_v < MD) ? _v : _v - MD;
    return *this;
  }
  explicit operator bool() const { return v != 0; }
  M operator-() const { return M() - *this; }
  M operator+(const M &r) const { return M().set_v(v + r.v); }
  M operator-(const M &r) const { return M().set_v(v + MD - r.v); }
  M operator*(const M &r) const { return M().set_v(uint(ull(v) * r.v % MD)); }
  M operator/(const M &r) const { return *this * r.inv(); }
  M &operator+=(const M &r) { return *this = *this + r; }
  M &operator-=(const M &r) { return *this = *this - r; }
  M &operator*=(const M &r) { return *this = *this * r; }
  M &operator/=(const M &r) { return *this = *this / r; }
  bool operator==(const M &r) const { return v == r.v; }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1)
        r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const { return pow(MD - 2); }
  friend ostream &operator<<(ostream &os, const M &r) { return os << r.v; }
};
using Mint = ModInt;
// head
//  cin >> t;
//  for (int cas = 1; cas <= t; ++cas)
//  {
//  	cout << "Case #" << cas << ": ";
//  }
long long tn, a, b, c, d;

ll getCost(ll num, ll md, ll &ret) {
  ll cc = (num % md) * d;
  ll nn = num - (num % md);
  ll xx = a;
  if (md == 3)
    xx = b;
  if (md == 5)
    xx = c;
  if (xx < d * (nn - nn / md)) {
    cc += xx;
  } else {
    cc += d * (nn - nn / md);
  }
  ret = nn / md;
  return cc;
}

ll getCost2(ll num, ll md, ll &ret) {
  ll tt = (num % md);
  if (tt == 0) {
    ret = num;
    return 0;
  }
  ll cc = (md - tt) * d;
  ll nn = num + (md - tt);
  ll xx = a;
  if (md == 3)
    xx = b;
  if (md == 5)
    xx = c;
  if (xx < d * (nn - nn / md)) {
    cc += xx;
  } else {
    cc += d * (nn - nn / md);
  }
  ret = nn / md;
  return cc;
}

int main() {
  // freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> tn >> a >> b >> c >> d;

    queue<pair<ll, ll>> q;
    q.push(mp(tn, 0));

    long long ans = LONG_LONG_MAX;
    unordered_map<ll, ll> mmap;
    mmap[tn] = 0;

    while (!q.empty()) {
      pair<ll, ll> tmp = q.front();
      q.pop();

      ll num = tmp.first;
      ll cost = tmp.second;

      if (num == 0) {
        if (cost < ans)
          ans = cost;
        continue;
      }

      ll ret;
      ll cc = getCost(num, 2, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(mp(ret, cost + cc));
      }

      cc = getCost(num, 3, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(mp(ret, cost + cc));
      }

      cc = getCost(num, 5, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(mp(ret, cost + cc));
      }

      cc = getCost2(num, 2, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(mp(ret, cost + cc));
      }

      cc = getCost2(num, 3, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(mp(ret, cost + cc));
      }

      cc = getCost2(num, 5, ret);
      if (mmap.count(ret) == 0 || cost + cc < mmap[ret]) {
        mmap[ret] = cost + cc;
        q.push(mp(ret, cost + cc));
      }
    }

    cout << ans << endl;
  }
  return 0;
}