Do you have any algorithm books? Your problem is roughly equivalent to the "traveling salesman" problem, which is a canonical example of an NP complete problem, that is, one which has no polynomial time solutions, only exponential ones.
You're going to have to settle for less than optimal to...