This paper presents a fifth-order weighted compact nonlinear scheme (WCNS) enhanced with machine learning for the simulation of compressible flows. The scheme is designed to achieve robust shock capturing and low dissipation while attaining unconditionally optimal high-order accuracy. This approach extends the fundamental ideas of Bezgin et al. [J. Comput. Phys. 452, 110920 (2022)] and introduces improvements to the training strategy. By incorporating an artificial neural network that can automatically identify local flow features and integrating critical-point detection into the network, the scheme is able to dynamically optimize interpolation-weight selection, ensuring the desired order of accuracy at any critical points. Several benchmark tests demonstrate that the proposed scheme outperforms traditional schemes in terms of accuracy and spectral resolution, exhibits strong generalization across various grid resolutions, and effectively captures shocks while maintaining high-resolution properties.