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

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

struct Info {
  Info(int arg_to, int arg_dist) {
    to = arg_to;
    dist = arg_dist;
  }
  int to, dist;
};

struct Data {
  Data(int arg_town, int arg_sum_dist, int arg_fuel) {
    town = arg_town;
    sum_dist = arg_sum_dist;
    fuel = arg_fuel;
  }
  bool operator<(const struct Data &arg) const {
    return sum_dist > arg.sum_dist;
  };
  int town, sum_dist, fuel;
};

char town_table[6000][16];
bool have_station[6000];

int town_index, N, M, start = 0, goal = 1, capacity;
int **min_dist;

bool strCmp(char *base, char *comp) {
  int length1, length2;
  for (length1 = 0; base[length1] != '\\0'; length1++)
    ;
  for (length2 = 0; comp[length2] != '\\0'; length2++)
    ;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void strcpy(char *to, char *str) {
  for (int i = 0; str[i] != '\\0'; i++) {
    to[i] = str[i];
    to[i + 1] = '\\0';
  }
}

void func() {

  town_index = 0;

  char left[16], right[16];
  scanf("%s %s", left, right);

  strcpy(town_table[town_index++], left);
  strcpy(town_table[town_index++], right);

  int dist;

  bool FLG;
  int left_index, right_index;

  vector<Info> V[6000];

  for (int loop = 0; loop < N; loop++) {
    scanf("%s %s %d", left, right, &dist);

    FLG = false;
    for (int i = 0; i < town_index; i++) {
      if (strCmp(town_table[i], left)) {
        FLG = true;
        left_index = i;
        break;
      }
    }
    if (!FLG) {
      left_index = town_index;
      strcpy(town_table[town_index++], left);
    }

    FLG = false;
    for (int i = 0; i < town_index; i++) {
      if (strCmp(town_table[i], right)) {
        FLG = true;
        right_index = i;
        break;
      }
    }
    if (!FLG) {
      right_index = town_index;
      strcpy(town_table[town_index++], right);
    }

    V[left_index].push_back(Info(right_index, dist));
    V[right_index].push_back(Info(left_index, dist));
  }

  for (int i = 0; i < town_index; i++)
    have_station[i] = false;

  for (int loop = 0; loop < M; loop++) {
    scanf("%s", left);
    for (int i = 0; i < town_index; i++) {
      if (strCmp(town_table[i], left)) {
        have_station[i] = true;
        break;
      }
    }
  }

  for (int i = 0; i < town_index; i++) {
    for (int k = 0; k <= 10 * capacity; k++)
      min_dist[i][k] = BIG_NUM;
  }

  priority_queue<Data> Q;

  min_dist[0][10 * capacity] = 0;
  Q.push(Data(0, 0, 10 * capacity));

  while (!Q.empty()) {

    if (Q.top().town == goal) {
      Q.pop();
    } else if (Q.top().sum_dist > min_dist[Q.top().town][Q.top().fuel]) {
      Q.pop();
    } else {

      for (int i = 0; i < V[Q.top().town].size(); i++) {
        if (V[Q.top().town][i].dist <= Q.top().fuel) {
          if (have_station[V[Q.top().town][i].to]) {
            if (min_dist[V[Q.top().town][i].to][10 * capacity] >
                Q.top().sum_dist + V[Q.top().town][i].dist) {
              min_dist[V[Q.top().town][i].to][10 * capacity] =
                  Q.top().sum_dist + V[Q.top().town][i].dist;
              Q.push(Data(V[Q.top().town][i].to,
                          Q.top().sum_dist + V[Q.top().town][i].dist,
                          10 * capacity));
            }
          } else {
            if (min_dist[V[Q.top().town][i].to]
                        [Q.top().fuel - V[Q.top().town][i].dist] >
                Q.top().sum_dist + V[Q.top().town][i].dist) {
              min_dist[V[Q.top().town][i].to]
                      [Q.top().fuel - V[Q.top().town][i].dist] =
                          Q.top().sum_dist + V[Q.top().town][i].dist;
              Q.push(Data(V[Q.top().town][i].to,
                          Q.top().sum_dist + V[Q.top().town][i].dist,
                          Q.top().fuel - V[Q.top().town][i].dist));
            }
          }
        }
      }
      Q.pop();
    }
  }

  int ans = BIG_NUM;

  for (int i = 0; i <= 10 * capacity; i++)
    ans = min(ans, min_dist[1][i]);

  if (ans == BIG_NUM) {
    printf("-1\\n");
  } else {
    printf("%d\\n", ans);
  }
}

int main() {

  min_dist = new int *[6000];
  for (int i = 0; i < 6000; i++)
    min_dist[i] = new int[2001];

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

    func();
  }

  return 0;
}

Error: 0

Fixed submission:

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

struct Info {
  Info(int arg_to, int arg_dist) {
    to = arg_to;
    dist = arg_dist;
  }
  int to, dist;
};

struct Data {
  Data(int arg_town, int arg_sum_dist, int arg_fuel) {
    town = arg_town;
    sum_dist = arg_sum_dist;
    fuel = arg_fuel;
  }
  bool operator<(const struct Data &arg) const {
    return sum_dist > arg.sum_dist;
  };
  int town, sum_dist, fuel;
};

char town_table[3000][15];
bool have_station[3000];

int town_index, N, M, start = 0, goal = 1, capacity;
int **min_dist;

bool strCmp(char *base, char *comp) {
  int length1, length2;
  for (length1 = 0; base[length1] != '\\0'; length1++)
    ;
  for (length2 = 0; comp[length2] != '\\0'; length2++)
    ;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void strcpy(char *to, char *str) {
  for (int i = 0; str[i] != '\\0'; i++) {
    to[i] = str[i];
    to[i + 1] = '\\0';
  }
}

void func() {

  town_index = 0;

  char left[16], right[16];
  scanf("%s %s", left, right);

  strcpy(town_table[town_index++], left);
  strcpy(town_table[town_index++], right);

  int dist;

  bool FLG;
  int left_index, right_index;

  vector<Info> V[3000];

  for (int loop = 0; loop < N; loop++) {
    scanf("%s %s %d", left, right, &dist);

    FLG = false;
    for (int i = 0; i < town_index; i++) {
      if (strCmp(town_table[i], left)) {
        FLG = true;
        left_index = i;
        break;
      }
    }
    if (!FLG) {
      left_index = town_index;
      strcpy(town_table[town_index++], left);
    }

    FLG = false;
    for (int i = 0; i < town_index; i++) {
      if (strCmp(town_table[i], right)) {
        FLG = true;
        right_index = i;
        break;
      }
    }
    if (!FLG) {
      right_index = town_index;
      strcpy(town_table[town_index++], right);
    }

    V[left_index].push_back(Info(right_index, dist));
    V[right_index].push_back(Info(left_index, dist));
  }

  for (int i = 0; i < town_index; i++)
    have_station[i] = false;

  for (int loop = 0; loop < M; loop++) {
    scanf("%s", left);
    for (int i = 0; i < town_index; i++) {
      if (strCmp(town_table[i], left)) {
        have_station[i] = true;
        break;
      }
    }
  }

  for (int i = 0; i < town_index; i++) {
    for (int k = 0; k <= 10 * capacity; k++)
      min_dist[i][k] = BIG_NUM;
  }

  priority_queue<Data> Q;

  min_dist[0][10 * capacity] = 0;
  Q.push(Data(0, 0, 10 * capacity));

  while (!Q.empty()) {

    if (Q.top().town == goal) {
      Q.pop();
    } else if (Q.top().sum_dist > min_dist[Q.top().town][Q.top().fuel]) {
      Q.pop();
    } else {

      for (int i = 0; i < V[Q.top().town].size(); i++) {
        if (V[Q.top().town][i].dist <= Q.top().fuel) {
          //?????????station?????????
          if (have_station[V[Q.top().town][i].to]) {
            if (min_dist[V[Q.top().town][i].to][10 * capacity] >
                Q.top().sum_dist + V[Q.top().town][i].dist) {
              min_dist[V[Q.top().town][i].to][10 * capacity] =
                  Q.top().sum_dist + V[Q.top().town][i].dist;
              Q.push(Data(V[Q.top().town][i].to,
                          Q.top().sum_dist + V[Q.top().town][i].dist,
                          10 * capacity));
            }
          } else {
            if (min_dist[V[Q.top().town][i].to]
                        [Q.top().fuel - V[Q.top().town][i].dist] >
                Q.top().sum_dist + V[Q.top().town][i].dist) {
              min_dist[V[Q.top().town][i].to]
                      [Q.top().fuel - V[Q.top().town][i].dist] =
                          Q.top().sum_dist + V[Q.top().town][i].dist;
              Q.push(Data(V[Q.top().town][i].to,
                          Q.top().sum_dist + V[Q.top().town][i].dist,
                          Q.top().fuel - V[Q.top().town][i].dist));
            }
          }
        }
      }
      Q.pop();
    }
  }

  int ans = BIG_NUM;

  for (int i = 0; i <= 10 * capacity; i++)
    ans = min(ans, min_dist[1][i]);

  if (ans == BIG_NUM) {
    printf("-1\\n");
  } else {
    printf("%d\\n", ans);
  }
}

int main() {

  min_dist = new int *[3000];
  for (int i = 0; i < 3000; i++)
    min_dist[i] = new int[2001];

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

    func();
  }

  return 0;
}