Problem p01309 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
typedef struct {
  int c, v, x, y, time, flag;
} CAT_t;
int main() {
  CAT_t a[1000];
  int X, Y, N, n, Nx, Ny;
  scanf("%d", &n);
  int i = 0;
  while (i < n) {
    int b = 0, ans = 0;
    for (int j = 0; j < 1000; j++) {
      a[j].v = 0;
      a[j].time = 0;
      a[j].c = 0;
    }
    scanf("%d %d %d %d %d", &N, &X, &Y, &Nx, &Ny);
    for (int j = 0; j < N; j++)
      scanf("%d %d", &a[j].x, &a[j].y);
    for (int j = 0; j < N; j++) {
      if (((a[j].x - Nx) * (a[j].x - Nx) + (a[j].y - Ny) * (a[j].y - Ny)) <=
          100) {
        ans = X;
        a[j].v = 1;
        a[j].time = Y;
        a[j].c++;
      }
    }
    for (int j = 0; j < N; j++)
      a[j].flag = 0;
    while (1) {
      int count = 0;
      for (int j = 0; j < N; j++)
        if (a[j].v == 1)
          count++;
      if (count == 0)
        break;
      for (int j = 0; j < N; j++) {
        if (a[j].v == 1) {
          for (int k = 0; k < N; k++)
            if ((a[j].x - a[k].x) * (a[j].x - a[k].x) +
                        (a[j].y - a[k].y) * (a[j].y - a[k].y) <=
                    2500 &&
                a[k].time <= 0 && a[k].flag == 0) {
              a[k].time = Y;
              a[k].c++;
            }
        }
      }
      for (int j = 0; j < N; j++)
        a[j].flag = 0;
      for (int j = 0; j < N; j++) {
        if (a[j].v == 1) {
          a[j].time -= X;
          if (a[j].time == 0)
            a[j].flag = 1;
          a[j].v = 0;
        }
        if (a[j].time == Y)
          a[j].v = 1;
        if (a[j].c == 2) {
          printf("You're always welcome!\\n");
          goto neko;
        }
      }
      ans += X;
      b = 1;
    }
    if (b == 1)
      ans -= X;
    printf("%d\\n", ans);
  neko:
    i++;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
typedef struct {
  int c, v, x, y, time;
} CAT_t;
int main() {
  CAT_t a[1000];
  int X, Y, N, n, Nx, Ny;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int b = 0, ans = 0;
    for (int j = 0; j < 1000; j++) {
      a[j].v = 0;
      a[j].time = 0;
      a[j].c = 0;
    }
    scanf("%d %d %d %d %d", &N, &X, &Y, &Nx, &Ny);
    for (int j = 0; j < N; j++)
      scanf("%d %d", &a[j].x, &a[j].y);
    for (int j = 0; j < N; j++) {
      if (((a[j].x - Nx) * (a[j].x - Nx) + (a[j].y - Ny) * (a[j].y - Ny)) <=
          100) {
        ans = X;
        a[j].v = 1;
        a[j].time = Y;
        a[j].c++;
      }
    }
    while (1) {
      int count = 0;
      for (int j = 0; j < N; j++)
        if (a[j].v == 1)
          count++;
      if (count == 0)
        break;
      for (int j = 0; j < N; j++) {
        if (a[j].v == 1) {
          for (int k = 0; k < N; k++)
            if ((a[j].x - a[k].x) * (a[j].x - a[k].x) +
                        (a[j].y - a[k].y) * (a[j].y - a[k].y) <=
                    2500 &&
                a[k].time <= 0) {
              a[k].time = Y;
              a[k].c++;
            }
        }
      }
      for (int j = 0; j < N; j++) {
        if (a[j].v == 1) {
          a[j].time -= X;
          a[j].v = 0;
        }
        if (a[j].time == Y)
          a[j].v = 1;
        if (a[j].c == 2) {
          printf("You're always welcome!\\n");
        }
      }
      ans += X;
      b = 1;
    }
    if (b == 1)
      ans -= X;
    printf("%d\\n", ans);
  }
  return 0;
}