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

#include <algorithm>
#include <cstdio>
#include <queue>
#include <set>
#include <vector>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define T_INF (1LL << 62)

void min_u(ll &m, ll v) {
  if (m > v)
    m = v;
}

struct q_t {
  ll t, s;
  int p;
  ll flags;
  ll trans_count;
  q_t(int n = 0) : t(T_INF), s(-1), p(-1), flags(0), trans_count(-1) {}
  bool operator<(const q_t &rhs) const {
    //		if(t!=rhs.t) return t>rhs.t;
    //		return s<rhs.s;
    return t > rhs.t;
  }
};

struct edge_t {
  int v;
  ll cs, ct;
};

bool min_qu(q_t &m, q_t &v) {
  if (m.t > v.t)
    m = v;
  return true;
}

priority_queue<q_t> que;

void push(q_t q) { que.push(q); }

int pop(q_t &q) {
  if (que.empty())
    return 0;
  q = que.top();
  que.pop();
  return 1;
}

int solve(vector<q_t> &ans, ll s, vector<vector<edge_t>> &e,
          vector<edge_t> &mt) {
  int n = ans.size() - 1;
  q_t q(n);
  int rest = n;
  vector<set<ll>> mb(n + 1);
  vector<vector<ll>> v(n + 1);
  ll amax = 0;
  for (auto &me : e)
    for (auto &mme : me)
      amax = max(amax, mme.cs);
  ll s_max = amax * (n - 1);
  for (auto &mv : v)
    mv.assign(s_max + 1, T_INF);

  {
    priority_queue<q_t> t;
    que = t;
  }

  q.t = 0;
  q.s = min(s, s_max);
  q.p = 1;
  q.trans_count = 0;
  q.flags |= 1 << 1;
  mb[1].insert(q.flags);

  push(q);
  while (pop(q)) {
    auto &mv = v[q.p];
    if (mv[q.s] <= q.t)
      continue;
    for (int i = q.s; i >= 0 && mv[i] > q.t; i--)
      mv[i] = q.t;

    if (ans[q.p].t == T_INF) {
      rest--;
    }
    min_qu(ans[q.p], q);
    if (rest <= 0)
      break;

    for (auto &me : e[q.p]) {
      q_t nq = q;
      nq.p = me.v;
      nq.t = q.t + me.ct;
      nq.s = q.s - me.cs;
      if (nq.s >= 0) {
        push(nq);
      }
    }
    {
      auto &mmt = mt[q.p];
      q_t nq = q;
      nq.t = q.t + mmt.ct;
      nq.s = min(q.s + mmt.cs, s_max);
      push(nq);
    }
  }
  return 1;
}

int main(void) {
  int n, m, s;

  while (scanf("%d%d%d", &n, &m, &s) == 3) {
    vector<q_t> ans(n + 1, q_t(n));
    vector<vector<edge_t>> e(n + 1);
    vector<edge_t> mt(n + 1);

    for (int i = 0; i < m; i++) {
      int u, v, a, b;
      edge_t t;

      scanf("%d%d%d%d", &u, &v, &a, &b);
      t.cs = a;
      t.ct = b;
      t.v = v;
      e[u].push_back(t);
      t.v = u;
      e[v].push_back(t);
    }

    for (int i = 1; i <= n; i++) {
      int a, b;
      edge_t &t = mt[i];

      scanf("%d%d", &a, &b);
      t.v = i;
      t.cs = a;
      t.ct = b;
    }

    solve(ans, s, e, mt);

    for (int i = 2; i <= n; i++) {
      //			printf("t=%lld  trans_count=%lld\\n", ans[i].t,
      //ans[i].trans_count);
      printf("%lld\\n", ans[i].t);
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define T_INF (1LL << 62)

void min_u(ll &m, ll v) {
  if (m > v)
    m = v;
}

struct q_t {
  ll t, s;
  int p;
  vi route;
  vector<int> flags;
  ll trans_count;
  q_t(int n) : t(T_INF), s(-1), p(-1), trans_count(-1) {
    flags.assign(n + 1, 0);
  }
  bool operator<(const q_t &rhs) const {
    //		if(t!=rhs.t) return t>rhs.t;
    //		return s<rhs.s;
    return t > rhs.t;
  }
};

struct edge_t {
  int v;
  ll cs, ct;
};

bool min_qu(q_t &m, q_t &v) {
  if (m.t > v.t)
    m = v;
  return true;
}

priority_queue<q_t> que;

void push(q_t q) { que.push(q); }

int pop(q_t &q) {
  if (que.empty())
    return 0;
  q = que.top();
  que.pop();
  return 1;
}

int solve(vector<q_t> &ans, ll s, vector<vector<edge_t>> &e,
          vector<edge_t> &mt) {
  int n = ans.size() - 1;
  q_t q(n);
  int rest = n;

  {
    priority_queue<q_t> t;
    que = t;
  }

  q.t = 0;
  q.s = s;
  q.p = 1;
  q.trans_count = 0;
  q.route.push_back(1);
  q.flags[1] = 1;

  push(q);
  while (pop(q)) {
    // printf("p=%d t=%lld s=%lld trans_count=%lld\\n", q.p, q.t, q.s,
    // q.trans_count);
    q_t nq = q;
    if (ans[q.p].t == T_INF) {
      rest--;
    }
    min_qu(ans[q.p], q);
    if (rest <= 0)
      break;

    for (auto &me : e[q.p]) {
      q_t nq = q;
      nq.p = me.v;
      nq.t = q.t + me.ct;
      nq.s = q.s - me.cs;
      nq.route.push_back(nq.p);
      nq.flags[nq.p] += 1;
      if (nq.s >= 0) {
        push(nq);
      } else {
        int lack = -nq.s;
        for (int i = 1; i <= n; i++) {
          if (q.flags[i] == 0)
            continue;
          int tc = (lack - 1 + mt[i].cs) / mt[i].cs;
          nq.trans_count = q.trans_count + tc;
          nq.t = q.t + me.ct + mt[i].ct * tc;
          nq.s = q.s - me.cs + mt[i].cs * tc;
          push(nq);
        }
      }
    }
  }
  return 1;
}

int main(void) {
  int n, m, s;

  while (scanf("%d%d%d", &n, &m, &s) == 3) {
    vector<q_t> ans(n + 1, q_t(n));
    vector<vector<edge_t>> e(n + 1);
    vector<edge_t> mt(n + 1);

    for (int i = 0; i < m; i++) {
      int u, v, a, b;
      edge_t t;

      scanf("%d%d%d%d", &u, &v, &a, &b);
      t.cs = a;
      t.ct = b;
      t.v = v;
      e[u].push_back(t);
      t.v = u;
      e[v].push_back(t);
    }

    for (int i = 1; i <= n; i++) {
      int a, b;
      edge_t &t = mt[i];

      scanf("%d%d", &a, &b);
      t.v = i;
      t.cs = a;
      t.ct = b;
    }

    solve(ans, s, e, mt);

    for (int i = 2; i <= n; i++) {
      //			printf("t=%lld  trans_count=%lld\\n", ans[i].t,
      //ans[i].trans_count);
      printf("%lld\\n", ans[i].t);
    }
  }
  return 0;
}