As traditional cloud computing is not efficient enough to support large-scale computational task execution in IoT environments, a task offloading and resource allocation algorithm for mobile edge computing (MEC) is proposed in this paper. First, a multiuser computation offloading model is constructed, including a communication model and computation offloading model, which is transformed into the minimization of users’ time delay and energy consumption (i.e., total system overhead) in the MEC system. Then, the task offloading model is formulated into a Markov decision process, and an offloading strategy based on a deep Q network (DQN) is designed to dynamically make fine tunings on the offloading proportion of each user so as to realize a low-cost MEC system. The proposed algorithm is analyzed based on the constructed simulation platform. The simulation results show that when the number of user terminals is 40, the average delay of the proposed algorithm does not exceed 0.9 s, and the average energy consumption tends to 65 J, which is better than the comparison method. Therefore, the proposed algorithm has certain application prospects.