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

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

#define INF INT_MAX >> 1

using namespace std;

int main(void) {
  while (1) {
    int n, m, c, s, g;
    cin >> n >> m >> c >> s >> g;
    if (!n && !m && !c && !s && !g)
      break;
    s--;
    g--;

    static int dist[100][100][20];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < c; k++) {
          if (i == j)
            dist[i][j][k] = 0;
          else
            dist[i][j][k] = INF;
        }
      }
    }
    for (int i = 0; i < m; i++) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      x--;
      y--;
      c--;
      dist[x][y][c] = dist[y][x][c] = min(dist[x][y][c], d);
    }

    int p[20]; // int len[20];
    for (int i = 0; i < c; i++) {
      cin >> p[i]; // scanf("%d",len+i);
    }
    vector<pair<int, int>> poly[20];
    for (int i = 0; i < c; i++) {
      poly[i].assign(p[i] + 1, make_pair(0, 1));
      for (int j = 0; j < p[i] - 1; j++) {
        int q;
        cin >> q;
        poly[i][j + 1].first = q;
      }
      for (int j = 0; j < p[i]; j++) {
        int r;
        cin >> r;
        poly[i][j + 1].second = r;
      }
      poly[i].back().first = INF; //????
    }

    for (int l = 0; l < c; l++) {
      // ワーシャルフロイド
      for (int k = 0; k < n; k++)
        for (int i = 0; i < n; i++)
          for (int j = 0; j < n; j++) {
            dist[i][j][l] = min(dist[i][j][l], dist[i][k][l] + dist[k][j][l]);
          }
    }

    static int cost[100][100][20];
    for (int l = 0; l < c; l++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          int d = dist[i][j][l];
          if (d == INF) {
            cost[i][j][l] = INF; //????
            continue;
          }
          int tmp = 0;
          for (int k = 0; k < poly[l].size() - 1; k++) {
            tmp += poly[l][k + 1].second *
                   max(min(poly[l][k + 1].first, d) - poly[l][k].first, 0);
          }
          cost[i][j][l] = tmp;
        }
      }
    }

    int ans[100][100];
    fill(ans[0], ans[100], INF);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < c; k++)
          ans[i][j] = min(ans[i][j], cost[i][j][k]);
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          ans[i][j] = min(ans[i][j], ans[i][k] + ans[k][j]);

    if (ans[s][g] != INF)
      cout << ans[s][g] << endl;
    else
      cout << -1 << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

#define INF INT_MAX >> 1

using namespace std;

int main(void) {
  while (1) {
    int n, m, c, s, g;
    cin >> n >> m >> c >> s >> g;
    if (!n && !m && !c && !s && !g)
      break;
    s--;
    g--;

    static int dist[100][100][20];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < c; k++) {
          if (i == j)
            dist[i][j][k] = 0;
          else
            dist[i][j][k] = INF;
        }
      }
    }
    for (int i = 0; i < m; i++) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      x--;
      y--;
      c--;
      dist[x][y][c] = dist[y][x][c] = min(dist[x][y][c], d);
    }

    int p[20]; // int len[20];
    for (int i = 0; i < c; i++) {
      cin >> p[i]; // scanf("%d",len+i);
    }
    vector<pair<int, int>> poly[20];
    for (int i = 0; i < c; i++) {
      poly[i].assign(p[i] + 1, make_pair(0, 1));
      for (int j = 0; j < p[i] - 1; j++) {
        int q;
        cin >> q;
        poly[i][j + 1].first = q;
      }
      for (int j = 0; j < p[i]; j++) {
        int r;
        cin >> r;
        poly[i][j + 1].second = r;
      }
      poly[i].back().first = INF; //????
    }

    for (int l = 0; l < c; l++) {
      // ワーシャルフロイド
      for (int k = 0; k < n; k++)
        for (int i = 0; i < n; i++)
          for (int j = 0; j < n; j++) {
            dist[i][j][l] = min(dist[i][j][l], dist[i][k][l] + dist[k][j][l]);
            cout << dist[i][j][l] << endl;
          }
    }

    static int cost[100][100][20];
    for (int l = 0; l < c; l++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          int d = dist[i][j][l];
          if (d == INF) {
            cost[i][j][l] = INF; //????
            continue;
          }
          int tmp = 0;
          for (int k = 0; k < poly[l].size() - 1; k++) {
            tmp += poly[l][k + 1].second *
                   max(min(poly[l][k + 1].first, d) - poly[l][k].first, 0);
          }
          cost[i][j][l] = tmp;
          // cout << cost[i][j][l] << endl;
        }
      }
    }

    int ans[100][100];
    fill(ans[0], ans[100], INF);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < c; k++) {
          ans[i][j] = min(ans[i][j], cost[i][j][k]);
        }
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          ans[i][j] = min(ans[i][j], ans[i][k] + ans[k][j]);

    if (ans[s][g] != INF)
      cout << ans[s][g] << endl;
    else
      cout << -1 << endl;
  }

  return 0;
}