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

#include "algorithm"
#include "cmath"
#include "cstdio"
using namespace std;

const int maxn = 10100;
struct edge {
  int u, v;
  double w;
};
edge edges[maxn * maxn];
int f[maxn], n, m, p;
double tot, res;
int wx[maxn], wy[maxn];

double dis(int i, int j) {
  return sqrt(1.0 * (wx[i] - wx[j]) * (wx[i] - wx[j]) +
              (wy[i] - wy[j]) * (wy[i] - wy[j]));
}

int find(int x) { return f[x] == x ? f[x] : f[x] = find(f[x]); }
int cmp(edge a, edge b) { return a.w > b.w; }

void add(int u, int v, double w) {
  edges[++p].u = u;
  edges[p].v = v;
  edges[p].w = w;
}

int main() {
  // freopen("v.txt","r",stdin);
  // tot=res=.0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &wx[i], &wy[i]);
    f[i] = i;
  }

  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v, dis(u, v));
    tot = tot + dis(u, v);
  }

  sort(edges + 1, edges + p + 1, cmp);
  for (int i = 1; i <= p; i++) {
    edge &e = edges[i];
    if (find(e.u) != find(e.v)) {
      res = res + e.w;
      f[find(e.u)] = find(e.v);
    }
  }

  printf("%.3f\\n", tot - res);
  return 0;
}


Error: 0

Fixed submission:

// Problem:
// Date:
// Skill:

#define CLR(a) memset((a), 0, sizeof(a))
#define MAX 2140000000;
#define RE(i, n) for (i = 0; i < n; i++)
#define RE2(i, n) for (i = 1; i <= n; i++)
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
using namespace std;

struct Edge {
  int from, to, w;
} es[11000];

int dis[110][110];
int x[10010];
int y[10010];
inline int delta_sq(int i, int j) {
  int dx, dy;
  dx = x[i] - x[j];
  dy = y[i] - y[j];
  return dx * dx + dy * dy;
}
inline int find(int x, int father[]) {
  return father[x] == x ? x : father[x] = find(father[x], father);
}
int cmp(int a, int b) { return es[a].w > es[b].w; }
double kruscal(int n, int m) {
  double ans(0);
  int i, j, k;
  int r[11000];
  int father[11000];

  RE2(i, n) father[i] = i;
  RE2(i, m) r[i] = i;

  sort(r + 1, r + 1 + m, cmp);
  RE2(i, m) {
    Edge e = es[r[i]];
    int x = find(e.from, father), y = find(e.to, father);
    if (x != y) {
      father[x] = y;
      //			ans = max(ans, e.w);
    } else {
      ans += sqrt((double)e.w);
    }
  }
  /*
  int dif(0);
  int temp(-1);
  RE2(i, n)
  {
          if (find(i,father)!= temp)
          {
                  temp = father[i];
                  dif++;
          }
  }
  if (dif > 1)return -1;*/
  return ans;
}

// #define LOCAL
int main() {
#ifdef LOCAL

  freopen("C:\\\\Users\\\\VULCAN\\\\Desktop\\\\data.in", "r", stdin);
  //	freopen("C:\\\\Users\\\\VULCAN\\\\Desktop\\\\data.out", "w", stdout);
#endif
  // 这是一条帅气的分界线，写程序从这里开始吧
  int i, j, k;
  int vn, en;

  while (cin >> en >> vn) {

    int sumw(0);
    RE2(i, en) { cin >> x[i] >> y[i]; }

    {
      int cnt(1);
      int a, b, c;
      RE2(i, vn) {
        cin >> es[cnt].from >> es[cnt].to;
        es[cnt].w = delta_sq(es[cnt].from, es[cnt].to);

        cnt++;
      }
    }

    double ans;
    ans = kruscal(en, vn);
    printf("%.3f\\n", ans);
  }

  // 这是一条可爱的分界线
#ifdef LOCAL
  printf("用时%.3f秒\\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}