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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stdio.h>

using namespace std;

int main() {

  int n, r, k, to, table[101][101], num_of_packet, start, goal, t_limit,
      memo[101][101], P[101], D[101], num, min, min_index, current;

  for (int i = 1; i < 101; i++) {
    for (int k = 1; k < 101; k++) {
      table[i][k] = 0;
      memo[i][k] = 0;
    }
  }

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &r, &k);
    for (int p = 0; p < k; p++) {
      scanf("%d", &to);
      table[r][to] = 1;
    }
  }

  scanf("%d", &num_of_packet);

  for (int i = 0; i < num_of_packet; i++) {
    scanf("%d %d %d", &start, &goal, &t_limit);

    if (t_limit <= 1) {
      printf("NA\\n");
      continue;
    }

    if (memo[start][goal] != 0) {
      if (memo[start][goal] <= t_limit) {
        printf("%d\\n", memo[start][goal]);
      } else {
        printf("NA\\n");
      }
      continue;
    }

    for (int index = 1; index <= n; index++) {
      P[index] = 0;          //???????????????????????¨???????????????
      D[index] = 1000000000; // start??°?????????????????¢?????¨???????????????
    }

    P[start] = 1; //??????????????°????????????????????¨??????
    num = 1;

    for (int index = 1; index <= n; index++) {
      if (index != start && table[start][index] == 1) {
        D[index] =
            1; // start??°???????????????????????????start??°??????????????¢1??¨??????
      }
    }

    while (num < n) {
      min = 1000000000;
      for (int index = 1; index <= n;
           index++) { //??????????????°?????????????????????????????¢???
        if (P[index] == 0 &&
            D[index] <
                min) { //?????????????????§?????????????????????????????????
          min = D[index];
          min_index = index;
        }
      }

      if (min == 1000000000)
        break; //?????????????????????????????????????????°break

      P[min_index] = 1; // min_index?????????????????¨??????
      current = min_index;
      num++; //???????????????????????°???1?¢???????

      for (int i = 1; i <= n; i++) {
        if (table[current][i] ==
            1) { // start???????????????????????¨???????????£????????????
          D[i] =
              (D[i] <= (D[current] + 1))
                  ? D[i]
                  : (D[current] +
                     1); //?????????????????´??\\????????¢??¨???current????????±???????????¢???????????????D??¨??????
        }
      }
    }

    if (D[goal] >= t_limit) {
      printf("NA\\n");
    } else {
      memo[start][goal] = D[goal] + 1;
      printf("%d\\n", D[goal] + 1);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stdio.h>

using namespace std;

struct Packet {
  Packet() { ttl = current = 0; }

  void copy(Packet t) {
    ttl = t.ttl;
    current = t.current;
    for (int i = 0; i < 101; i++)
      checkTable[i] = t.checkTable[i];
  }

  Packet(int arg_ttl, int loc) {
    ttl = arg_ttl;
    for (int i = 0; i < 101; i++)
      checkTable[i] = 0;
    checkTable[loc] = 1;
    current = loc;
  }

  void set(int loc) {
    checkTable[loc] = 1;
    current = loc;
  }

  int ttl, checkTable[101], current;
};

int main() {

  int n, r, k, to, table[101][101], num_of_packet, start, goal, t_limit;
  Packet tmp;

  for (int i = 1; i < 101; i++) {
    for (int k = 1; k < 101; k++) {
      table[i][k] = 0;
    }
  }

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &r, &k);
    for (int p = 0; p < k; p++) {
      scanf("%d", &to);
      table[r][to] = 1;
    }
  }

  scanf("%d", &num_of_packet);

  queue<Packet> Q;
  bool FLG;

  for (int i = 0; i < num_of_packet; i++) {
    scanf("%d %d %d", &start, &goal, &t_limit);

    FLG = false;

    if (t_limit <= 1) {
      printf("NA\\n");
      FLG = true;
    }

    if (!FLG) {
      for (int d = 1; d <= n; d++) {
        if (d != start && table[start][d] == 1) {
          Packet p(t_limit - 1, start);
          p.set(d);
          Q.push(p);
        }
      }
    }

    while (!Q.empty()) {
      tmp.copy(Q.front());
      Q.pop();

      if (tmp.ttl > 0 && tmp.current == goal) {
        printf("%d\\n", t_limit - tmp.ttl + 1);
        FLG = true;

        while (!Q.empty())
          Q.pop();

        break;
      }

      if (tmp.ttl > 1 && t_limit - tmp.ttl + 1 <= n) {
        for (int m = 1; m <= n; m++) {
          if (m != tmp.current && tmp.checkTable[m] == 0 &&
              table[tmp.current][m] == 1) {
            Packet q;
            q.copy(tmp);
            q.ttl--;
            q.set(m);
            Q.push(q);
          }
        }
      }
      if (FLG)
        break;
    }

    if (!FLG) {
      printf("NA\\n");
    }
  }

  return 0;
}