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

#define INF 10000000
#define BIAS 24 * 60
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char gCity[2001][64];
int gCityNum = 0;
int from_hakodate[2001][101];
int to_hakodate[2001][101];
int from_tokyo[2001][101];
int to_tokyo[2001][101];

struct Train {
  int from, to;
  int dpt, arv;
  int fare;
};

int max(int lhs, int rhs) {
  if (lhs > rhs)
    return lhs;
  else
    return rhs;
}

int min(int lhs, int rhs) {
  if (lhs > rhs)
    return rhs;
  else
    return lhs;
}

int getCityNum(char *cityName) {
  int i;
  for (i = 0; i < gCityNum; i++) {
    if (strcmp(gCity[i], cityName) == 0)
      return i;
  }

  strcpy(gCity[gCityNum], cityName);
  return gCityNum++;
}

void setTrain(int idx, struct Train *trains, int dptH, int dptM, int arvH,
              int arvM, int fare, char dptCity[64], char arvCity[64]) {
  trains[idx].from = getCityNum(dptCity);
  trains[idx].to = getCityNum(arvCity);
  trains[idx].dpt = dptH * 60 + dptM;
  trains[idx].arv = arvH * 60 + arvM;
  trains[idx].fare = fare;
}

void setRTrain(int idx, struct Train *trains, int dptH, int dptM, int arvH,
               int arvM, int fare, char dptCity[64], char arvCity[64]) {
  trains[idx].from = getCityNum(arvCity);
  trains[idx].to = getCityNum(dptCity);
  trains[idx].dpt = BIAS - (arvH * 60 + arvM);
  trains[idx].arv = BIAS - (dptH * 60 + dptM);
  trains[idx].fare = fare;
}

int seekMaxFareTrain(struct Train *trains, int ub) {
  int i;
  int maxFare = -1;
  int res;
  for (i = 0; i < ub; i++) {
    if (maxFare < trains[i].fare) {
      maxFare = trains[i].fare;
      res = i;
    }
  }
  return res;
}

int seekMostDelayTrain(struct Train *trains, int ub) {
  int i;
  int mostDelay = -1;
  int res;
  for (i = 0; i < ub; i++) {
    if (mostDelay < abs(trains[i].dpt - trains[i].arv)) {
      mostDelay = abs(trains[i].dpt - trains[i].arv);
      res = i;
    }
  }
  return res;
}

int change(int city, int id, struct Train *trains) {
  int i;
  for (i = id - 1; i >= 0; i--) {
    if (trains[i].arv <= trains[id].dpt && trains[i].to == city) {
      return i;
    }
  }

  return 0;
}

int fr(char *name, int trainNum, int table[2001][101], struct Train *trains) {
  int src = getCityNum(name);

  // printf("%s %d\\n",name,src);

  table[0][src] = 0;
  int i;
  for (i = 0; i < trainNum; i++) {
    int j;
    for (j = 0; j < gCityNum; j++) {
      if (j == trains[i].to) {
        table[i + 1][j] =
            min(table[i][j],
                table[change(trains[i].from, i, trains) + 1][trains[i].from] +
                    trains[i].fare);
      } else
        table[i + 1][j] = table[i][j];
    }
  }
}

void printTable(int table[2001][101], int n) {
  int i;
  for (i = 0; i <= n; i++) {
    int j;
    for (j = 0; j < gCityNum; j++) {
      printf("%d ", table[i][j]);
    }
    printf("\\n");
  }
}

int mycmp(const void *t1, const void *t2) {
  return ((const struct Train *)t1)->arv - ((const struct Train *)t2)->arv;
}

void printTrains(struct Train *trains, int n) {
  int i;
  for (i = 0; i < n; i++) {
    printf("%d %d %d %d %d\\n", trains[i].from, trains[i].dpt, trains[i].to,
           trains[i].arv, trains[i].fare);
  }
}

int calc_cost(int city, struct Train *trains, struct Train *rtrains,
              int trainNum) {
  int i;
  int res = INF;
  for (i = 0; i < trainNum; i++) {
    int j;
    for (j = trainNum - 1; j >= 0; j--) {
      int stay = (BIAS - rtrains[j].arv) - trains[i].arv;
      if (stay < 30)
        continue;

      int c = from_hakodate[i + 1][city] + from_tokyo[i + 1][city] +
              to_hakodate[j + 1][city] + to_tokyo[j + 1][city];
      res = min(res, c);
    }
  }
  return res;
}

int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    struct Train *trains = (struct Train *)malloc(n * sizeof(struct Train));
    struct Train *rtrains = (struct Train *)malloc(n * sizeof(struct Train));
    int i = 0;
    int j = 0;
    for (i = 0, j = 0; i < n; i++) {
      int dptH, dptM, arvH, arvM, fare;
      char dptCity[64], arvCity[64];
      scanf("%s %d:%d %s %d:%d %d", dptCity, &dptH, &dptM, arvCity, &arvH,
            &arvM, &fare);
      if ((dptH * 60 + dptM < 8 * 60) || (arvH * 60 + arvM > 18 * 60))
        continue;
      setTrain(j, trains, dptH, dptM, arvH, arvM, fare, dptCity, arvCity);
      setRTrain(j++, rtrains, dptH, dptM, arvH, arvM, fare, dptCity, arvCity);
    }

    //    printTrains(trains,j);
    //    printf("\\n");
    qsort(trains, j, sizeof(struct Train), mycmp);
    qsort(rtrains, j, sizeof(struct Train), mycmp);

    //    printTrains(trains,j);
    //    printf("\\n");

    memset(from_hakodate, 0x3, sizeof(from_hakodate));
    fr("Hakodate", j, from_hakodate, trains);

    memset(from_tokyo, 0x3, sizeof(from_tokyo));
    fr("Tokyo", j, from_tokyo, trains);

    memset(to_hakodate, 0x3, sizeof(to_hakodate));
    fr("Hakodate", j, to_hakodate, rtrains);

    memset(to_tokyo, 0x3, sizeof(to_tokyo));
    fr("Tokyo", j, to_tokyo, rtrains);

    int mincost = INF;

    for (i = 0; i < gCityNum; i++) {
      mincost = min(calc_cost(i, trains, rtrains, j), mincost);
    }
    // printTable(to_hakodate,j);
    // printTable(to_tokyo,j);

    // printf("\\n");
    // printf("cityname:%s cityid:%d\\n",arvCity,getCityNum(arvCity));

    printf("%d\\n", mincost == INF ? 0 : mincost);

    memset(gCity, '\\0', sizeof(gCity));
    gCityNum = 0;
    free(trains);
    free(rtrains);
  }
  return 0;
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const int BIAS = (18 - 8) * 60;
static const int INF = 10000000;

int h2m(char *time) {
  int h, m;
  sscanf(time, "%d:%d", &h, &m);
  return h * 60 + m;
}

class Train {
private:
  string mFrom;
  string mTo;
  int mDpt;
  int mArv;
  int mFare;

public:
  Train(char *from, char *to, int dpt, int arv, int fare) {
    mFrom = from;
    mTo = to;
    mDpt = dpt;
    mArv = arv;
    mFare = fare;
  }
  Train() : mFrom(""), mTo(""), mDpt(0), mArv(0), mFare(0) {}

  string getFrom() const { return mFrom; }
  string getTo() const { return mTo; }
  int getDpt() const { return mDpt; }
  int getArv() const { return mArv; }
  int getFare() const { return mFare; }
};

class arv_order {
public:
  bool operator()(const Train train1, const Train train2) const {
    return train1.getArv() < train2.getArv();
  }
};

int change(const vector<Train> &tv, int p, int st, int dpt_time,
           map<string, int> city) {
  for (; p >= 0; p--) {
    if (city[tv[p].getTo()] == st && tv[p].getArv() <= dpt_time)
      break;
  }

  return p;
}

void make_table(int **v, int org, const vector<Train> &tv,
                map<string, int> city) {

  for (int i = 0; i < city.size(); i++) {
    v[i][0] = INF;
  }

  v[org][0] = 0;

  for (int i = 0; i < tv.size(); i++) {
    for (int j = 0; j < city.size(); j++) {
      v[j][i + 1] = v[j][i];
    }

    int a = change(tv, i - 1, city[tv[i].getFrom()], tv[i].getDpt(), city);

    v[city[tv[i].getTo()]][i + 1] =
        min(v[city[tv[i].getTo()]][i],
            tv[i].getFare() + v[city[tv[i].getFrom()]][a + 1]);
  }
}

int calc_cost(int city, const vector<Train> &trains,
              const vector<Train> &rtrains, int **from_hak, int **from_tok,
              int **to_hak, int **to_tok) {
  int a = 0;
  int d = trains.size() - 1;
  int min_c = INF;

  for (int d = trains.size() - 1; d >= 0; d--) {
    for (int a = 0; a < trains.size(); a++) {
      int stay = (BIAS - rtrains[d].getArv()) - trains[a].getArv();
      if (stay < 30)
        continue;
      int c = from_hak[city][a + 1] + from_tok[city][a + 1] +
              to_hak[city][d + 1] + to_tok[city][d + 1];

      min_c = min(min_c, c);
    }
  }

  return min_c;
}

int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    vector<Train> rtrains;
    vector<Train> trains;
    map<string, int> city;

    int **from_hak;
    int **from_tok;
    int **to_hak;
    int **to_tok;

    for (int i = 0, j = 0; i < n; i++) {
      char from[64], dpt[64], to[64], arv[64];
      int fare;
      scanf("%s %s %s %s %d", from, dpt, to, arv, &fare);
      // printf("time:%d\\n",getsec(src_time));
      if (h2m(dpt) < 8 * 60 || h2m(arv) > 18 * 60)
        continue;

      if (city.find(from) == city.end()) {
        city[from] = j++;
      }
      rtrains.push_back(
          Train(to, from, BIAS - h2m(arv), BIAS - h2m(dpt), fare));
      trains.push_back(Train(from, to, h2m(dpt), h2m(arv), fare));
    }

    from_hak = new int *[city.size() + 1];
    from_tok = new int *[city.size() + 1];
    to_hak = new int *[city.size() + 1];
    to_tok = new int *[city.size() + 1];

    for (int i = 0; i < city.size() + 1; i++) {
      from_hak[i] = new int[trains.size() + 1];
      from_tok[i] = new int[trains.size() + 1];
      to_hak[i] = new int[trains.size() + 1];
      to_tok[i] = new int[trains.size() + 1];
    }

    sort(trains.begin(), trains.end(), arv_order());
    sort(rtrains.begin(), rtrains.end(), arv_order());

    make_table(from_hak, city["Hakodate"], trains, city);
    make_table(from_tok, city["Tokyo"], trains, city);
    make_table(to_hak, city["Hakodate"], rtrains, city);
    make_table(to_tok, city["Tokyo"], rtrains, city);

    int min_cost = INF;

    for (int i = 0; i < city.size(); i++) {
      int cost =
          calc_cost(i, trains, rtrains, from_hak, from_tok, to_hak, to_tok);
      min_cost = min(min_cost, cost);
    }

    printf("%d\\n", min_cost >= INF ? 0 : min_cost);

    for (int i = 0; i < city.size() + 1; i++) {
      delete[] from_hak[i];
      delete[] from_tok[i];
      delete[] to_hak[i];
      delete[] to_tok[i];
    }

    delete[] from_hak;
    delete[] from_tok;
    delete[] to_hak;
    delete[] to_tok;
  }

  return 0;
}