什么时候使用优先级队列? [英] When would I use a priority queue?

查看:82
本文介绍了什么时候使用优先级队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道的使用优先级队列的唯一示例是Dijkstra的算法(用于计算最低成本)

The only example of using the priority queue I know of, is the Dijkstra's Algorithm (for calculating minimum cost)

在其他情况下,它还会有用吗?

In what other situations would it be useful?

推荐答案

以下是一个实际示例-对于商业应用程序:

Here's a practical example - for a business application:

您'正在开一家医院,病人正在进来。工作人员只有一名医生。第一个男人走进来-他立即被送达。接下来,一个感冒的男人进来并需要帮助。您将他添加到队列中,他排队等候医生可用。接下来,一个脑袋里有斧头的男人穿过门。他被赋予较高的优先权,因为他的医疗责任更高。因此,感冒的人被撞倒了。接下来,有人出现呼吸困难。因此,再次感冒的人被优先撞倒。在现实世界中这被称为分诊-但在这种情况下,这是一条医疗电话。

You're running a hospital and patients are coming in. There's only one doctor on staff. The first man walks in - and he's served immediately. Next, a man with a cold comes in and requires assistance. You add him to the queue and he waits in line for the doctor to become available. Next, a man with an axe in his head comes through the door. He is assigned a higher priority because he has a higher medical liability. So the man with the cold is bumped down in line. Next, someone comes in with breathing problems. So, once again, the man with the cold is bumped down in priority. This is called triaging in the real world - but in this case it's a medical line.

在代码中实现此操作将使用优先级队列和工作线程(医生)对消耗品/工作单位(患者)进行工作

Implementing this in code would use a priority queue and a worker thread (the doctor) to perform work on the consumable / units of work (the patients)

这篇关于什么时候使用优先级队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆