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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>
#define pii pair<int, pi>
#define X first
#define Y second
#define pb push_back
#define ab(x) ((x) < 0 ? (-(x)) : (x))
#define xx(x) ((x) * (x))
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define vpi vector<pi>
#define vpll vector<pll>
#define ALL(x) (x).begin(), (x).end()
#define Max (1 << 30)
#define LLMax (1ll << 60)
template <class T> string ToString(T t) {
  stringstream s;
  s << t;
  return s.str();
}
template <class T> void ToOther(T &t, string a) {
  stringstream s(a);
  s >> t;
}

int N, M, C, S, G;

struct node {
  int n, c, cost;
  node(int A, int B, int D) {
    n = A;
    c = B;
    cost = D;
  }
  node() {}
  bool operator<(const node &t) const { return cost > t.cost; }
};

int p[155];
int q[155];
int di[155];
int Cost[22][12222];
int dif[22];
int d[102][22];

int gg[22][105][105];

int Get(int c, int d) {
  if (d <= 10000)
    return Cost[c][d];
  return Cost[c][10000] + dif[c] * (d - 10000);
}

void make(int c) {

  for (int k = 1; k <= N; k++)
    for (int i = 1; i <= N; i++)
      for (int j = 1; j <= N; j++) {
        gg[c][i][j] = min(gg[c][i][k] + gg[c][k][j], gg[c][i][j]);
      }
}
int main() {
  //	freopen("output.txt","w",stdout);
  while (cin >> N >> M >> C >> S >> G, N) {
    memset(p, 0, sizeof(p));
    memset(dif, 0, sizeof(dif));
    memset(q, 0, sizeof(q));
    memset(dif, 0, sizeof(dif));
    memset(gg, 63, sizeof(gg));
    memset(d, 63, sizeof(d));
    int MMM = gg[0][0][0];
    memset(Cost, 0, sizeof(Cost));

    for (int i = 1; i <= N; i++)
      for (int k = 1; k <= C; k++)
        gg[k][i][i] = 0;
    while (M--) {
      int _S, _E, _D, _C;
      scanf("%d%d%d%d", &_S, &_E, &_D, &_C);
      gg[_C][_S][_E] = min(gg[_C][_S][_E], _D);
      gg[_C][_E][_S] = min(gg[_C][_E][_S], _D);
    }
    for (int i = 1; i <= C; i++)
      scanf("%d", &p[i]);

    for (int i = 1; i <= C; i++) {
      Cost[i][0] = 0;
      for (int k = 1; k < p[i]; k++)
        scanf("%d", &q[k]);
      for (int k = 0; k < p[i]; k++)
        scanf("%d", &di[k]);
      q[p[i]] = Max;
      int pos = 0;
      int now = 0;
      dif[i] = di[p[i] - 1];
      for (int k = 1; k <= 10000; k++) {
        now += di[pos];
        Cost[i][k] = now;
        if (q[pos + 1] == k)
          pos++;
      }
    }
    for (int i = 1; i <= C; i++)
      make(i);
    priority_queue<node> q;

    q.push(node(S, 1, 0));
    d[S][1] = 0;
    //	int n,c,dis,cost;
    // 	int e,d,c;
    int r = Max;

    while (q.size()) {
      int k = q.top().n;
      int c = q.top().c;
      int cost = q.top().cost;

      q.pop();
      if (k == G)
        r = min(r, cost);

      if (d[k][c] < cost)
        continue;

      for (int i = 1; i <= N; i++)
        for (int nc = 1; nc <= C; nc++)
          if (gg[nc][k][i] != MMM) {
            if (i == k)
              continue;
            int t = i;
            int w = gg[nc][k][i];
            ;
            int nxt_cost = cost;

            nxt_cost += Get(nc, w);

#define T1 t][nc

            if (d[T1] > nxt_cost) {
              d[T1] = nxt_cost;
              q.push(node(t, nc, nxt_cost));
            }
          }
    }
    if (r == Max)
      r = -1;
    cout << r << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>
#define pii pair<int, pi>
#define X first
#define Y second
#define pb push_back
#define ab(x) ((x) < 0 ? (-(x)) : (x))
#define xx(x) ((x) * (x))
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define vpi vector<pi>
#define vpll vector<pll>
#define ALL(x) (x).begin(), (x).end()
#define Max (1 << 30)
#define LLMax (1ll << 60)
template <class T> string ToString(T t) {
  stringstream s;
  s << t;
  return s.str();
}
template <class T> void ToOther(T &t, string a) {
  stringstream s(a);
  s >> t;
}

int N, M, C, S, G;

struct data {
  int e, d, c;
  data(int A, int B, int C) {
    e = A;
    d = B;
    c = C;
  }
  data() {}
};

struct node {
  int n, c, dis, cost;
  node(int A, int B, int C, int D) {
    n = A;
    c = B;
    dis = C;
    cost = D;
  }
  node() {}
  bool operator<(const node &t) const { return cost > t.cost; }
};

vector<data> v[155];
int p[155];
int q[155];
int di[155];
int Cost[22][12222];
int dif[22];
int d[102][22][23333];
int ck[102][22][23333];
int vis[102][22][23333];
int VIS;
int CK;

int Get(int c, int d) {
  if (d <= 10000)
    return Cost[c][d];
  return Cost[c][10000] + dif[c] * (d - 10000);
}
int main() {
  //	freopen("output.txt","w",stdout);
  while (cin >> N >> M >> C >> S >> G, N) {
    VIS++;
    CK++;
    memset(Cost, -1, sizeof(Cost));
    for (int i = 0; i < 155; i++)
      v[i].clear();

    while (M--) {
      int _S, _E, _D, _C;
      scanf("%d%d%d%d", &_S, &_E, &_D, &_C);
      v[_S].pb(data(_E, _D, _C));
      v[_E].pb(data(_S, _D, _C));
    }
    for (int i = 1; i <= C; i++)
      scanf("%d", &p[i]);

    for (int i = 1; i <= C; i++) {
      Cost[i][0] = 0;
      for (int k = 1; k < p[i]; k++)
        scanf("%d", &q[k]);
      for (int k = 0; k < p[i]; k++)
        scanf("%d", &di[k]);
      q[p[i]] = Max;
      int pos = 0;
      int now = 0;
      dif[i] = di[p[i] - 1];
      for (int k = 1; k <= 10000; k++) {
        now += di[pos];
        Cost[i][k] = now;
        if (q[pos + 1] == k)
          pos++;
      }
    }

    priority_queue<node> q;
    q.push(node(S, 1, 0, 0));

    //	int n,c,dis,cost;
    // 	int e,d,c;
    int r = Max;

    while (q.size()) {
      int k = q.top().n;
      int c = q.top().c;
      int dis = q.top().dis;
      int cost = q.top().cost;

      q.pop();
      if (k == G)
        r = min(r, cost);

      if (vis[k][c][dis] == VIS)
        continue;
      vis[k][c][dis] = VIS;
      // if(d[k][c][dis]<cost)continue;

      for (int i = 0; i < v[k].size(); i++) {
        int t = v[k][i].e;
        int w = v[k][i].d;
        int nc = v[k][i].c;

        int nxt_dis = 0;
        int nxt_cost = cost;
        if (c == nc) {
          nxt_dis = dis + w;
          nxt_cost += Get(c, dis + w) - Get(c, dis);
        } else {
          nxt_dis = w;
          nxt_cost += Get(nc, w);
        }

#define T1 t][nc][nxt_dis
        if (nxt_dis > 23000)
          continue;
        if (vis[T1] == VIS)
          continue;

        if (ck[T1] != CK || d[T1] > nxt_cost) {
          ck[T1] = CK;
          d[T1] = nxt_cost;
          q.push(node(t, nc, nxt_dis, nxt_cost));
        }
      }
    }
    if (r == Max)
      r = -1;
    cout << r << endl;
  }
}