<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://www.33dai.wiki/index.php?action=history&amp;feed=atom&amp;title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%2F06-priority_queue</id>
	<title>04-数据结构/06-priority queue - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://www.33dai.wiki/index.php?action=history&amp;feed=atom&amp;title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%2F06-priority_queue"/>
	<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/06-priority_queue&amp;action=history"/>
	<updated>2026-05-20T23:07:28Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/06-priority_queue&amp;diff=341&amp;oldid=prev</id>
		<title>33DAI：​导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/06-priority_queue&amp;diff=341&amp;oldid=prev"/>
		<updated>2026-05-20T18:12:10Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 18:12的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key my_wiki:diff:1.41:old-340:rev-341 --&gt;
&lt;/table&gt;</summary>
		<author><name>33DAI</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/06-priority_queue&amp;diff=340&amp;oldid=prev</id>
		<title>-&gt;Importer：​批量导入三三文档</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/06-priority_queue&amp;diff=340&amp;oldid=prev"/>
		<updated>2026-05-20T16:50:33Z</updated>

		<summary type="html">&lt;p&gt;批量导入三三文档&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 16:50的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>-&gt;Importer</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/06-priority_queue&amp;diff=241&amp;oldid=prev</id>
		<title>33DAI：​导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/06-priority_queue&amp;diff=241&amp;oldid=prev"/>
		<updated>2026-05-20T16:25:28Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 16:25的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key my_wiki:diff:1.41:old-240:rev-241 --&gt;
&lt;/table&gt;</summary>
		<author><name>33DAI</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/06-priority_queue&amp;diff=240&amp;oldid=prev</id>
		<title>-&gt;Importer：​批量导入三三文档</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/06-priority_queue&amp;diff=240&amp;oldid=prev"/>
		<updated>2026-05-20T16:22:29Z</updated>

		<summary type="html">&lt;p&gt;批量导入三三文档&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
优先队列每次取出的都是优先级最高（默认最大）的元素。&lt;br /&gt;
&lt;br /&gt;
=== 定义 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
priority_queue&amp;lt;int&amp;gt; pq;                       // 大顶堆（默认）&lt;br /&gt;
priority_queue&amp;lt;int, vector&amp;lt;int&amp;gt;, greater&amp;lt;int&amp;gt;&amp;gt; pq; // 小顶堆&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 常用操作 ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! 操作 !! 说明&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;pq.push(x)&amp;lt;/code&amp;gt;&lt;br /&gt;
| 插入元素，自动维护顺序&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;pq.pop()&amp;lt;/code&amp;gt;&lt;br /&gt;
| 弹出优先级最高的元素&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;pq.top()&amp;lt;/code&amp;gt;&lt;br /&gt;
| 返回优先级最高的元素&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;pq.size()&amp;lt;/code&amp;gt;&lt;br /&gt;
| 返回元素数量&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;pq.empty()&amp;lt;/code&amp;gt;&lt;br /&gt;
| 判断是否为空&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== 小顶堆的两种写法 ===&lt;br /&gt;
&lt;br /&gt;
==== 方法一：传相反数 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
priority_queue&amp;lt;int&amp;gt; pq;&lt;br /&gt;
pq.push(-x);           // 存相反数&lt;br /&gt;
int val = -pq.top();   // 取回时再取反&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 方法二：使用 &amp;lt;code&amp;gt;greater&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
priority_queue&amp;lt;int, vector&amp;lt;int&amp;gt;, greater&amp;lt;int&amp;gt;&amp;gt; pq;&lt;br /&gt;
pq.push(x);&lt;br /&gt;
int val = pq.top();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 结构体优先队列 ===&lt;br /&gt;
&lt;br /&gt;
当元素是结构体时，需要定义比较规则：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
struct Student&lt;br /&gt;
{&lt;br /&gt;
    int score;&lt;br /&gt;
    string name;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// 重载 &amp;lt; 运算符（注意：priority_queue 默认用 &amp;lt; 判断，但取的是&amp;quot;最大&amp;quot;的）&lt;br /&gt;
// 如果想按分数从小到大取，就定义 a.score &amp;gt; b.score（反过来）&lt;br /&gt;
bool operator&amp;lt;(const Student &amp;amp;a, const Student &amp;amp;b)&lt;br /&gt;
{&lt;br /&gt;
    return a.score &amp;gt; b.score; // 分数小的优先级高&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
priority_queue&amp;lt;Student&amp;gt; pq;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 自定义比较器 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
struct cmp&lt;br /&gt;
{&lt;br /&gt;
    bool operator()(const int &amp;amp;a, const int &amp;amp;b)&lt;br /&gt;
    {&lt;br /&gt;
        return a &amp;gt; b; // 变成小顶堆&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
priority_queue&amp;lt;int, vector&amp;lt;int&amp;gt;, cmp&amp;gt; pq;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 应用场景 ===&lt;br /&gt;
&lt;br /&gt;
- Dijkstra 最短路径算法&lt;br /&gt;
- 合并果子（哈夫曼编码）&lt;br /&gt;
- 数据流中的中位数&lt;br /&gt;
- 贪心算法中取最优选择&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:数据结构]]&lt;br /&gt;
[[Category:三三文档]]&lt;/div&gt;</summary>
		<author><name>-&gt;Importer</name></author>
	</entry>
</feed>