Combining optimal path search with task-dependent learning in a neural network