The rapid advancements in vehicular technologies have enabled modern autonomous vehicles (AVs) to perform complex tasks, such as augmented reality, real-time video surveillance, and automated parking. However, these applications require significant computational resources, which AVs often lack. To address this limitation, Vehicular Edge Computing (VEC) has emerged as a promising solution, allowing AVs to offload computational tasks to nearby vehicles and edge servers. This offloading process, however, is complicated by factors such as high vehicle mobility and intermittent connectivity. In this paper, we propose the Hungarian Algorithm for Task Offloading (HATO), a novel approach designed to optimize the distribution of computational tasks in 5G-enabled VEC systems. HATO leverages 5G’s low-latency, high-bandwidth communication to efficiently allocate tasks across edge servers and nearby vehicles, utilizing the Hungarian algorithm for optimal task assignment. By designating an edge server to gather contextual information from surrounding nodes and compute the best offloading scheme, HATO reduces computational burdens on AVs and minimizes task failures. Through extensive simulations in both urban and highway scenarios, HATO achieved a significant performance improvement, reducing execution time by up to 75.4% compared to existing methods under full 5G coverage in high-density environments. Additionally, HATO demonstrated zero energy constraint violations and achieved the highest task processing reliability, with an offloading success rate of 87.75% in high-density urban areas. These results highlight the potential of HATO to enhance the efficiency and scalability of VEC systems for autonomous vehicles.