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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int i, j, k, n, m, x, y, z, xx, yy;
struct node {
  int x, y;
} a[20000];
struct egde {
  int x, y;
  double z;
} b[500000];

int fa[20000];
double sum, now;

double dist(int x, int y) {
  return sqrt((a[x].x - a[y].x) * (a[x].x - a[y].x) * 1.0 +
              (a[x].y - a[y].y) * (a[x].y - a[y].y) * 1.0);
}

bool cmp(struct egde a, struct egde b) {
  if (a.z > b.z)
    return true;
  return false;
}

int getf(int x) {
  if (x == fa[x])
    return x;
  return (fa[x] = getf(fa[x]));
}

int main() {
  scanf("%d%d", &n, &m);

  for (i = 1; i <= n; i++)
    fa[i] = i;
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].x, &a[i].y);
  }
  sum = 0;
  for (i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    b[i].x = x;
    b[i].y = y;
    b[i].z = dist(x, y);
    sum += b[i].z;
  }
  now = 0;
  sort(b, b + m, cmp);

  for (i = 0; i < m; i++) {
    xx = getf(b[i].x);
    yy = getf(b[i].y);
    if (xx != yy) {
      now += b[i].z;
      fa[xx] = yy;
    }
  }

  printf("%.3lf\\n", sum - now);

  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>

using namespace std;

const int maxn = 51;

int p[maxn][maxn];
int dis[maxn][maxn], ans, sum, w, h, n;
const int f[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int x1, x2, y1, y2;
int sx, sy, tx, ty;
int x, y;
queue<int> Q;

void bfs() {
  while (!Q.empty())
    Q.pop();
  for (int i = 0; i < w; i++)
    for (int j = 0; j < h; j++)
      dis[i][j] = -1;
  dis[sx][sy] = 0;
  Q.push(sx);
  Q.push(sy);
  while (!Q.empty()) {
    x = Q.front();
    Q.pop();
    y = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++)
      if ((p[x][y] & (1 << i)) && dis[x + f[i][0]][y + f[i][1]] == -1) {
        dis[x + f[i][0]][y + f[i][1]] = dis[x][y] + 1;
        Q.push(x + f[i][0]);
        Q.push(y + f[i][1]);
      }
  }
  // printf("%d\\n",dis[tx][ty]);
}

int main() {
  while (scanf("%d%d%d", &w, &h, &n) == 3 && (w || h || n)) {
    for (int i = 0; i < w; i++)
      for (int j = 0; j < h; j++) {
        p[i][j] = 0;
        for (int k = 0; k < 4; k++)
          if (0 <= i + f[k][0] && i + f[k][0] < w && 0 <= j + f[k][1] &&
              j + f[k][1] < h)
            p[i][j] |= 1 << k;
      }
    while (n--) {
      scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
      if (x1 == x2) {
        if (x1 != 0 && x2 != w) {
          for (int i = y1; i < y2; i++)
            p[x1 - 1][i] = p[x1 - 1][i] & (~1), p[x1][i] = p[x1][i] & (~2);
        }
      }
      if (y1 == y2) {
        if (y1 != 0 && y2 != h) {
          for (int i = x1; i < x2; i++)
            p[i][y1 - 1] = p[i][y1 - 1] & (~4), p[i][y1] = p[i][y] & (~8);
        }
      }
    }
    ans = 0;
    scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
    bfs();
    sum = dis[tx][ty];
    ans = 0;
    for (x1 = x2 = 1; x1 < w; x1++, x2++)
      for (y1 = 0, y2 = 1; y2 <= h; y1++, y2++) {
        if ((p[x1 - 1][y1] & 1) == 0)
          continue;
        p[x1 - 1][y1] ^= 1;
        p[x1][y1] ^= 2;
        bfs();
        if (dis[tx][ty] != -1 && dis[tx][ty] - sum > ans)
          ans = dis[tx][ty] - sum;
        p[x1 - 1][y1] ^= 1;
        p[x1][y1] ^= 2;
      }
    for (y1 = y2 = 1; y1 < h; y1++, y2++)
      for (x1 = 0, x2 = 1; x2 <= w; x1++, x2++) {
        if ((p[x1 - 1][y1] & 4) == 0)
          continue;
        p[x1 - 1][y1] ^= 4;
        p[x1][y1] ^= 8;
        bfs();
        if (dis[tx][ty] != -1 && dis[tx][ty] - sum > ans)
          ans = dis[tx][ty] - sum;
        p[x1][y1 - 1] ^= 4;
        p[x1][y1] ^= 8;
      }
    printf("%d\\n", ans);
  }
  return 0;
}