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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int d;
  cin >> d;

  while (--d >= 0) {
    int n, a, b;
    cin >> n >> a >> b;

    int sx, sy;
    cin >> sx >> sy;
    vector<int> x(n), y(n);
    for (int i = 0; i < n; ++i)
      cin >> x[i] >> y[i];

    vector<vector<bool>> near(n, vector<bool>(n, false));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < i; ++j) {
        int dx = x[i] - x[j];
        int dy = y[i] - y[j];
        if (dx * dx + dy * dy <= 50 * 50)
          near[i][j] = near[j][i] = true;
      }
    }

    vector<bool> check(n, false);
    queue<int> q;
    for (int i = 0; i < n; ++i) {
      int dx = sx - x[i];
      int dy = sy - y[i];
      if (dx * dx + dy * dy <= 10 * 10) {
        check[i] = true;
        q.push(i);
      }
    }

    if (a > b) {
      bool loop = false;
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          if (check[i] && !check[j] && near[i][j])
            loop = true;
        }
      }
      if (loop)
        cout << "You're always welcome!" << endl;
      else if (q.size() == 0)
        cout << 0 << endl;
      else
        cout << a << endl;
    } else {
      int ret = 0;
      while (!q.empty()) {
        int m = q.size();
        while (--m >= 0) {
          int pos = q.front();
          q.pop();
          for (int i = 0; i < n; ++i) {
            if (near[pos][i] && !check[i]) {
              check[i] = true;
              q.push(i);
            }
          }
        }
        ret += a;
      }
      cout << ret << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int d;
  cin >> d;

  while (--d >= 0) {
    int n, a, b;
    cin >> n >> a >> b;

    int sx, sy;
    cin >> sx >> sy;
    vector<int> x(n), y(n);
    for (int i = 0; i < n; ++i)
      cin >> x[i] >> y[i];

    vector<vector<bool>> near(n, vector<bool>(50, false));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < i; ++j) {
        int dx = x[i] - x[j];
        int dy = y[i] - y[j];
        if (dx * dx + dy * dy <= 50 * 50)
          near[i][j] = near[j][i] = true;
      }
    }

    vector<bool> check(n, false);
    queue<int> q;
    for (int i = 0; i < n; ++i) {
      int dx = sx - x[i];
      int dy = sx - y[i];
      if (dx * dx + dy * dy <= 10 * 10) {
        check[i] = true;
        q.push(i);
      }
    }

    if (a > b) {
      bool loop = false;
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          if (check[i] && !check[j] && near[i][j])
            loop = true;
        }
      }
      if (loop)
        cout << "You're always welcome!" << endl;
      else if (q.size() == 0)
        cout << 0 << endl;
      else
        cout << a << endl;
    } else {
      int ret = 0;
      while (!q.empty()) {
        int m = q.size();
        while (--m >= 0) {
          int pos = q.front();
          q.pop();
          for (int i = 0; i < n; ++i) {
            if (near[pos][i] && !check[i]) {
              check[i] = true;
              q.push(i);
            }
          }
        }
        ret += a;
      }
      cout << ret << endl;
    }
  }

  return 0;
}