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

#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

typedef complex<double> P;

#define EPS (1e-10)
#define EQ(a, b) ((abs((a) - (b))) < EPS)

int past[1001];
vector<int> G[1001];
P points[1001];
bool loud[2][1001];

/*
Ü¸£50ÈàÌXõ¯mðGbWÅñ¾Otðì¬
 éXõª­ºðI¦Ä©çAybæè¨¨­ÌÔª§ÁÄ¢éêÍA×Ú·ém[hÌXõª­ºÂ\\
Ü½AXõÌ­ºÍAxb²ÆÉ¯ÉsíêéB
æÁÄA­ºI¹ãy/x+1ñÌ­º²ÆÉAÌ­ºªÂ\\ÆÈéB
Ü½AÇÌæ¤ÈðÌ³Âè~ÜÈ¢©ðl¦éB
Ü¸AOñÜÅÉ¯¶­ºXõÌgÝí¹ÉoïÁ½©Ç¤©ðl¦éBi2^1000Êèà éÌÅA·×ÄÌóÔÍÛÅ«È¢j
*/

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, x, y;
    int nx, ny;
    int a;
    cin >> n >> x >> y;
    cin >> nx >> ny;
    a = y / x + 1;
    P np = P(nx, ny);
    for (int i = 0; i < n; i++)
      G[i].clear();
    memset(loud, 0, sizeof(loud));
    memset(past, 0, sizeof(past));
    for (int i = 0; i < n; i++) {
      int ex, ey;
      cin >> ex >> ey;
      points[i] = P(ex, ey);
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        double d = abs(points[i] - points[j]);
        if (EQ(d, 50) || d < 50) {
          G[i].push_back(j);
          G[j].push_back(i);
        }
      }
    }
    set<int> st;
    for (int i = 0; i < n; i++) {
      double d = abs(points[i] - np);
      if (EQ(d, 10) || d < 10) {
        loud[0][i] = true;
        past[i] = a;
        st.insert(i);
      }
    }
    // Åå2000ñV~[V
    int cnt;
    int cur = 0;
    int nxt = 1;
    set<set<int>> ssi;
    ssi.insert(st);
    for (cnt = 0;; cnt++) {
      set<int> s;
      memset(loud[nxt], 0, sizeof(loud[nxt]));
      // »ÝtrueÈçÎA×Úm[hðtrueÉ·é
      bool fin = true;
      for (int i = 0; i < n; i++) {
        if (loud[cur][i]) {
          fin = false;
          for (int j = 0; j < (int)G[i].size(); j++) {
            int to = G[i][j];
            if (!loud[cur][to] && past[to] <= 0 && !loud[nxt][to]) {
              s.insert(to);
              loud[nxt][to] = true;
              past[to] = a + 1;
            }
          }
        }
      }
      if (fin) {
        cout << cnt * x << endl;
        break;
      } else if (ssi.find(s) != ssi.end()) {
        cout << "You're always welcome!" << endl;
        break;
      }
      ssi.insert(s);
      for (int i = 0; i < n; i++)
        past[i]--;
      swap(nxt, cur);
    }
    // goalsÂ\\
//		if(cnt==100000)
    //			cout<<"You're always welcome!"<<endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

typedef complex<double> P;

#define EPS (1e-10)
#define EQ(a, b) ((abs((a) - (b))) < EPS)

int past[1001];
vector<int> G[1001];
P points[1001];
bool loud[2][1001];

/*
Ü¸£50ÈàÌXõ¯mðGbWÅñ¾Otðì¬
 éXõª­ºðI¦Ä©çAybæè¨¨­ÌÔª§ÁÄ¢éêÍA×Ú·ém[hÌXõª­ºÂ\\
Ü½AXõÌ­ºÍAxb²ÆÉ¯ÉsíêéB
æÁÄA­ºI¹ãy/x+1ñÌ­º²ÆÉAÌ­ºªÂ\\ÆÈéB
Ü½AÇÌæ¤ÈðÌ³Âè~ÜÈ¢©ðl¦éB
Ü¸AOñÜÅÉ¯¶­ºXõÌgÝí¹ÉoïÁ½©Ç¤©ðl¦éBi2^1000Êèà éÌÅA·×ÄÌóÔÍÛÅ«È¢j
*/

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, x, y;
    int a = y / x + 1;
    cin >> n >> x >> y;
    int nx, ny;
    cin >> nx >> ny;
    P np = P(nx, ny);
    for (int i = 0; i < n; i++) {
      int ex, ey;
      cin >> ex >> ey;
      points[i] = P(ex, ey);
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        double d = abs(points[i] - points[j]);
        if (EQ(d, 50) || d < 50) {
          G[i].push_back(j);
          G[j].push_back(i);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      double d = abs(points[i] - np);
      if (EQ(d, 10) || d < 10) {
        loud[0][i] = true;
        past[i] = a;
      }
    }
    memset(past, 0, sizeof(past));
    // Åå2000ñV~[V
    int cnt;
    int cur = 0;
    int nxt = 1;
    for (cnt = 0; cnt < 2000; cnt++) {
      memset(loud[nxt], 0, sizeof(loud[nxt]));
      // »ÝtrueÈçÎA×Úm[hðtrueÉ·é
      bool fin = true;
      for (int i = 0; i < n; i++) {
        if (loud[cur][i]) {
          fin = false;
          for (int j = 0; j < (int)G[i].size(); j++) {
            int to = G[i][j];
            if (!loud[cur][to] && past[to] <= 0 && !loud[nxt][to]) {
              loud[nxt][to] = true;
              past[to] = a;
            }
          }
        }
      }
      if (fin) {
        cout << cnt * x << endl;
        break;
      }
      for (int i = 0; i < n; i++)
        past[i]--;
      swap(nxt, cur);
    }
    // goalsÂ\\
		if(cnt==1000)cout<<"You're always welcome!"<<endl;
  }
  return 0;
}