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

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int N, M, C, S, G;

struct Edge {
  Edge() {}
  Edge(int t, int d, int c) : to(t), dist(d), company(c) {}
  int to, dist, company;
};

struct State {
  State() {}
  State(int v_, LL p, int d, int c) : v(v_), dist(d), company(c) { price = p; }
  int v, dist, company;
  LL price;

  bool operator>(const State &s) const { return price > s.price; }
};

vector<vector<Edge>> es;
VI ps;
VVI qs, rs;
VVLL sum_ps;

VLL calc_sum_ps_one(int c) {
  VLL sum;
  sum.PB(0);

  int d = 1;
  for (int i = 0; i < ps[c] - 1; i++) {
    while (d <= qs[c][i]) {
      sum.PB(sum.back() + (LL)rs[c][i]);
      ++d;
    }
  }

  return sum;
}

LL calc_price(int d, int c) {
  LL res;
  if (d < (int)sum_ps[c].size()) {
    res = sum_ps[c][d];
  } else {
    res = (LL)sum_ps[c].back() +
          ((LL)(d - sum_ps[c].size() + 1)) * (LL)(rs[c].back());
  }
  return res < 0 ? INFL : res;
}

LL solve() {
  // ?´???????????±?????????????
  sum_ps.clear();
  sum_ps.resize(C + 1);
  for (int i = 1; i <= C; i++) {
    sum_ps[i] = calc_sum_ps_one(i);
  }

  // ????????????????????????

  // dists[v][c][d] = min_price;
  const int MAX_DIST = 210;
  vector<VVLL> dists(N, VVLL(C + 1, VLL(MAX_DIST + 1, INFL)));
  dists[S][0][0] = 0;

  priority_queue<State, vector<State>, greater<State>> q;
  q.push(State(S, 0, 0, 0));

  while (q.size()) {
    State cur = q.top();
    q.pop();

    // ????°?????????????
    if (dists[cur.v][cur.company][min(cur.dist, MAX_DIST)] < cur.price)
      continue;

    for (auto &e : es[cur.v]) {
      State next;
      next.v = e.to;
      next.company = e.company;

      if (next.company != cur.company) {
        next.dist = e.dist;
        next.price = min(cur.price + calc_price(next.dist, next.company), INFL);
      } else {
        next.dist = cur.dist + e.dist;
        next.price = min(cur.price + calc_price(next.dist, next.company) -
                             calc_price(cur.dist, cur.company),
                         INFL);
      }

      if (dists[next.v][next.company][min(next.dist, MAX_DIST)] > next.price) {
        dists[next.v][next.company][min(next.dist, MAX_DIST)] = next.price;
        q.push(next);
      }
    }
  }

  LL ans = INFL;
  for (int c = 0; c <= C; c++) {
    for (int d = 0; d <= MAX_DIST; d++) {
      ans = min(ans, dists[G][c][d]);
    }
  }

  return ans == INFL ? -1 : ans;
}

int main(void) {
  while (cin >> N >> M >> C >> S >> G, N) {
    --S;
    --G;

    es.clear();
    es.resize(N);
    for (int i = 0; i < M; i++) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      --x;
      --y;
      es[x].EB(y, d, c);
      es[y].EB(x, d, c);
    }

    ps.clear();
    ps.resize(C + 1);
    for (int i = 1; i <= C; i++) {
      cin >> ps[i];
    }

    qs.clear();
    qs.resize(C + 1);
    rs.clear();
    rs.resize(C + 1);
    for (int i = 1; i <= C; i++) {
      qs[i].clear();
      qs[i].resize(ps[i] - 1);
      for (auto &q : qs[i])
        cin >> q;

      rs[i].clear();
      rs[i].resize(ps[i]);
      for (auto &r : rs[i])
        cin >> r;
    }

    cout << solve() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int N, M, C, S, G;

struct Edge {
  Edge() {}
  Edge(int t, int d, int c) : to(t), dist(d), company(c) {}
  int to, dist, company;
};

struct State {
  State() {}
  State(int v_, int p, int d, int c) : v(v_), price(p), dist(d), company(c) {}
  int v, price, dist, company;

  bool operator>(const State &s) const { return price < s.price; }
};

// ????¬?????´???????
template <typename T> vector<T> calc_csum(vector<T> &val) {
  vector<T> sum(val.size() + 1);
  partial_sum(val.begin(), val.end(), sum.begin() + 1);
  return sum;
}

vector<vector<Edge>> es;
VI ps;
VVI qs, rs;

VVI sum_ps;

VI calc_sum_ps_one(int c) {
  VI sum;
  sum.PB(0);

  int d = 1;
  for (int i = 0; i < ps[c] - 1; i++) {
    while (d <= qs[c][i]) {
      sum.PB(sum.back() + rs[c][i]);
      ++d;
    }
  }

  return sum;
}

int calc_price(int d, int c) {
  if (d <= (int)sum_ps[c].size()) {
    return sum_ps[c][d];
  } else {
    return sum_ps[c].back() + (d - sum_ps[c].size() + 1) * rs[c].back();
  }
}

int solve() {
  // ?´???????????±?????????????
  sum_ps.clear();
  sum_ps.resize(C + 1);
  for (int i = 1; i <= C; i++) {
    sum_ps[i] = calc_sum_ps_one(i);
  }

  // ????????????????????????

  // dists[v][c][d] = min_price;
  const int MAX_DIST = 200;
  VVVI dists(N, VVI(C + 1, VI(MAX_DIST + 1, INF)));
  dists[S][0][0] = 0;

  priority_queue<State, vector<State>, greater<State>> q;
  q.push(State(S, 0, 0, 0));

  while (q.size()) {
    State cur = q.top();
    q.pop();

    // ????°?????????????
    if (dists[cur.v][cur.company][min(cur.dist, MAX_DIST)] < cur.price)
      continue;

    for (auto &e : es[cur.v]) {
      State next;
      next.v = e.to;
      next.company = e.company;

      if (next.company != cur.company) {
        next.dist = e.dist;
        next.price = cur.price + calc_price(next.dist, next.company);
      } else {
        next.dist = cur.dist + e.dist;
        next.price = cur.price + calc_price(next.dist, next.company) -
                     calc_price(cur.dist, cur.company);
      }

      if (dists[next.v][next.company][min(next.dist, MAX_DIST)] > next.price) {
        dists[next.v][next.company][min(next.dist, MAX_DIST)] = next.price;
        q.push(next);
      }
    }
  }

  int ans = INF;
  for (int c = 0; c <= C; c++) {
    for (int d = 0; d <= MAX_DIST; d++) {
      int t = dists[G][c][d];
      ans = min(ans, t);
    }
  }

  return ans == INF ? -1 : ans;
}

int main(void) {
  while (cin >> N >> M >> C >> S >> G, N) {
    --S;
    --G;

    es.clear();
    es.resize(N);
    for (int i = 0; i < M; i++) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      --x;
      --y;
      es[x].EB(y, d, c);
      es[y].EB(x, d, c);
    }

    ps.clear();
    ps.resize(C + 1);
    for (int i = 1; i <= C; i++) {
      cin >> ps[i];
    }

    qs.clear();
    qs.resize(C + 1);
    rs.clear();
    rs.resize(C + 1);
    for (int i = 1; i <= C; i++) {
      qs[i].clear();
      qs[i].resize(ps[i] - 1);
      for (auto &q : qs[i])
        cin >> q;

      rs[i].clear();
      rs[i].resize(ps[i]);
      for (auto &r : rs[i])
        cin >> r;
    }

    cout << solve() << endl;
  }

  return 0;
}