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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info() { station = total_cost = 0; }
  Info(int arg_station, int arg_total_cost) {
    station = arg_station;
    total_cost = arg_total_cost;
  }
  bool operator<(const struct Info &arg) const {
    return total_cost > arg.total_cost;
  };

  int station, total_cost;
};

int N, M, company, start, goal, num_oresen[25], loc[52], value[52],
    cost_table[20002];
int ***cost;
int final_cost[101][101];

void func() {

  if (M == 0) {
    for (int i = 1; i <= company; i++)
      scanf("%d", &num_oresen[i]);
    for (int com = 1; com <= company; com++) {
      for (int i = 1; i <= num_oresen[com] - 1; i++)
        scanf("%d", &loc[i]);
      for (int i = 1; i <= num_oresen[com]; i++)
        scanf("%d", &value[i]);
    }

    if (start != goal)
      printf("-1\\n");
    else {
      printf("0\\n");
    }
    return;
  }

  for (int i = 1; i <= company; i++) {
    for (int k = 1; k <= N; k++) {
      for (int p = 1; p <= N; p++) {
        if (k != p)
          cost[i][k][p] = BIG_NUM;
        else {
          cost[i][k][p] = 0;
        }
      }
    }
  }

  int from, to, dist, tmp_com;

  for (int i = 0; i < M; i++) {
    scanf("%d %d %d %d", &from, &to, &dist, &tmp_com);
    cost[tmp_com][from][to] = min(cost[tmp_com][from][to], dist);
    cost[tmp_com][to][from] = min(cost[tmp_com][to][from], dist);
  }

  for (int com = 1; com <= company; com++) {
    for (int mid = 1; mid <= N; mid++) {
      for (int st = 1; st <= N; st++) {
        if (cost[com][st][mid] == BIG_NUM)
          continue;
        for (int gl = 1; gl <= N; gl++) {
          if (cost[com][mid][gl] == BIG_NUM)
            continue;
          cost[com][st][gl] =
              min(cost[com][st][gl], cost[com][st][mid] + cost[com][mid][gl]);
        }
      }
    }
  }

  for (int i = 1; i <= company; i++) {
    scanf("%d", &num_oresen[i]);
  }

  int index;

  for (int com = 1; com <= company; com++) {
    for (int i = 1; i <= num_oresen[com] - 1; i++)
      scanf("%d", &loc[i]);
    for (int i = 1; i <= num_oresen[com]; i++)
      scanf("%d", &value[i]);

    index = 0;
    for (int dist = 0; dist <= 200; dist++)
      cost_table[dist] = 0;

    for (int dist = 1; dist <= 20000; dist++) {
      if (dist <= loc[index]) {
        cost_table[dist] = cost_table[dist - 1] + value[index];
      } else {
        index = min(index + 1, num_oresen[com]);
        cost_table[dist] = cost_table[dist - 1] + value[index];
      }
    }

    for (int i = 1; i <= N; i++) {
      for (int k = 1; k <= N; k++) {
        if (i != k && cost[com][i][k] != BIG_NUM) {
          cost[com][i][k] = cost_table[cost[com][i][k]];
        }
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int k = 1; k <= N; k++) {
      if (i != k)
        final_cost[i][k] = BIG_NUM;
      else {
        final_cost[i][k] = 0;
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int k = 1; k <= N; k++) {
      for (int com = 1; com <= company; com++) {
        final_cost[i][k] = min(final_cost[i][k], cost[com][i][k]);
      }
    }
  }

  for (int mid = 1; mid <= N; mid++) {
    for (int st = 1; st <= N; st++) {
      if (final_cost[st][mid] == BIG_NUM)
        continue;
      for (int gl = 1; gl <= N; gl++) {
        if (final_cost[mid][gl] == BIG_NUM)
          continue;
        final_cost[st][gl] =
            min(final_cost[st][gl], final_cost[st][mid] + final_cost[mid][gl]);
      }
    }
  }

  if (final_cost[start][goal] != BIG_NUM)
    printf("%d\\n", final_cost[start][goal]);
  else {
    printf("-1\\n");
  }
}

int main() {

  cost = new int **[21];
  for (int i = 1; i <= 20; i++) {
    cost[i] = new int *[101];
    for (int k = 1; k <= 100; k++)
      cost[i][k] = new int[101];
  }

  while (true) {
    scanf("%d %d %d %d %d", &N, &M, &company, &start, &goal);
    if (N == 0 && M == 0 && company == 0 && start == 0 && goal == 0)
      break;

    func();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info() { station = total_cost = 0; }
  Info(int arg_station, int arg_total_cost) {
    station = arg_station;
    total_cost = arg_total_cost;
  }
  bool operator<(const struct Info &arg) const {
    return total_cost > arg.total_cost;
  };

  int station, total_cost;
};

int N, M, company, start, goal, num_oresen[25], loc[52], value[52],
    cost_table[205];
int ***cost;

void func() {

  if (M == 0) {
    for (int i = 1; i <= company; i++)
      scanf("%d", &num_oresen[i]);
    for (int com = 1; com <= company; com++) {
      for (int i = 1; i <= num_oresen[com] - 1; i++)
        scanf("%d", &loc[i]);
      for (int i = 1; i <= num_oresen[com]; i++)
        scanf("%d", &value[i]);
    }

    if (start != goal)
      printf("-1\\n");
    else {
      printf("0\\n");
    }
    return;
  }

  for (int i = 1; i <= company; i++) {
    for (int k = 1; k <= N; k++) {
      for (int p = 1; p <= N; p++) {
        if (k != p)
          cost[i][k][p] = BIG_NUM;
        else {
          cost[i][k][p] = 0;
        }
      }
    }
  }

  int from, to, dist, tmp_com;

  for (int i = 0; i < M; i++) {
    scanf("%d %d %d %d", &from, &to, &dist, &tmp_com);
    cost[tmp_com][from][to] = min(cost[tmp_com][from][to], dist);
    cost[tmp_com][to][from] = min(cost[tmp_com][to][from], dist);
  }

  //???????????£???????????????<??????????????°?????´???:?????§2000??????>
  for (int com = 1; com <= company; com++) {
    for (int mid = 1; mid <= N; mid++) {
      for (int st = 1; st <= N; st++) {
        if (cost[com][st][mid] == BIG_NUM)
          continue;
        for (int gl = 1; gl <= N; gl++) {
          if (cost[com][mid][gl] == BIG_NUM)
            continue;
          cost[com][st][gl] =
              min(cost[com][st][gl], cost[com][st][mid] + cost[com][mid][gl]);
        }
      }
    }
  }

  for (int i = 1; i <= company; i++) {
    scanf("%d", &num_oresen[i]);
  }

  int index;

  for (int com = 1; com <= company; com++) {
    for (int i = 1; i <= num_oresen[com] - 1; i++)
      scanf("%d", &loc[i]);
    for (int i = 1; i <= num_oresen[com]; i++)
      scanf("%d", &value[i]);

    //?????????????????????????????????<??£???>
    index = 0;
    for (int dist = 0; dist <= 200; dist++)
      cost_table[dist] = 0;

    for (int dist = 1; dist <= 200; dist++) {
      if (dist <= loc[index]) {
        cost_table[dist] = cost_table[dist - 1] + value[index];
      } else {
        index = min(index + 1, num_oresen[com]);
        cost_table[dist] = cost_table[dist - 1] + value[index];
      }
    }

    // cost????????????????????¢????????????????????´??????
    for (int i = 1; i <= N; i++) {
      for (int k = 1; k <= N; k++) {
        if (cost[com][i][k] != BIG_NUM) {
          cost[com][i][k] = cost_table[cost[com][i][k]];
        }
      }
    }
  }

  int min_Cost[N + 1];
  for (int i = 1; i <= N; i++)
    min_Cost[i] = BIG_NUM;
  min_Cost[start] = 0;

  priority_queue<Info> Q;

  Info first;
  first.station = start;
  first.total_cost = 0;

  Q.push(first);

  int min_index;

  while (!Q.empty()) {

    if (Q.top().total_cost > min_Cost[Q.top().station]) {
      Q.pop();
    } else {
      min_index = Q.top().station;
      Q.pop();

      for (int i = 1; i <= N; i++) {
        for (int com = 1; com <= company; com++) {
          if (cost[com][min_index][i] == BIG_NUM)
            continue;
          if (min_Cost[i] > min_Cost[min_index] + cost[com][min_index][i]) {
            min_Cost[i] = min_Cost[min_index] + cost[com][min_index][i];
            Q.push(Info(i, min_Cost[i]));
          }
        }
      }
    }
  }

  if (min_Cost[goal] != BIG_NUM)
    printf("%d\\n", min_Cost[goal]);
  else {
    printf("-1\\n");
  }
}

int main() {

  cost = new int **[21];
  for (int i = 1; i <= 20; i++) {
    cost[i] = new int *[101];
    for (int k = 1; k <= 100; k++)
      cost[i][k] = new int[101];
  }

  while (true) {
    scanf("%d %d %d %d %d", &N, &M, &company, &start, &goal);
    if (N == 0 && M == 0 && company == 0 && start == 0 && goal == 0)
      break;

    func();
  }

  return 0;
}