Network slicing shows promise as a means to endow 5G networks with flexible and dynamic features. Network function virtualization (NFV) and software-defined networking (SDN) are the key methods for deploying network slicing, which will enable end-to-end (E2E) isolation services permitting each slice to be customized depending on service requirements. The goal of this investigation is to construct network slices through a machine learning algorithm and allocate resources for the newly created slices using dynamic programming in an efficient manner. A substrate network is constructed with a list of key performance indicators (KPIs) like CPU capacity, bandwidth, delay, link capacity, and security level. After that, network slices are produced by employing multi-layer perceptron (MLP) using the adaptive moment estimation (ADAM) optimization algorithm. For each requested service, the network slices are categorized as massive machine-type communications (mMTC), enhanced mobile broadband (eMBB), and ultra-reliable low-latency communications (uRLLC). After network slicing, resources are provided to the services that have been requested. In order to maximize the total user access rate and resource efficiency, Dijkstra’s algorithm is adopted for resource allocation that determines the shortest path between nodes in the substrate network. The simulation output shows that the present model allocates optimum slices to the requested services with high resource efficiency and reduced total bandwidth utilization.