Problem p00763 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  while (1) {

    int n, m, c, s, g;
    int a[110][110][30];
    fill(&a[0][0][0], &a[109][109][29], 1000000000);

    cin >> n >> m >> c >> s >> g;
    if (n == 0 && m == 0 && c == 0 && s == 0 && g == 0) {
      break;
    }
    for (int i = 0; i < m; i++) {
      int p, q, d, com;
      cin >> p >> q >> d >> com;
      a[p - 1][q - 1][com - 1] = min(d, a[p - 1][q - 1][com - 1]);
      a[q - 1][p - 1][com - 1] = a[p - 1][q - 1][com - 1];
    }
    int p[30] = {};
    for (int i = 0; i < c; i++) {
      cin >> p[i];
    }
    int coin[30][50][2] = {};
    for (int i = 0; i < c; i++) {
      for (int t = 0; t < p[i] - 1; t++) {
        cin >> coin[i][t][0];
      }
      for (int t = 0; t < p[i]; t++) {
        cin >> coin[i][t][1];
      }
    }

    for (int i = 0; i < c; i++) {
      for (int t = 0; t < n; t++) {
        for (int h = 0; h < n; h++) {
          for (int k = 0; k < n; k++) {
            if (a[h][t][i] != 1000000000 && a[t][k][i] != 1000000000) {
              a[h][k][i] = min(a[h][t][i] + a[t][k][i], a[h][k][i]);
            }
          }
        }
      }
    }

    /*
    cout<<endl;
    for(int i=0;i<c;i++){
        for(int t=0;t<n;t++){
            for(int h=0;h<n;h++){
                if(a[t][h][i]==1000000000){cout<<-1<<" ";}
                else{
                    int k=0;
                    if(a[t][h][i]>coin[i][p[i]-2][0]){
                        k=coin[i][p[i]-1][1]*(a[t][h][i]-coin[i][p[i]-2][0]);
                    }
                    for(int q=0;q<p[i]-1;q++){
                        int w=a[t][h][i];
                        if(w>coin[i][q][0]){w=coin[i][q][0];}
                        if(q!=0){w-=coin[i][q-1][0];}
                        if(w<0){break;}
                        k+=w*coin[i][q][1];
                    }

                    cout<<a[t][h][i]<<" ";}
            }
            cout<<endl;
        }
        cout<<endl;
    }
    */

    /*
        cout<<endl;
        for(int i=0;i<c;i++){
            for(int t=0;t<n;t++){
                for(int h=0;h<n;h++){
                    if(a[t][h][i]==1000000000){cout<<-1<<" ";}
                    else{
                        int k=0;
                        if(a[t][h][i]>coin[i][p[i]-2][0]){
                            k=coin[i][p[i]-1][1]*(a[t][h][i]-coin[i][p[i]-2][0]);
                        }
                        for(int q=0;q<p[i]-1;q++){
                            int w=a[t][h][i];
                            if(w>coin[i][q][0]){w=coin[i][q][0];}
                            if(q!=0){w-=coin[i][q-1][0];}
                            if(w<0){break;}
                            k+=w*coin[i][q][1];
                        }

                        cout<<k<<" ";}
                }
                cout<<endl;
            }
            cout<<endl;
        }
    */

    int dis[110][110];
    fill(&dis[0][0], &dis[109][109], 1000000000);

    for (int i = 0; i < c; i++) {
      for (int t = 0; t < n; t++) {
        for (int h = 0; h < n; h++) {
          if (a[t][h][i] != 1000000000) {
            int k = 0;
            if (a[t][h][i] > coin[i][p[i] - 2][0]) {
              k = coin[i][p[i] - 1][1] * (a[t][h][i] - coin[i][p[i] - 2][0]);
            }
            for (int q = 0; q < p[i] - 1; q++) {
              int w = a[t][h][i];
              if (w > coin[i][q][0]) {
                w = coin[i][q][0];
              }
              if (q != 0) {
                w -= coin[i][q - 1][0];
              }
              if (w < 0) {
                break;
              }
              k += w * coin[i][q][1];
            }
            dis[t][h] = min(k, dis[t][h]);
          }
        }
      }
    }

    /*
    for(int i=0;i<n;i++){
        for(int t=0;t<n;t++){
            if(dis[i][t]==1000000000){cout<<-1<<" ";}
            else{cout<<dis[i][t]<<" ";}
        }
            cout<<endl;
    }
    cout<<endl;
    */

    for (int i = 0; i < n; i++) {
      for (int t = 0; t < n; t++) {
        for (int h = 0; h < n; h++) {
          dis[t][h] = min(dis[t][h], dis[t][i] + dis[i][h]);
        }
      }
    }

    if (dis[s - 1][g - 1] == 1000000000) {
      cout << "-1" << endl;
    } else {
      cout << dis[s - 1][g - 1] << endl;
    }
  }

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, m, c, s, g;
  int a[110][110][30];
  fill(&a[0][0][0], &a[110][110][30], 1000000000);
  cin >> n >> m >> c >> s >> g;
  for (int i = 0; i < m; i++) {
    int p, q, d, com;
    cin >> p >> q >> d >> com;
    a[p][q][com] = d;
    a[q][p][com] = d;
  }
  int p[30] = {};
  for (int i = 1; i <= c; i++) {
    cin >> p[i];
  }
  int coin[30][50][2] = {};
  for (int i = 1; i <= c; i++) {
    for (int t = 0; t < p[i] - 1; t++) {
      cin >> coin[i][t][0];
    }
    for (int t = 0; t < p[i]; t++) {
      cin >> coin[i][t][1];
    }
  }

  for (int i = 0; i < c; i++) {
    for (int t = 0; t < n; t++) {
      for (int h = 0; h < n; h++) {
        for (int k = 0; k < n; k++) {
          if (a[h][t][i] != 1000000000 && a[t][k][i] != 1000000000) {
            a[h][k][i] = min(a[h][t][i] + a[t][k][i], a[h][k][i]);
          }
        }
      }
    }
  }

  int dis[110][110];
  fill(&dis[0][0], &dis[110][110], 1000000000);

  for (int i = 0; i < c; i++) {
    for (int t = 0; t < n; t++) {
      for (int h = 0; h < n; h++) {
        if (a[t][h][i] != 1000000000) {
          int k = 0;
          if (a[t][h][i] > coin[i][p[i] - 2][0]) {
            k = coin[i][p[i] - 1][1] * (a[t][h][i] - coin[i][p[i] - 2][0]);
          }
          for (int q = 0; q < p[i] - 1; q++) {
            int w = a[t][h][i];
            if (w > coin[i][q][0]) {
              w = coin[i][q][0];
            }
            if (q != 0) {
              w -= coin[i][q - 1][0];
            }
            if (w < 0) {
              break;
            }
            k += w * coin[i][q][1];
          }
          dis[t][h] = min(k, dis[t][h]);
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int t = 0; t < n; t++) {
      for (int h = 0; h < n; h++) {
        dis[t][h] = min(dis[t][h], dis[t][i] + dis[i][h]);
      }
    }
  }

  if (dis[s][g] == 1000000000) {
    cout << "-1" << endl;
  } else {
    cout << dis[s][g] << endl;
  }

  return 0;
}