22 lines
393 B
Python
22 lines
393 B
Python
|
|
#!/usr/bin/env python
|
||
|
|
# -*- encoding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
Topic: 优先级队列
|
||
|
|
Desc :
|
||
|
|
"""
|
||
|
|
import heapq
|
||
|
|
|
||
|
|
|
||
|
|
class PriorityQueue:
|
||
|
|
def __init__(self):
|
||
|
|
self._queue = []
|
||
|
|
self._index = 0
|
||
|
|
|
||
|
|
def push(self, item, priority):
|
||
|
|
heapq.heappush(self._queue, (-priority, self._index, item))
|
||
|
|
self._index += 1
|
||
|
|
|
||
|
|
def pop(self):
|
||
|
|
return heapq.heappop(self._queue)[-1]
|
||
|
|
|