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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define eps 1e-12
#define INF 1000000
#define maxn 200005
double a[105][105];
double sum[105];
struct Edge {
  int u, v;
  double w;
} b[10005], e[10005];
int pre[maxn], id[maxn], vis[maxn];
double in[maxn];

int dcmp(double x) {
  if (fabs(x) < eps)
    return 0;
  return x < 0 ? -1 : 1;
}

double Directed_MST(int root, int NV, int NE) {
  double ret = 0;
  while (1) {
    for (int i = 0; i < NV; i++)
      in[i] = INF;
    memset(pre, -1, sizeof(pre));
    memset(vis, -1, sizeof(vis));
    memset(id, -1, sizeof(id));
    for (int i = 0; i < NE; i++) {
      if (e[i].u == e[i].v)
        continue;
      int v = e[i].v;
      if (dcmp(e[i].w - in[v]) < 0) {
        //          ans=i;
        pre[v] = e[i].u;
        in[v] = e[i].w;
      }
    }
    for (int i = 0; i < NV; i++) {
      if (dcmp(in[i] - INF) == 0 && i != root) {
        // cout<<i<<endl;
        return -1;
      }
    }
    int cnt = 0;
    in[root] = 0;
    for (int i = 0; i < NV; i++) {
      ret += in[i];
      int v = i;
      while (vis[v] != i && id[v] == -1 && v != root) {
        vis[v] = i;
        v = pre[v];
      }
      if (v != root && id[v] == -1) {
        for (int u = pre[v]; u != v; u = pre[u]) {
          id[u] = cnt;
        }
        id[v] = cnt++;
      }
    }
    if (cnt == 0)
      break;
    for (int i = 0; i < NV; i++)
      if (id[i] == -1)
        id[i] = cnt++;
    for (int i = 0; i < NE; i++) {
      int v = e[i].v;
      e[i].u = id[e[i].u];
      e[i].v = id[e[i].v];
      e[i].w -= in[v];
    }
    NV = cnt;
    root = id[root];
  }
  return ret;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    for (int j = 0; j < n; j++)
      scanf("%lf", a[i] + j);
  int tot = 0;
  for (int i = 1; i <= m; i++) // j->i??±?
  {
    for (int j = 1; j <= m; j++) {
      double x1 = 0, x2 = 0, x3 = 0; // y=r*r*x3-2*r*x2+x1
      for (int k = 0; k < n; k++) {
        x1 += a[i][k] * a[i][k];
        x3 += a[j][k] * a[j][k];
        x2 += a[i][k] * a[j][k];
      }
      sum[i] = x1;
      if (i == j)
        continue;
      double x = x2 / x3;
      double y = x * x * x3 - 2.0 * x * x2 + x1;
      e[tot].u = j;
      e[tot].v = i;
      e[tot].w = y;
      tot++;
    }
  }
  for (int i = 1; i <= m; i++) {
    e[tot].u = 0;
    e[tot].v = i;
    e[tot].w = sum[i];
    tot++;
  }
  double temp = Directed_MST(0, m + 1, tot);
  printf("%.7f\\n", temp);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define eps 1e-8
#define INF 1000000
#define maxn 200005
double a[105][105];
double sum[105];
struct Edge {
  int u, v;
  double w;
} b[10005], e[10005];
int pre[maxn], id[maxn], vis[maxn];
double in[maxn];

int dcmp(double x) {
  if (fabs(x) < eps)
    return 0;
  return x < 0 ? -1 : 1;
}

double Directed_MST(int root, int NV, int NE) {
  double ret = 0;
  while (1) {
    for (int i = 0; i < NV; i++)
      in[i] = INF;
    memset(pre, -1, sizeof(pre));
    memset(vis, -1, sizeof(vis));
    memset(id, -1, sizeof(id));
    for (int i = 0; i < NE; i++) {
      if (e[i].u == e[i].v)
        continue;
      int v = e[i].v;
      if (e[i].w < in[v]) {
        //          ans=i;
        pre[v] = e[i].u;
        in[v] = e[i].w;
      }
    }
    for (int i = 0; i < NV; i++) {
      if (in[i] == INF && i != root)
        return -1;
    }
    int cnt = 0;
    in[root] = 0;
    for (int i = 0; i < NV; i++) {
      ret += in[i];
      int v = i;
      while (vis[v] != i && id[v] == -1 && v != root) {
        vis[v] = i;
        v = pre[v];
      }
      if (v != root && id[v] == -1) {
        for (int u = pre[v]; u != v; u = pre[u]) {
          id[u] = cnt;
        }
        id[v] = cnt++;
      }
    }
    if (cnt == 0)
      break;
    for (int i = 0; i < NV; i++)
      if (id[i] == -1)
        id[i] = cnt++;
    for (int i = 0; i < NE; i++) {
      int v = e[i].v;
      e[i].u = id[e[i].u];
      e[i].v = id[e[i].v];
      e[i].w -= in[v];
    }
    NV = cnt;
    root = id[root];
  }
  return ret;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      scanf("%lf", a[i] + j);
  int tot = 0;
  for (int i = 0; i < m; i++) // j->i??±?
  {
    for (int j = 0; j < m; j++) {
      double x1 = 0, x2 = 0, x3 = 0; // y=r*r*x3-2*r*x2+x1
      for (int k = 0; k < n; k++) {
        x1 += a[i][k] * a[i][k];
        x3 += a[j][k] * a[j][k];
        x2 += a[i][k] * a[j][k];
      }
      sum[i] = x1;
      if (i == j)
        continue;
      double x = x2 / x3;
      double y = x * x * x3 - 2.0 * x * x2 + x1;
      b[tot].u = j;
      b[tot].v = i;
      b[tot].w = y;
      tot++;
    }
  }
  double fin = INF;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < tot; j++) {
      e[j].w = b[j].w;
      e[j].u = b[j].u;
      e[j].v = b[j].v;
    }
    double temp = Directed_MST(i, m, tot);
    if (temp < 0)
      while (1)
        ;
    temp = temp + sum[i];
    fin = min(fin, temp);
  }
  printf("%.7f\\n", fin);
}