High-precision algorithms for constructing optimal trajectories via solving Hamiltonian systems