Assume that there are many holes with their locations fixed, and the same number of balls distributed randomly. What is the smallest total distance for the balls fitting into the holes on the precondition that each hole can only fit one ball? For instance, the holes(black dots) are regularly distributed, and the balls(red dots) are randomly distributed. The nearest holes of the individual balls are indicated by arrows.
holes = Tuples[Range[1, 2, 1], 2];
balls = RandomReal[{1, 2}, Dimensions[holes]];
Graphics[{PointSize[Large], Point[holes], Red,PointSize[Medium],Point[balls]}]

Thanks for all the helps and answers. The problem is called 'The Euclidean matching problem' or 'Euclidean minimum weight matching problem' 1. I recently found an approximate alogrithm which achieves nearly O(n) time complexity [2].
1 http://dl.acm.org/citation.cfm?id=1882725&CFID=469610786&CFTOKEN=72872074
[2] A Near-Linear Constant-Factor Approximation for Euclidean Bipartite Matching





The total distance being

\[DirectedEdge]up there? – Oct 23 '14 at 15:53->instead of\[DirectedEdge]? – ybeltukov Oct 23 '14 at 18:29Sqrt@Total[Outer[Plus,balls,-holes,1]]^2,{3}]increase the speed? – ybeltukov Oct 23 '14 at 19:40edges = Flatten@Table[...]~Join~...it is instantaneous whilegraph = Graph[edges]is the slow part. – Oct 23 '14 at 20:19->works when constructing the graph, but not when pattern matching afterwards; it gets converted into\[DirectedEdge]. – Oct 23 '14 at 20:21AdjacencyGraph? Because you almost construct the adjacency matrix. Your solution is very good but I believe it is possible to make it even better. – ybeltukov Oct 23 '14 at 20:35