Optimal Trajectory Planning and Train Scheduling for Railway Systems