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

#include <cstdio>
#include <iostream>
#include <vector>
#define INF 999999999
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
vector<int> p[130];
vector<int> cost[130];

int DistanceCalc(int dis, int com) {
  int Index, totalcost;
  Index = totalcost = 0;

  /*
  rep(du,dis){
    // cout << "dis = " << dis << ", p[" <<com <<"]["<< Index <<"] = " <<
  p[com][Index] << endl; totalcost += cost[com][Index]; if(p[com].size() != 0 &&
  p[com][Index] == du){ Index++;
    }

  }
  return totalcost;
  */
  int remain = 0;
  rep(i, p[com].size()) {
    if (remain <= dis && dis <= p[com][i])
      return totalcost + cost[com][i] * (dis - remain);
    else
      totalcost += cost[com][i] * (p[com][i] - remain);
    remain = p[com][i];
  }
  if (p[com].size() != 0)
    return totalcost +
           cost[com][p[com].size()] * (dis - p[com][p[com].size() - 1]);
  else
    return cost[com][0] * dis;
}

int main() {
  int n, m, c, s, g, x, y, d, cc;
  int AllD[30][130][130];
  int D[130][130];

  while (cin >> n >> m >> c >> s >> g) {
    if (n + m + c + s + g == 0)
      break;
    for (int i = 0; i < 25; i++) {
      for (int j = 0; j < 130; j++) {
        p[j].clear();
        cost[j].clear();
        for (int k = 0; k < 130; k++) {
          AllD[i][j][k] = INF;
          D[j][k] = INF;
        }
      }
    }
    rep(i, m) {
      cin >> x >> y >> d >> cc;
      AllD[cc - 1][x - 1][y - 1] =
          min(d, AllD[cc - 1][x - 1][y - 1]); // hukusu taisaku
      AllD[cc - 1][y - 1][x - 1] = min(d, AllD[cc - 1][y - 1][x - 1]);
    }
    int pin[c + 1];
    rep(i, c) { cin >> pin[i]; }
    int pp;

    rep(j, c) { // MINA
      rep(i, pin[j] - 1) {
        cin >> pp;
        p[j].push_back(pp); //-1
      }
      rep(i, pin[j]) {
        cin >> pp;
        cost[j].push_back(pp);
      }
    } // MIKE

    rep(j, c) {
      rep(i, n) { AllD[j][i][i] = D[i][i] = 0; }
    }

    /*
    rep(i,c){
      cout << "会社 - " << i << endl;
      rep(j,n){
        rep(k,n){
          cout << AllD[i][j][k] << " ";
        }
        cout << endl;
      }
    }
    cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
    */

    /*
    rep(i,c){
      rep(j,n){
        rep(k,n){
          if(AllD[i][j][k] != INF){
            cout << "i = " << i << ", j = " << j << ", k = " << k << endl;
            cout << AllD[i][j][k] << endl;
            AllD[i][j][k] = DistanceCalc(AllD[i][j][k],i);
          }
        }
      }
    }
    */

    /*
    rep(i,c){
      rep(j,n){
        rep(k,n){
          rep(l,n){
            AllD[i][k][l] = min(AllD[i][k][l],AllD[i][k][j]+AllD[i][j][l]);
            D[k][l] = min(D[k][l],AllD[i][k][l]);
          }
        }
      }
    }
    */

    rep(i, c) {
      rep(j, n) {
        rep(k, n) {
          rep(l, n) {
            if (AllD[i][k][j] != INF && AllD[i][j][l] != INF && k != l) {
              AllD[i][k][l] = min(AllD[i][k][l], AllD[i][k][j] + AllD[i][j][l]);
              D[k][l] = min(D[k][l], DistanceCalc(AllD[i][k][l], i));
            }
          }
        }
      }
    }

    /*
 rep(i,n){
      rep(j,n)cout << D[i][j] << " ";
      cout << endl;
    }
 cout << "----------------" << endl;
    */

    rep(k, n) rep(i, n) rep(j, n) D[i][j] = min(D[i][j], D[i][k] + D[k][j]);

    /*
    rep(i,n){
      rep(j,n)cout << D[i][j] << " ";
      cout << endl;
    }
    */

    if (D[s - 1][g - 1] >= INF)
      cout << "-1" << endl;
    else
      cout << D[s - 1][g - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>
#define INF 1 << 15
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
vector<int> p[130];
vector<int> cost[130];

int DistanceCalc(int dis, int com) {
  int Index, totalcost;
  Index = totalcost = 0;

  rep(du, dis) {
    // cout << "dis = " << dis << ", p[" <<com <<"]["<< Index <<"] = " <<
    // p[com][Index] << endl;
    totalcost += cost[com][Index];
    if (p[com].size() != 0 && p[com][Index] == du) {
      Index++;
    }
  }
  // cout << "-------" << endl;
  //  cout << "taotal = " << totalcost << endl;
  return totalcost;
}

int main() {
  int n, m, c, s, g, x, y, d, cc;
  int AllD[30][130][130];
  int D[130][130];

  while (cin >> n >> m >> c >> s >> g) {
    if (n + m + c + s + g == 0)
      break;
    for (int i = 0; i < 25; i++) {
      for (int j = 0; j < 130; j++) {
        p[j].clear();
        cost[j].clear();
        for (int k = 0; k < 130; k++) {
          AllD[i][j][k] = INF;
          D[j][k] = INF;
        }
      }
    }
    rep(i, m) {
      cin >> x >> y >> d >> cc;
      AllD[cc - 1][x - 1][y - 1] = min(d, AllD[cc - 1][x - 1][y - 1]);
      AllD[cc - 1][y - 1][x - 1] = min(d, AllD[cc - 1][y - 1][x - 1]);
    }
    int pin[c + 1];
    rep(i, c) { cin >> pin[i]; }
    int pp;
    rep(j, c) { // MINA
      rep(i, pin[j] - 1) {
        cin >> pp;
        p[j].push_back(pp - 1);
      }
      rep(i, pin[j]) {
        cin >> pp;
        cost[j].push_back(pp);
      }
    } // MIKE

    rep(j, c) {
      rep(i, n) { AllD[j][i][i] = D[i][i] = 0; }
    }

    /*
    rep(i,c){
      cout << "会社 - " << i << endl;
      rep(j,n){
        rep(k,n){
          cout << AllD[i][j][k] << " ";
        }
        cout << endl;
      }
    }
    cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
    */

    /*
    rep(i,c){
      rep(j,n){
        rep(k,n){
          if(AllD[i][j][k] != INF){
            cout << "i = " << i << ", j = " << j << ", k = " << k << endl;
            cout << AllD[i][j][k] << endl;
            AllD[i][j][k] = DistanceCalc(AllD[i][j][k],i);
          }
        }
      }
    }
    */

    /*
    rep(i,c){
      rep(j,n){
        rep(k,n){
          rep(l,n){
            AllD[i][k][l] = min(AllD[i][k][l],AllD[i][k][j]+AllD[i][j][l]);
            D[k][l] = min(D[k][l],AllD[i][k][l]);
          }
        }
      }
    }
    */

    rep(i, c) {
      rep(j, n) {
        rep(k, n) {
          rep(l, n) {
            if (AllD[i][k][j] != INF && AllD[i][j][l] != INF) {
              AllD[i][k][l] = min(AllD[i][k][l], AllD[i][k][j] + AllD[i][j][l]);
              D[k][l] = min(D[k][l], DistanceCalc(AllD[i][k][l], i));
            }
          }
        }
      }
    }

    /*
 rep(i,n){
      rep(j,n)cout << D[i][j] << " ";
      cout << endl;
    }
 cout << "----------------" << endl;
    */

    rep(k, n) rep(i, n) rep(j, n) D[i][j] = min(D[i][j], D[i][k] + D[k][j]);

    /*
    rep(i,n){
      rep(j,n)cout << D[i][j] << " ";
      cout << endl;
    }
    */

    if (D[s - 1][g - 1] == INF)
      cout << "-1" << endl;
    else
      cout << D[s - 1][g - 1] << endl;
  }
  return 0;
}