Dynamic Optimization of Path-Constrained Switched Systems