Spanning tree minimizing $F_T = \sum_{i = 1}^{|V| - 1|} (w(e_i) - P_T)^2$ Let $G = \langle V, E \rangle$ be an undirected, connected and weighted multigraph, with the weights given by a function $w: E \rightarrow N$. Consider any spanning tree $T$. Denote the edges of $T$ by $e_1, e_2, \ldots , e_{|V|-1}$. Define $P_T = \frac{\sum_{i = 1}^{|V| - 1|} w(e_i)}{|V| - 1}$, the arithmetic mean of weights of edges in $T$. Define $F_T = \sum_{i = 1}^{|V| - 1|} (w(e_i) - P_T)^2$. I want to find a spanning tree $T$ which minimizes $F_T$. Is there any known polynomial time algorithm for this problem?
UPD: What is the fastest algorithm that can solve it?
 A: The problem is equivalent to finding $\min_{e_i, \lambda} \sum (w(e_i) - \lambda)^2$, where $\lambda$ is a free real parameter subject to optimization as well as the edges of a spanning tree. Indeed, for a chosen set of edges $e_i$ we have that $\sum (w(e_i) - \lambda)^2$ is minimized when $\lambda$ is the mean of $w(e_i)$.
If $\lambda$ is fixed, we can use, say, Kruskal's algorithm which considers edges by increasing of $|w(e) - \lambda|$. Note that if we gradually increase $\lambda$, the order of edges only changes at $\lambda = (w(e) + w(e')) / 2$ for some edges $e, e'$, therefore there are polynomially many different orders. Run Kruskal's algorithm with respect to each of these orders and choose the best answer.
The complexity is roughly $O(|E|^3)$ if done straightforwardly. UPD: This approach can be optimized to $O(|E|^2 \log |V|)$, roughly as follows: when we process a "two edges $e_1$, $e_2$ switch their order to $e_2$, $e_1$" event, the result of the Kruskal algorithm changes iff all edges on the cycle of the current MST induced by $e_2$ are $\geq e_1$, then we have to delete $e_1$ and insert $e_2$. This can be done efficiently with link-cut trees.
A: It seems that Mikhail's nice answer will lead to a solution of the opening poster's problem. 
I would like to make one little precise point: 

Even in situations where the 'linear' problem of  'minimum-weight-spanning tree' has a unique solution, the problem of the opening poster (of finding a spanning tree with minimum quadratic deviation need not have a unique solution. (As proved by the following explicit counterexample, which probably is the 'smallest counterexample', in a reasonably loose sense of 'smallest'). 

Consider the undirected connected and (edge-)weigted multigraph (which happens to be a simple graph)


This completes the counterexample. 
