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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
const ll INF = (ll)1e18 + 1;
ll a, b, c, d;

ll solve(ll n) {
  map<ll, ll> mp;
  priority_queue<pair<ll, ll>> q;
  q.push({n, 0});
  while (q.size()) {
    auto p = q.top();
    q.pop();
    if (mp[p.first] < 0) {
      cout << p.first << " " << p.second << " " << mp[p.first] << endl;
      break;
    }

    for (int x : {2, 3, 5}) {
      ll cost = 0;
      if (x == 2)
        cost = a;
      else if (x == 3)
        cost = b;
      else
        cost = c;

      ll m = p.first;
      ll y = mp[m];
      pair<ll, ll> np1, np2;
      if (m == 1) {
        if (mp[0] == 0) {
          q.push({0, y + d});
          mp[0] = y + d;
        }
        mp[0] = min(mp[0], y + d);
        continue;
      }

      np1.first = m / x;
      np1.second = y + (m % x) * d + cost;
      if ((np1.second - y) / d > abs(m - np1.first))
        np1.second = min(np1.second, y + abs(m - np1.first) * d);
      // cout << "np1:" << np1.first << " " << np1.second << endl;
      if (mp[np1.first] == 0) {
        q.push(np1);
        mp[np1.first] = np1.second;
      }
      mp[np1.first] = min(mp[np1.first], np1.second);

      if (m % x != 0) {
        np2.first = m / x + 1;
        np2.second = y + (x - m % x) * d + cost;
        if ((np2.second - y) / d > abs(m - np2.first))
          np2.second = min(np2.second, y + abs(np2.first - m) * d);
        // cout << "np2:" << np2.first << " " << np2.second << endl;
        if (mp[np2.first] == 0) {
          q.push(np2);
          mp[np2.first] = np2.second;
        }
        mp[np2.first] = min(mp[np2.first], np2.second);
      }
    }
  }

  // for(auto p:mp){
  //     cout << p.first << " " << p.second << endl;
  // }
  return mp[0];
}

int main() {
  int t;
  cin >> t;
  ll n;
  vector<vector<ll>> vp;
  rep(i, t) {
    vector<ll> v(5);
    cin >> v.at(0) >> v.at(1) >> v.at(2) >> v.at(3) >> v.at(4);
    vp.push_back(v);
  }

  rep(i, t) {
    n = vp.at(i).at(0);
    a = vp.at(i).at(1);
    b = vp.at(i).at(2);
    c = vp.at(i).at(3);
    d = vp.at(i).at(4);
    ll res = solve(n);
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
const ll INF = (ll)1e18 + 1;
ll a, b, c, d;

ll solve(ll n) {
  map<ll, ll> mp;
  priority_queue<pair<ll, ll>> q;
  q.push({n, 0});
  while (q.size()) {
    auto p = q.top();
    q.pop();
    if (mp[p.first] < 0) {
      cout << p.first << " " << p.second << " " << mp[p.first] << endl;
      break;
    }

    for (int x : {2, 3, 5}) {
      ll cost = 0;
      if (x == 2)
        cost = a;
      else if (x == 3)
        cost = b;
      else
        cost = c;

      ll m = p.first;
      ll y = mp[m];
      pair<ll, ll> np1, np2;
      if (m == 1) {
        if (mp[0] == 0) {
          q.push({0, y + d});
          mp[0] = y + d;
        }
        mp[0] = min(mp[0], y + d);
        continue;
      }
      np1.first = m / x;
      np1.second = y + (m % x) * d + cost;
      if ((np1.second - y) / d > abs(m - np1.first))
        np1.second = min(np1.second, y + abs(m - np1.first) * d);
      // cout << "np1:" << np1.first << " " << np1.second << endl;
      if (mp[np1.first] == 0) {
        q.push(np1);
        mp[np1.first] = np1.second;
      }
      mp[np1.first] = min(mp[np1.first], np1.second);

      np2.first = m / x + 1;
      np2.second = y + (x - m % x) * d + cost;
      if ((np2.second - y) / d > abs(m - np2.first))
        np2.second = min(np2.second, y + abs(np2.first - m) * d);
      // cout << "np2:" << np2.first << " " << np2.second << endl;
      if (mp[np2.first] == 0) {
        q.push(np2);
        mp[np2.first] = np2.second;
      }
      mp[np2.first] = min(mp[np2.first], np2.second);
    }
  }

  // for(auto p:mp){
  //     cout << p.first << " " << p.second << endl;
  // }
  return mp[0];
}

int main() {
  int t;
  cin >> t;
  ll n;
  vector<vector<ll>> vp;
  rep(i, t) {
    vector<ll> v(5);
    cin >> v.at(0) >> v.at(1) >> v.at(2) >> v.at(3) >> v.at(4);
    vp.push_back(v);
  }

  rep(i, t) {
    n = vp.at(i).at(0);
    a = vp.at(i).at(1);
    b = vp.at(i).at(2);
    c = vp.at(i).at(3);
    d = vp.at(i).at(4);
    ll res = solve(n);
    cout << res << endl;
  }

  return 0;
}