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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define ll long long
using namespace std;
typedef pair<int, int> P;
const int INF = 1e7;
int N, M, C, S, G;
int d[20][100][100];
int cost[100][100];
int p[20], q[20][50], r[20][50];
int rec(int c, int ds) {
  int res = 0;
  for (int i = 1; i < p[c]; i++) {
    if (ds <= q[c][i]) {
      res += (ds - q[c][i - 1]) * r[c][i - 1];
      return res;
    } else {
      res += (q[c][i] - q[c][i - 1]) * r[c][i - 1];
    }
  }
  res += (ds - q[c][p[c] - 1]) * r[c][p[c] - 1];
  return res;
}
int main() {
  while (1) {
    cin >> N >> M >> C >> S >> G;
    if (N + M + C + S + G == 0)
      break;
    S--;
    G--;
    REP(i, N) REP(j, N) REP(k, C) {
      if (i == j)
        d[k][i][j] = 0;
      else
        d[k][i][j] = INF;
    }
    REP(i, N) REP(j, N) cost[i][j] = INF;
    REP(i, M) {
      int x, y, z, c;
      cin >> x >> y >> z >> c;
      x--;
      y--;
      c--;
      d[c][x][y] = min(d[c][x][y], z);
      d[c][y][x] = d[c][x][y];
    }
    REP(i, C) cin >> p[i];
    REP(i, C) {
      q[i][0] = 0;
      REP(j, p[i] - 1) cin >> q[i][j + 1];
      REP(j, p[i]) cin >> r[i][j];
    }
    REP(c, C)
    REP(k, N) REP(i, N) REP(j, N) d[c][i][j] =
        min(d[c][i][j], d[c][i][k] + d[c][k][j]);

    REP(c, C) {
      REP(i, N) REP(j, N) { cost[i][j] = min(cost[i][j], rec(c, d[c][i][j])); }
    }

    /*REP(i,N){
            REP(j,N)cout<<cost[i][j]<<" ";cout<<endl;
    }*/

    REP(k, N)
    REP(i, N) REP(j, N) cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);

    if (cost[S][G] == INF)
      cout << -1 << endl;
    else
      cout << cost[S][G] << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define ll long long
using namespace std;
typedef pair<int, int> P;
const int INF = 1e7;
int N, M, C, S, G;
int d[20][100][100];
int cost[100][100];
int p[20], q[20][50], r[20][50];
int rec(int c, int ds) {
  int i = 0, res = 0;
  for (int x = 1; x <= ds; x++) {
    if (x <= q[c][i])
      res += r[c][i];
    else {
      if (i == p[c] - 1) {
        res += r[c][i];
      } else {
        i++;
        res += r[c][i];
      }
    }
  }
  return res;
}
int main() {
  while (1) {
    cin >> N >> M >> C >> S >> G;
    if (N + M + C + S + G == 0)
      break;
    S--;
    G--;
    REP(i, N) REP(j, N) REP(k, C) {
      if (i == j)
        d[k][i][j] = 0;
      else
        d[k][i][j] = INF;
    }
    REP(i, N) REP(j, N) cost[i][j] = INF;
    REP(i, M) {
      int x, y, z, c;
      cin >> x >> y >> z >> c;
      x--;
      y--;
      c--;
      d[c][x][y] = d[c][y][x] = z;
    }
    REP(i, C) cin >> p[i];
    REP(i, C) {
      REP(j, p[i] - 1) cin >> q[i][j];
      REP(j, p[i]) cin >> r[i][j];
    }
    REP(c, C)
    REP(k, N) REP(i, N) REP(j, N) d[c][i][j] =
        min(d[c][i][j], d[c][i][k] + d[c][k][j]);

    REP(c, C) {
      REP(i, N) REP(j, N) { cost[i][j] = min(cost[i][j], rec(c, d[c][i][j])); }
    }

    REP(k, N)
    REP(i, N) REP(j, N) cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);

    cout << cost[S][G] << endl;
  }
}