Problem p01341 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define inf 1 << 30
#define ll long long
#define db double
#define c233 cout << "233" << endl
#define mem(s) memset(s, 0, sizeof(s))
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
const int N = 10050;
using namespace std;

struct lll {
  int x, y;
} p[N];
struct hhh {
  int a, b;
  db len;
} e[N * 10];
int fa[N];
int n, m;
db sum, mst;

void fre() {
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
}

inline int gi() {
  int x = 0, res = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      res *= -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0')
    x = (x << 1) + (x << 3) + ch - 48, ch = getchar();
  return x * res;
}

db len(lll x, lll y) {
  return sqrt(
      (1.0 * (x.x - y.x) * (x.x - y.x) + 1.0 * (x.y - y.y) * (x.y - y.y)));
}

void init() {
  n = gi();
  m = gi();
  for (int i = 1; i <= n; i++)
    p[i].x = gi(), p[i].y = gi();
  for (int i = 1; i <= m; i++) {
    e[i].a = gi(), e[i].b = gi();
    e[i].len = len(p[e[i].a], p[e[i].b]);
    sum += e[i].len;
  }
}

int getfa(int x) { return fa[x] == x ? fa[x] : fa[x] = getfa(fa[x]); }

void merge(int a, int b) {
  int f1 = getfa(a), f2 = getfa(b);
  fa[f1] = f2;
}

void work() {
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    if (getfa(e[i].a) != getfa(e[i].b))
      merge(e[i].a, e[i].b), mst += e[i].len;
  }
  printf("%.3lf\\n", sum - mst);
}

bool mmp(hhh x, hhh y) { return x.len > y.len; }

int main() {
  // fre();
  init();
  sort(e + 1, e + 1 + m, mmp);
  work();
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX_V 10000 + 10
double cost[MAX_V][MAX_V];
double mincost[MAX_V];
bool used[MAX_V];
int V;
pair<int, int> p[MAX_V];
double prim() {
  for (int i = 0; i < V; i++) {
    mincost[i] = INF;
    used[i] = false;
  }
  mincost[0] = 0;
  double res = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];
    // cout<<v<<endl;
    for (int u = 0; u < V; u++) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  return res;
}
double dis(int a, int b) {
  return sqrt((p[a].first - p[b].first) * (p[a].first - p[b].first) +
              (p[a].second - p[b].second) * (p[a].second - p[b].second));
}
int main() {
  // freopen("1.txt","r",stdin);
  int M;
  while (~scanf("%d%d", &V, &M)) {
    for (int i = 0; i < V; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      p[i].first = a;
      p[i].second = b;
    }
    double total = 0;
    for (int i = 0; i < M; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--;
      b--;
      cost[a][b] = cost[b][a] = -dis(a, b);
      total += -cost[b][a];
    }
    printf("%.3f", total + prim());
  }
  return 0;
}