Problem:
Vector Compression You are recording a result of a secret experiment, which consists of a large set of N -dimensional vectors.
Since the result may become very large, you are thinking of
compressing it. Fortunately you already have a good compression method
for vectors with small absolute values, all you have to do is to
preprocess the vectors and make them small. You can record the set of vectors in any order you like.
Let's assume you process them in the order v_1 , v_2 ,..., v_M .
Each vector v_i is recorded either as is , or as a difference vector.
When it is recorded as a difference,
you can arbitrarily pick up an already chosen vector v_j (j<i) and a real value r .
Then the actual vector value recorded is (v_i - r v_j) .
The values of r and j do not affect the compression ratio so much, so you don't have to care about them. Given  a set of vectors,
your task is to write a program  that calculates the minimum sum of the squared length of the  recorded vectors. Input The input is like the following style.  N  M  v_{1,1}  v_{1,2}  ...  v_{1,N}  ...  v_{M,1}  v_{M,2}  ...  v_{M,N}  The first line contains two integers N and M ( 1 \leq N, M \leq 100 ),
where N is the dimension of each vector,
and M is the number of the vectors.
Each of the following M lines contains N floating point values v_{i,j} ( -1.0 \leq v_{i,j} \leq 1.0 )
which represents the j -th element value of the i -th vector. Output Output the minimum sum of the squared length of the  recorded vectors. 
The output should not contain an absolute error greater than 10^{-6} . Sample Input 1 2 3
1.0 1.0
-1.0 0.0
0.5 0.5 Output for the Sample Input 1 1.0 Sample Input 2 1 1
1.0 Output for the Sample Input 2 1.0 Sample Input 3 4 3
1.0 1.0 0.0 0.0
-1.0 0.0 -1.0 0.0
0.5 0.5 0.5 0.5 Output for the Sample Input 3 3.0

Solution (Python):
import sys
readline = sys.stdin.readline
write = sys.stdout.write

def calc(V, es, r):
    mins = [(10**18, -1)]*V
    for s, t, w in es:
        mins[t] = min(mins[t], (w, s))
    mins[r] = (-1, -1)

    group = [0]*V
    comp = [0]*V
    cnt = 0

    used = [0]*V
    for v in range(V):
        if not used[v]:
            chain = []
            cur = v
            while cur!=-1 and not used[cur]:
                chain.append(cur)
                used[cur] = 1
                cur = mins[cur][1]
            if cur!=-1:
                cycle = 0
                for e in chain:
                    group[e] = cnt
                    if e==cur:
                        cycle = 1
                        comp[cnt] = 1
                    if not cycle:
                        cnt += 1
                if cycle:
                    cnt += 1
            else:
                for e in chain:
                    group[e] = cnt
                    cnt += 1

    if cnt == V:
        return sum(map(lambda x:x[0], mins)) + 1

    res = sum(mins[v][0] for v in range(V) if v!=r and comp[group[v]])

    n_es = []
    for s, t, w in es:
        gs = group[s]; gt = group[t]
        if gs == gt:
            continue
        if comp[gt]:
            n_es.append((gs, gt, w - mins[t][0]))
        else:
            n_es.append((gs, gt, w))
    return res + calc(cnt, n_es, group[r])

def solve():
    N, M = map(int, readline().split())
    V = []
    D = []
    for i in range(M):
        *Vi, = map(float, readline().split())
        d = sum(e**2 for e in Vi)
        if d <= 1e-9:
            continue
        V.append(Vi)
        D.append(d)

    M = len(V)
    E = []
    for i in range(M):
        Vi = V[i]
        for j in range(M):
            if i == j:
                continue
            Vj = V[j]
            t = 0
            for k in range(N):
                t += Vi[k] * Vj[k]
            r = t / (D[j])
            c = 0
            for k in range(N):
                c += (Vi[k] - r*Vj[k])**2
            E.append((j+1, i+1, c))
        E.append((0, i+1, D[i]))
    write("%.16f\n" % calc(M+1, E, 0))
solve()