﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-ACM___________________________-随笔分类-ACM_资料</title><link>http://www.cppblog.com/MiYu/category/14430.html</link><description>                             ______________白白の屋</description><language>zh-cn</language><lastBuildDate>Fri, 12 Nov 2010 13:35:26 GMT</lastBuildDate><pubDate>Fri, 12 Nov 2010 13:35:26 GMT</pubDate><ttl>60</ttl><item><title>Splay Tree 介绍</title><link>http://www.cppblog.com/MiYu/archive/2010/11/12/133404.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 12 Nov 2010 03:13:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/11/12/133404.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/133404.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/11/12/133404.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/133404.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/133404.html</trackback:ping><description><![CDATA[<span  style="font-size: 12px; "><div style="padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; "><pre style="font-family: 'Courier New'; text-align: left; "><span  style="font-family: verdana, 'courier new'; white-space: normal; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://baiyun.me/"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</span></pre><pre style="font-family: 'Courier New'; text-align: left; "><span  style="font-family: verdana, 'courier new'; white-space: normal; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span></pre><pre style="font-family: 'Courier New'; text-align: left; ">伸展树（Splay Tree）是一种二叉排序树，它能在O<strong><font color="#FF00FF">(</font></strong>log n<strong><font color="#FF00FF">)</font></strong>内完成插入、查找和删除操作。它由Daniel Sleator和Robert Tarjan创造。它的优势在于不需要记录用于平衡树的冗余信息。在伸展树上的一般操作都基于伸展操作。
查找树的相关知识
　　各种查找树存在不足。比如：对于一个有n个节点的平衡树，虽然最坏情况下每次查找的时间复杂度不会超过O<strong><font color="#FF00FF">(</font></strong>logn<strong><font color="#FF00FF">)</font></strong>，但是如果访问模式不均匀，平衡树的效率就会受到影响。此外，它们还需要额外的空间来存储平衡信息。 
　　这些查找树的设计目标都是减少最坏情况下单次操作时间，但是查找树的典型应用经常需要执行一系列的查找操作，此时更关心的性能指标是所有这些操作总共需要多少时间。对于此类应用，更好的目标就是降低操作的摊平时间，此处的摊平时间是指在一系列最坏情况的操作序列中单次操作的平均时间。获得摊平效率的一种方法 就是使用&#8220;自调整&#8221;的数据结构。 
　　和平衡的或是其它对结构有明确限制的数据结构比起来，自调整数据结构有以下几个优点：<font color="#CC3300"> 
　　1</font>、从摊平角度而言，它们忽略常量因子，因此绝对不会比有明确限制的数据结构差。而且由于它们可以根据使用情况进行调整，于是在使用模式不均匀的情况下更加有效。<font color="#CC3300"> 
　　2</font>、由于无需存储平衡或者其它的限制信息，它们所需的空间更小。<font color="#CC3300"> 
　　3</font>、它们的查找和更新算法概念简单，易于实现。 
　　当然，自调整结构也有潜在的缺点：<font color="#CC3300"> 
　　1</font>、它们需要更多的局部调整，尤其是在查找期间。（那些有明确限制的数据结构仅需在更新期间进行调整，查找期间则不用）<font color="#CC3300"> 
　　2</font>、一系列查找操作中的某一个可能会耗时较长，这在实时应用程序中可能是个不足之处。
伸展树存在的意义
　　假设想要对一个二叉查找树执行一系列的查找操作。为了使整个查找时间更小，被查频率高的那些条目就应当经常处于靠近树根的位置。于是想到设计一个简单方法， 在每次查找之后对树进行重构，把被查找的条目搬移到离树根近一些的地方。splay tree应运而生。splay tree是一种自调整形式的二叉查找树，它会沿着从某个节点到树根之间的路径，通过一系列的旋转把这个节点搬移到树根去。
已知重构方法与伸展树的重构方法
　　先前，已经存在两种重构方法：<font color="#CC3300"> 
　　1</font>、单旋：在查找完位于节点x中的条目i之后，旋转链接x和其父节点的边。（除非x就是树根）<font color="#CC3300"> 
　　2</font>、搬移至树根：在查找完位于节点x中的条目i之后，旋转链接x和其父节点的边，然后重复这个操作直至x成为树根。 
　　splay tree的重构方法和搬移至树根的方法相似，它也会沿着查找路径做自底向上的旋转，将被查找条目移至树根。但不同的是，它的旋转是成对进行的，顺序取决于查找路径的结构。为了在节点x处对树进行splay操作，我们需要重复下面的步骤，直至x成为树根为止：<font color="#CC3300"> 
　　1</font>、第一种情况：如果x的父节点p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>是树根，则旋转连接x和p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>的边。（这种情况是最后一步）<font color="#CC3300"> 
　　2</font>、第二种情况：如果p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>不是树根，而且x和p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>本身都是左孩子或者都是右孩子，则先旋转连接p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>和x的祖父节点g<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>的边，然后再旋转连接x和p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>的边。<font color="#CC3300"> 
　　3</font>、第三种情况：如果p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>不是树根，而且x是左孩子，p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>是右孩子，或者相反，则先旋转连接x和p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>的边，再旋转连接x和新的p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>的边。 
　　在节点x处进行splay操作的时间是和查找x所需的时间成比例的。splay操作不单是把x搬移到了树根，而且还把查找路径上的每个节点的深度都大致减掉了一半。
伸展树（Splay Tree）支持的操作
　　具体操作包括：<font color="#CC3300"> 
　　1</font>、access<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">):</font></strong>如果i在树t中，则返回指向它的指针，否则返回空指针。为了实现access<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，可以从树t的根部向下查找i。如果 查找操作遇到了一个含有i的节点x，就在x处进行splay操作，并返回指向x的指针，访问结束。如果遇到了空指针，表示i不在树中，此时就在最后一个非 空节点处进行splay操作，然后返回空指针。如果树是空的，将忽略掉splay操作。<font color="#CC3300"> 
　　2</font>、insert<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">):</font></strong>将条目i插入树t中（假设其尚不存在）。为了实现insert<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，首先执行split<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，然后把t换成一个由新的包含有i的根节点组成的树，这个根节点的左右子树分别是split返回的树t1和t2。<font color="#CC3300"> 
　　3</font>、<strong><font color="#0000FF">delete</font></strong><strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">):</font></strong>从树t中删除条目i（假设其已经存在）。为了实现<strong><font color="#0000FF">delete</font></strong><strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，首先执行access<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，然后把t换成其左子树和右子树join之后的新树。<font color="#CC3300"> 
　　4</font>、join<strong><font color="#FF00FF">(</font></strong>t1<strong><font color="#FF00FF">,</font></strong>t2<strong><font color="#FF00FF">):</font></strong>将树t1和t2合并成一棵树，其中包含之前两棵树的所有条目，并返回合并之后的树。这个操作假设t1中的所有条目都小于t2 中的条目，操作完成之后会销毁t1和t2。为了实现join<strong><font color="#FF00FF">(</font></strong>t1<strong><font color="#FF00FF">,</font></strong>t2<strong><font color="#FF00FF">)</font></strong>，首先访问t1中最大的条目i。访问结束之后，t1的根节点中包含的就是i，它 的右孩子显然为空。于是把t2作为这个根节点的右子树并返回完成之后的新树即可实现join操作。<font color="#CC3300"> 
　　5</font>、split<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">):</font></strong>构建并返回两棵树t1和t2，其中t1包含t中所有小于等于i的条目，t2包含t中所有大于i的条目。操作完成之后销毁t。为 了实现split<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，首先执行access<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，然后根据新根节点中的值是大于还是小于等于i来切断这个根节点的左链接或右链接，并返回形 成的两棵树。 
　　另外insert和<strong><font color="#0000FF">delete</font></strong>方法有更好的实现，时间复杂度更小：<font color="#CC3300"> 
　　1</font>、insert<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong> t<strong><font color="#FF00FF">):</font></strong>查找i，把遇到的空指针替换成一个含有i的新节点，然后再在新节点处对树进行splay操作。<font color="#CC3300"> 
　　2</font>、<strong><font color="#0000FF">delete</font></strong><strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong> t<strong><font color="#FF00FF">):</font></strong>查找含有i的节点，设此节点为x，其父节点为y。把x的左右子树合并之后替换掉x，然后再从y处进行splay操作。
伸展树的优势
　　由于Splay Tree仅仅是不断调整，并没有引入额外的标记，因而树结构与标准BST没有任何不同，从空间角度来看，它比Treap、Red<strong><font color="#FF00FF">-</font></strong>Black Tree、AVL要高效得多。因为结构不变，因此只要是通过左旋和右旋进行的操作对Splay Tree性质都没有丝毫影响，因而它也提供了BST中最丰富的功能，包括快速的拆分和合并（这里指的是将原树拆分成两棵子树，其中一棵子树所有节点都比另一子树小，以及它的逆过程），并且实现极为便捷。这一点是其它结构较难实现的。其时间效率也相当稳定，和Treap基本相当</pre></div></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/133404.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-11-12 11:13 <a href="http://www.cppblog.com/MiYu/archive/2010/11/12/133404.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)伸展树 ( Splay tree )</title><link>http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 12 Nov 2010 03:13:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/133405.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/133405.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/133405.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;伸展树(Splay Tree)是AVL树不错的替代，它有以下几个特点：(1)它是二叉查找树的改进，所以具有二叉查找树的有序性。(2)对伸展树的操作的平摊复杂度是O(log2n)。(3)伸展树的空间要求、编程难度非常低。提到伸展树，就不得不提到AVL树和R...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/133405.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-11-12 11:13 <a href="http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>树状数组 （Binary Indexed Trees ）论文解析</title><link>http://www.cppblog.com/MiYu/archive/2010/10/29/131711.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 29 Oct 2010 01:47:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/29/131711.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/131711.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/29/131711.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/131711.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/131711.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: By&nbsp;boba5551TopCoder MemberIntroductionWe often need some sort of data structure to make our algorithms faster. In this article we willdiscuss the&nbsp;Binary Indexed Trees&nbsp;structure. Accordi...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/10/29/131711.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/131711.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-29 09:47 <a href="http://www.cppblog.com/MiYu/archive/2010/10/29/131711.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU 1512 HDOJ 1512 Monkey King ( 左偏树 ) ACM 1512 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/10/24/131043.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sun, 24 Oct 2010 03:43:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/24/131043.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/131043.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/24/131043.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/131043.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/131043.html</trackback:ping><description><![CDATA[<p>MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://baiyun.me/"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p>&nbsp;</p><p>题目地址:</p><p><a href="http://acm.hdu.edu.cn/showproblem.php?pid=1512">http://acm.hdu.edu.cn/showproblem.php?pid=1512</a></p><p>题目描述 :</p><div class="cnblogs_code" onclick="cnblogs_code_show('93ce2b86-12de-4b7c-b5b1-c014e2356326')"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" class="code_img_closed" id="code_img_closed_93ce2b86-12de-4b7c-b5b1-c014e2356326" alt=""><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_93ce2b86-12de-4b7c-b5b1-c014e2356326" onclick="cnblogs_code_hide('93ce2b86-12de-4b7c-b5b1-c014e2356326',event)" style="display:none"><span class="cnblogs_code_collapse">代码</span><div id="cnblogs_code_open_93ce2b86-12de-4b7c-b5b1-c014e2356326" class="cnblogs_code_hide"><div><!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><span style="color: #000000;">Monkey&nbsp;King<br><br>Time&nbsp;Limit:&nbsp;</span><span style="color: #800080;">10000</span><span style="color: #000000;">/</span><span style="color: #800080;">5000</span><span style="color: #000000;">&nbsp;MS&nbsp;(Java</span><span style="color: #000000;">/</span><span style="color: #000000;">Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory&nbsp;Limit:&nbsp;</span><span style="color: #800080;">65536</span><span style="color: #000000;">/</span><span style="color: #800080;">32768</span><span style="color: #000000;">&nbsp;K&nbsp;(Java</span><span style="color: #000000;">/</span><span style="color: #000000;">Others)<br>Total&nbsp;Submission(s):&nbsp;</span><span style="color: #800080;">914</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;Accepted&nbsp;Submission(s):&nbsp;</span><span style="color: #800080;">426</span><span style="color: #000000;"><br><br><br>Problem&nbsp;Description<br>Once&nbsp;</span><span style="color: #0000FF;">in</span><span style="color: #000000;">&nbsp;a&nbsp;forest,&nbsp;there&nbsp;lived&nbsp;N&nbsp;aggressive&nbsp;monkeys.&nbsp;At&nbsp;the&nbsp;beginning,&nbsp;they&nbsp;each&nbsp;does&nbsp;things&nbsp;</span><span style="color: #0000FF;">in</span><span style="color: #000000;">&nbsp;its&nbsp;own&nbsp;way&nbsp;and&nbsp;none&nbsp;of&nbsp;them&nbsp;knows&nbsp;each&nbsp;other.&nbsp;But&nbsp;monkeys&nbsp;can</span><span style="color: #800000;">'</span><span style="color: #800000;">t&nbsp;avoid&nbsp;quarrelling,&nbsp;and&nbsp;it&nbsp;only&nbsp;happens&nbsp;between&nbsp;two&nbsp;monkeys&nbsp;who&nbsp;does&nbsp;not&nbsp;know&nbsp;each&nbsp;other.&nbsp;And&nbsp;when&nbsp;it&nbsp;happens,&nbsp;both&nbsp;the&nbsp;two&nbsp;monkeys&nbsp;will&nbsp;invite&nbsp;the&nbsp;strongest&nbsp;friend&nbsp;of&nbsp;them,&nbsp;and&nbsp;duel.&nbsp;Of&nbsp;course,&nbsp;after&nbsp;the&nbsp;duel,&nbsp;the&nbsp;two&nbsp;monkeys&nbsp;and&nbsp;all&nbsp;of&nbsp;there&nbsp;friends&nbsp;knows&nbsp;each&nbsp;other,&nbsp;and&nbsp;the&nbsp;quarrel&nbsp;above&nbsp;will&nbsp;no&nbsp;longer&nbsp;happens&nbsp;between&nbsp;these&nbsp;monkeys&nbsp;even&nbsp;if&nbsp;they&nbsp;have&nbsp;ever&nbsp;conflicted.</span><span style="color: #800000;"><br></span><span style="color: #000000;"><br>Assume&nbsp;that&nbsp;every&nbsp;money&nbsp;has&nbsp;a&nbsp;strongness&nbsp;value,&nbsp;which&nbsp;will&nbsp;be&nbsp;reduced&nbsp;to&nbsp;only&nbsp;half&nbsp;of&nbsp;the&nbsp;original&nbsp;after&nbsp;a&nbsp;duel(that&nbsp;</span><span style="color: #0000FF;">is</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800080;">10</span><span style="color: #000000;">&nbsp;will&nbsp;be&nbsp;reduced&nbsp;to&nbsp;</span><span style="color: #800080;">5</span><span style="color: #000000;">&nbsp;and&nbsp;</span><span style="color: #800080;">5</span><span style="color: #000000;">&nbsp;will&nbsp;be&nbsp;reduced&nbsp;to&nbsp;</span><span style="color: #800080;">2</span><span style="color: #000000;">).<br><br>And&nbsp;we&nbsp;also&nbsp;assume&nbsp;that&nbsp;every&nbsp;monkey&nbsp;knows&nbsp;himself.&nbsp;That&nbsp;</span><span style="color: #0000FF;">is</span><span style="color: #000000;">,&nbsp;when&nbsp;he&nbsp;</span><span style="color: #0000FF;">is</span><span style="color: #000000;">&nbsp;the&nbsp;strongest&nbsp;one&nbsp;</span><span style="color: #0000FF;">in</span><span style="color: #000000;">&nbsp;all&nbsp;of&nbsp;his&nbsp;friends,&nbsp;he&nbsp;himself&nbsp;will&nbsp;go&nbsp;to&nbsp;duel.<br>&nbsp;<br><br>Input<br>There&nbsp;are&nbsp;several&nbsp;test&nbsp;cases,&nbsp;and&nbsp;each&nbsp;</span><span style="color: #0000FF;">case</span><span style="color: #000000;">&nbsp;consists&nbsp;of&nbsp;two&nbsp;parts.<br><br>First&nbsp;part:&nbsp;The&nbsp;first&nbsp;line&nbsp;contains&nbsp;an&nbsp;integer&nbsp;N(N</span><span style="color: #000000;">&lt;=</span><span style="color: #800080;">100</span><span style="color: #000000;">,</span><span style="color: #800080;">000</span><span style="color: #000000;">),&nbsp;which&nbsp;indicates&nbsp;the&nbsp;number&nbsp;of&nbsp;monkeys.&nbsp;And&nbsp;then&nbsp;N&nbsp;lines&nbsp;follows.&nbsp;There&nbsp;</span><span style="color: #0000FF;">is</span><span style="color: #000000;">&nbsp;one&nbsp;number&nbsp;on&nbsp;each&nbsp;line,&nbsp;indicating&nbsp;the&nbsp;strongness&nbsp;value&nbsp;of&nbsp;ith&nbsp;monkey(</span><span style="color: #000000;">&lt;=</span><span style="color: #800080;">32768</span><span style="color: #000000;">).<br><br>Second&nbsp;part:&nbsp;The&nbsp;first&nbsp;line&nbsp;contains&nbsp;an&nbsp;integer&nbsp;M(M</span><span style="color: #000000;">&lt;=</span><span style="color: #800080;">100</span><span style="color: #000000;">,</span><span style="color: #800080;">000</span><span style="color: #000000;">),&nbsp;which&nbsp;indicates&nbsp;there&nbsp;are&nbsp;M&nbsp;conflicts&nbsp;happened.&nbsp;And&nbsp;then&nbsp;M&nbsp;lines&nbsp;follows,&nbsp;each&nbsp;line&nbsp;of&nbsp;which&nbsp;contains&nbsp;two&nbsp;integers&nbsp;x&nbsp;and&nbsp;y,&nbsp;indicating&nbsp;that&nbsp;there&nbsp;</span><span style="color: #0000FF;">is</span><span style="color: #000000;">&nbsp;a&nbsp;conflict&nbsp;between&nbsp;the&nbsp;Xth&nbsp;monkey&nbsp;and&nbsp;Yth.<br><br>&nbsp;<br><br>Output<br>For&nbsp;each&nbsp;of&nbsp;the&nbsp;conflict,&nbsp;output&nbsp;</span><span style="color: #000000;">-</span><span style="color: #800080;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000FF;">if</span><span style="color: #000000;">&nbsp;the&nbsp;two&nbsp;monkeys&nbsp;know&nbsp;each&nbsp;other,&nbsp;otherwise&nbsp;output&nbsp;the&nbsp;strongness&nbsp;value&nbsp;of&nbsp;the&nbsp;strongest&nbsp;monkey&nbsp;</span><span style="color: #0000FF;">in</span><span style="color: #000000;">&nbsp;all&nbsp;friends&nbsp;of&nbsp;them&nbsp;after&nbsp;the&nbsp;duel.<br>&nbsp;<br><br>Sample&nbsp;Input<br></span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #800080;">20</span><span style="color: #000000;"><br></span><span style="color: #800080;">16</span><span style="color: #000000;"><br></span><span style="color: #800080;">10</span><span style="color: #000000;"><br></span><span style="color: #800080;">10</span><span style="color: #000000;"><br></span><span style="color: #800080;">4</span><span style="color: #000000;"><br></span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #800080;">2</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">3</span><span style="color: #000000;"><br></span><span style="color: #800080;">3</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">4</span><span style="color: #000000;"><br></span><span style="color: #800080;">3</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #800080;">4</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #800080;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">5</span><span style="color: #000000;"><br>&nbsp;<br><br>Sample&nbsp;Output<br></span><span style="color: #800080;">8</span><span style="color: #000000;"><br></span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #000000;">-</span><span style="color: #800080;">1</span><span style="color: #000000;"><br></span><span style="color: #800080;">10</span><span style="color: #000000;"><br>&nbsp;<br></span></div></div></div><p>&nbsp;</p><p>&nbsp;</p><p>题目分析:</p><div class="cnblogs_code"><div><font  color="#800080"><div><div>/*</div><div>Mail to &nbsp; : miyubai@gamil.com</div><div>My Blog &nbsp; : www.baiyun.me</div><div>Link &nbsp; &nbsp; &nbsp;: http://www.cnblogs.com/MiYu &nbsp;|| http://www.cppblog.com/MiYu</div><div>Author By : MiYu</div><div>Test &nbsp; &nbsp; &nbsp;: 1</div><div>Complier &nbsp;: g++ mingw32-3.4.2</div><div>Program &nbsp; : HDU_1512</div><div>Doc Name &nbsp;: Monkey King</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>题目意思:&nbsp;</div><div><br></div><div>有N只猴子, 每只都有一个力量值. 开始的时候互不认识, 它们之间会发生M次斗争. 每次发生a, b的斗争时, a, b都会从各自的朋友圈里拉出一个最强的, 之后两只猴子打, 打完后这两只猴子的力量值各减半. 并且打完后, 两只猴子的朋友圈的所有人都互相认识(也就是不会再打).</div><div><br></div><div>你的任务就是对于每个斗争, 若a, b是朋友, 那么输出-1, 否则输出打完后它们的朋友圈的最强猴子的力量值.</div><div><br></div><div>&nbsp;使用 普通 优先队列的话 估计会超时, 因为数据量很大 100000 ! !, 等下有空试试看.&nbsp;</div><div><br></div><div>对于每一个节点, 定义dis 表示X节点到最右边的空节点的距离的最小值</div><div><br></div><div>对于每个节点X, 要求X的左儿子的dis &gt;= 右儿子的dis, 那么容易发现, 对于N个节点的左偏树, 其右儿子最多只有logN个节点.</div><div><br></div><div>合并操作就是让复杂度落在右儿子上, 从而达到logN的合并复杂度.</div><div><br></div><div>首先对于两个堆, 若其中一个为空, 返回另一个.</div><div><br></div><div>否则(这里以大根堆为例), a指向堆顶较大的堆, b指向另一个. 让a的右儿子和b合并, 合并后的子树作为a的右儿子.</div><div><br></div><div>接下来, 检查a的两个儿子是否满足dis, 不满足就交换两个儿子.</div><div><br></div><div>最后, 更新a的dis.</div><div><br></div><div>这样就容易实现堆的其他操作 ( 比如插入, 删除顶等 ).</div><div><br></div><div>另外 还需要用到 并查集. &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>*/</div><div>//#pragma warning( disable:4789 )</div><div>#include &lt;iostream&gt;</div><div>#include &lt;fstream&gt;</div><div>#include &lt;sstream&gt;</div><div>#include &lt;algorithm&gt;</div><div>#include &lt;string&gt;</div><div>#include &lt;set&gt;</div><div>#include &lt;map&gt;</div><div>#include &lt;utility&gt;</div><div>#include &lt;queue&gt;</div><div>#include &lt;stack&gt;</div><div>#include &lt;list&gt;</div><div>#include &lt;vector&gt;</div><div>#include &lt;cstdio&gt;</div><div>#include &lt;cstdlib&gt;</div><div>#include &lt;cstring&gt;</div><div>#include &lt;cmath&gt;</div><div>#include &lt;ctime&gt;</div><div>using namespace std;</div><div>const int MM = 100010;</div><div>struct left {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;int l,r,dis,val,dad;</div><div>} heap[MM];</div><div><br></div><div>int N, M;</div><div><br></div><div>inline int max ( const int &amp;a, const int &amp;b) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; return a &gt; b ? a : b;</div><div>}</div><div><br></div><div>inline int find ( int &amp;x ) {</div><div>&nbsp;&nbsp; &nbsp;return heap[x].dad == x ? x : heap[x].dad = find ( heap[x].dad );</div><div>}</div><div><br></div><div>inline void swap(int &amp;a, int &amp;b) {</div><div>&nbsp;&nbsp; &nbsp; a ^= b ^= a ^= b;</div><div>}</div><div><br></div><div>inline int merge ( int x, int y ) {</div><div>&nbsp;&nbsp; &nbsp;if ( x == 0 ) return y;</div><div>&nbsp;&nbsp; &nbsp;if ( y == 0 ) return x;</div><div>&nbsp;&nbsp; &nbsp;if ( heap[y].val &gt; heap[x].val ) swap ( x, y ); &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;heap[x].r = merge ( heap[x].r, y );</div><div>&nbsp;&nbsp; &nbsp;heap[heap[x].r].dad = x;</div><div>&nbsp;&nbsp; &nbsp;if ( heap[ heap[x].l ].dis &lt; heap[ heap[x].r ].dis )&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; swap ( heap[x].l, heap[x].r );</div><div>&nbsp;&nbsp; &nbsp;if ( heap[x].r == 0 ) heap[x].dis = 0;</div><div>&nbsp;&nbsp; &nbsp;else heap[x].dis = heap[ heap[x].r ].dis + 1;</div><div>&nbsp;&nbsp; &nbsp;return x;</div><div>}</div><div><br></div><div>inline int push ( int x, int y ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; return merge ( x, y ); &nbsp; &nbsp; &nbsp;&nbsp;</div><div>}</div><div><br></div><div>inline int pop ( int &amp;x ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; int l = heap[x].l;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; int r = heap[x].r;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; heap[l].dad = l;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; heap[r].dad = r;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; heap[x].l = heap[x].r = heap[x].dis = 0; &nbsp;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; return merge ( l, r ); &nbsp;</div><div>}</div><div><br></div><div>inline bool scan_d(int &amp;num) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;char in;bool IsN=false;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;in=getchar();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in==EOF) return false;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in!='-'&amp;&amp;(in&lt;'0'||in&gt;'9')) in=getchar();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in=='-'){ IsN=true;num=0;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;else num=in-'0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in=getchar(),in&gt;='0'&amp;&amp;in&lt;='9'){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num*=10,num+=in-'0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(IsN) num=-num;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return true;</div><div>}</div><div><br></div><div>int main() {</div><div>&nbsp;&nbsp; &nbsp;while ( scan_d ( N ) ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 1; i &lt;= N; ++ i ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scan_d ( heap[i].val );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;heap[i].l = heap[i].r = heap[i].dis = 0;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;heap[i].dad = i; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; scan_d ( M );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int a, b, x, y;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; while ( M -- ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scan_d (a); scan_d (b);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = find ( a );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = find ( b );&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( x == y ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;puts ( "-1" ); &nbsp; &nbsp;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;heap[x].val /= 2;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int xx = push ( pop ( x ), x ); &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;heap[y].val /= 2;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int yy = push ( pop ( y ), y ); &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf ( "%d\n", heap[ merge ( xx, yy ) ].val ); &nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;return 0;</div><div>}</div></div><div><br></div><div><br></div></font></div></div><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><img src ="http://www.cppblog.com/MiYu/aggbug/131043.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-24 11:43 <a href="http://www.cppblog.com/MiYu/archive/2010/10/24/131043.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>左偏树详解 ( 转载 )</title><link>http://www.cppblog.com/MiYu/archive/2010/10/24/131040.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sun, 24 Oct 2010 02:53:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/24/131040.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/131040.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/24/131040.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/131040.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/131040.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.1.2&nbsp;可并堆的定义可并堆(Mergeable Heap)也是一种抽象数据类型，它除了支持优先队列的三个基本操作(Insert, Minimum, Delete-Min)，还支持一个额外的操作——合并操作：H &#8592; Merge(H...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/10/24/131040.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/131040.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-24 10:53 <a href="http://www.cppblog.com/MiYu/archive/2010/10/24/131040.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Bellman-Ford &amp;&amp; SPFA 算法</title><link>http://www.cppblog.com/MiYu/archive/2010/10/23/131018.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sat, 23 Oct 2010 14:09:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/23/131018.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/131018.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/23/131018.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/131018.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/131018.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://baiyun.me/"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><div><font  color="#800080"><span  style="font-family: sans-serif; color: rgb(0, 0, 0); line-height: 19px; "><h2 style="color: black; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; font-weight: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0.6em; margin-left: 0px; padding-top: 0.5em; padding-bottom: 0.17em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(170, 170, 170); font-size: 19px; ">&nbsp;B-F&nbsp;</h2><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="mw-headline" style="font-family: 'Courier New'; ">适用条件&amp;范围</span></p><ol style="line-height: 1.5em; margin-top: 0.3em; margin-right: 0px; margin-bottom: 0px; margin-left: 3.2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-image: none; "><li style="margin-bottom: 0.1em; ">单源最短路径(从源点s到其它所有顶点v);</li><li style="margin-bottom: 0.1em; ">有向图&amp;无向图(无向图可以看作(u,v),(v,u)同属于边集E的有向图);</li><li style="margin-bottom: 0.1em; ">边权可正可负(如有负权回路输出错误提示);</li><li style="margin-bottom: 0.1em; ">差分约束系统;</li></ol><a name=".E7.AE.97.E6.B3.95.E6.8F.8F.E8.BF.B0" style="text-decoration: none; color: rgb(0, 43, 184); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a><h2 style="color: black; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; font-weight: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0.6em; margin-left: 0px; padding-top: 0.5em; padding-bottom: 0.17em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(170, 170, 170); font-size: 19px; "><span class="mw-headline" style="font-family: 'Courier New'; ">算法描述</span></h2><ol style="line-height: 1.5em; margin-top: 0.3em; margin-right: 0px; margin-bottom: 0px; margin-left: 3.2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-image: none; "><li style="margin-bottom: 0.1em; ">对每条边进行|V|-1次Relax ( 就是松弛操作 )操作;</li><li style="margin-bottom: 0.1em; ">如果存在(u,v)&#8712;E使得dis[u]+w&lt;dis[v],则存在负权回路;否则dis[v]即为s到v的最短距离,pre[v]为前驱。</li></ol><pre style="padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(47, 111, 171); border-right-color: rgb(47, 111, 171); border-bottom-color: rgb(47, 111, 171); border-left-color: rgb(47, 111, 171); color: black; background-color: rgb(249, 249, 249); line-height: 1.1em; ">For i:=1 to |V|-1 do //v为顶点数
For 每条边(u,v)&#8712;E do  //对每条边进行遍历
  Relax(u,v,w);
For每条边(u,v)&#8712;E do
  If dis[u]+w&lt;dis[v] Then Exit(False)
</pre><a name=".E6.97.B6.E7.A9.BA.E5.A4.8D.E6.9D.82.E5.BA.A6" style="text-decoration: none; color: rgb(0, 43, 184); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a><h2 style="color: black; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; font-weight: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0.6em; margin-left: 0px; padding-top: 0.5em; padding-bottom: 0.17em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(170, 170, 170); font-size: 19px; "><span class="mw-headline" style="font-family: 'Courier New'; ">时空复杂度</span></h2><p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; line-height: 1.5em; ">算法时间复杂度O(VE)。因为算法简单，适用范围又广，虽然复杂度稍高，仍不失为一个很实用的算法。</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">算法的改进---&gt; SPFA&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><h2 style="color: black; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; font-weight: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0.6em; margin-left: 0px; padding-top: 0.5em; padding-bottom: 0.17em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(170, 170, 170); font-size: 19px; "><span class="mw-headline" style="font-family: 'Courier New'; ">算法简介</span></h2><p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; line-height: 1.5em; ">SPFA(Shortest Path Faster Algorithm)是<a href="http://www.nocow.cn/index.php/Bellman-Ford%E7%AE%97%E6%B3%95" title="Bellman-Ford算法" style="text-decoration: none; color: rgb(90, 54, 150); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; ">Bellman-Ford算法</a>的一种<a href="http://www.nocow.cn/index.php/%E9%98%9F%E5%88%97" title="队列" style="text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; ">队列</a>实现，减少了不必要的冗余计算。也有人说SPFA本来就是Bellman-Ford算法，现在广为流传的<a href="http://www.nocow.cn/index.php/Bellman-Ford%E7%AE%97%E6%B3%95" title="Bellman-Ford算法" style="text-decoration: none; color: rgb(90, 54, 150); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; ">Bellman-Ford算法</a>实际上是山寨版。</p><a name=".E7.AE.97.E6.B3.95.E6.B5.81.E7.A8.8B" style="text-decoration: none; color: rgb(0, 43, 184); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a><h2 style="color: black; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; font-weight: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0.6em; margin-left: 0px; padding-top: 0.5em; padding-bottom: 0.17em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(170, 170, 170); font-size: 19px; "><span class="mw-headline" style="font-family: 'Courier New'; ">算法流程</span></h2><p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; line-height: 1.5em; ">算法大致流程是用一个队列来进行维护。 初始时将源加入队列。 每次从队列中取出一个元素，并对所有与他相邻的点进行<a href="http://www.nocow.cn/index.php/%E6%9D%BE%E5%BC%9B" title="松弛" style="text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; ">松弛</a>，若某个相邻的点松弛成功，则将其入队。 直到队列为空时算法结束。</p><p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; line-height: 1.5em; ">这个算法，简单的说就是队列优化的bellman-ford,利用了每个点不会更新次数太多的特点发明的此算法</p><p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; line-height: 1.5em; ">SPFA——Shortest Path Faster Algorithm，它可以在O(kE)的时间复杂度内求出源点到其他所有点的最短路径，可以处理负边。SPFA的实现甚至比Dijkstra或者Bellman_Ford还要简单：</p><p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; line-height: 1.5em; ">设Dist代表S到I点的当前最短距离，Fa代表S到I的当前最短路径中I点之前的一个点的编号。开始时Dist全部为+&#8734;，只有Dist[S]=0，Fa全部为0。</p><p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; line-height: 1.5em; ">维护一个队列，里面存放所有需要进行迭代的点。初始时队列中只有一个点S。用一个布尔数组记录每个点是否处在队列中。</p><p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; line-height: 1.5em; ">每次迭代，取出队头的点v，依次枚举从v出发的边v-&gt;u，设边的长度为len，判断Dist[v]+len是否小于Dist[u]，若小于则改进Dist[u]，将Fa[u]记为v，并且由于S到u的最短距离变小了，有可能u可以改进其它的点，所以若u不在队列中，就将它放入队尾。这样一直迭代下去直到队列变空，也就是S到所有的最短距离都确定下来，结束算法。若一个点入队次数超过n，则有负权环。</p><p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; line-height: 1.5em; ">SPFA 在形式上和宽度优先搜索非常类似，不同的是宽度优先搜索中一个点出了队列就不可能重新进入队列，但是SPFA中一个点可能在出队列之后再次被放入队列，也就是一个点改进过其它的点之后，过了一段时间可能本身被改进，于是再次用来改进其它的点，这样反复迭代下去。设一个点用来作为迭代点对其它点进行改进的平均次数为k，有办法证明对于通常的情况，k在2左右</p><a name=".E7.AE.97.E6.B3.95.E4.BB.A3.E7.A0.81" style="text-decoration: none; color: rgb(0, 43, 184); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a><h2 style="color: black; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; font-weight: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0.6em; margin-left: 0px; padding-top: 0.5em; padding-bottom: 0.17em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(170, 170, 170); font-size: 19px; "><span class="mw-headline" style="font-family: 'Courier New'; ">算法代码</span></h2><pre class="pascal" style="padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(47, 111, 171); border-right-color: rgb(47, 111, 171); border-bottom-color: rgb(47, 111, 171); border-left-color: rgb(47, 111, 171); color: black; background-color: rgb(249, 249, 249); line-height: 1.1em; "><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); font-weight: bold; ">Procedure</span> SPFA;
&nbsp;
<span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">Begin</span>
  initialize-single-source<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>G,s<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>;
  initialize-queue<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>Q<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>;
  enqueue<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>Q,s<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>;
  <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">while</span> <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">not</span> empty<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>Q<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span> <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">do</span> 
    <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">begin</span>
      u:=dequeue<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>Q<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>;
      <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">for</span> each v&#8712;adj<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>u<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span> <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">do</span> 
        <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">begin</span>
          tmp:=d<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>v<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>;
          relax<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>u,v<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>;
          <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">if</span> <span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>tmp&lt;&gt;d<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>v<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span><span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span> <span style="font-family: 'Courier New'; ">and</span> <span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span><span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">not</span> v <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">in</span> Q<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span> <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">then</span>
            enqueue<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>Q,v<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>;
        <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">end</span>;
    <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">end</span>;
<span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">End</span>;</pre><p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; line-height: 1.5em; "><br></p><pre class="pascal" style="padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(47, 111, 171); border-right-color: rgb(47, 111, 171); border-bottom-color: rgb(47, 111, 171); border-left-color: rgb(47, 111, 171); color: black; background-color: rgb(249, 249, 249); line-height: 1.1em; "><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); font-weight: bold; ">procedure</span> spfa;
<span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">begin</span>
  fillchar<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>q,sizeof<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>q<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>,<span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">0</span><span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>; h:=<span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">0</span>; t:=<span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">0</span>;<span style="font-family: 'Courier New'; color: rgb(128, 128, 128); font-style: italic; ">//队列</span>
  fillchar<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>v,sizeof<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>v<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>,<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); font-weight: bold; ">false</span><span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>;<span style="font-family: 'Courier New'; color: rgb(128, 128, 128); font-style: italic; ">//v[i]判断i是否在队列中</span>
  <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">for</span> i:=<span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">1</span> <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">to</span> n <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">do</span> 
    dist<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>:=maxint;<span style="font-family: 'Courier New'; color: rgb(128, 128, 128); font-style: italic; ">//初始化最小值</span>
&nbsp;
  inc<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>t<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span>;
  q<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>t<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>:=<span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">1</span>;
  v<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span><span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">1</span><span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>:=<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); font-weight: bold; ">true</span>;
  dist<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span><span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">1</span><span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>:=<span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">0</span>;<span style="font-family: 'Courier New'; color: rgb(128, 128, 128); font-style: italic; ">//这里把1作为源点</span>
&nbsp;
  <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">while</span> h&lt;&gt;t <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">do</span>
    <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">begin</span>
      h:=<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>h <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">mod</span> n<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span><span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">+1</span>;
      x:=q<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>h<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>;
      v<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>x<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>:=<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); font-weight: bold; ">false</span>;
      <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">for</span> i:=<span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">1</span> <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">to</span> n <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">do</span>
        <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">if</span> <span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>cost<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>x,i<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>&gt;<span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">0</span><span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span> <span style="font-family: 'Courier New'; ">and</span> <span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>dist<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>x<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>+cost<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>x,i<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>&lt;dist<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span><span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span> <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">then</span>
          <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">begin</span>
            dist<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>:=dist<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>x<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>+cost<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>x,i<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>;
            <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">if</span> <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">not</span><span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>v<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span><span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span> <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">then</span>
              <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">begin</span>
                t:=<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">(</span>t <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">mod</span> n<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">)</span><span style="font-family: 'Courier New'; color: rgb(204, 102, 204); ">+1</span>;
                q<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>t<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>:=i;
                v<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(102, 204, 102); ">]</span>:=<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); font-weight: bold; ">true</span>;
              <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">end</span>;
          <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">end</span>;
    <span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">end</span>;
<span style="font-family: 'Courier New'; color: rgb(177, 177, 0); ">end</span>;</pre><pre class="cpp" style="padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(47, 111, 171); border-right-color: rgb(47, 111, 171); border-bottom-color: rgb(47, 111, 171); border-left-color: rgb(47, 111, 171); color: black; background-color: rgb(249, 249, 249); line-height: 1.1em; "><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span> SPFA<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0); ">//好久以前写的&#8230;&#8230;今天丢上来&#8230;&#8230;话说我都不记得SPFA怎么写了&#8230;&#8230;囧&#8230;&#8230;ms存图是矩阵&#8230;&#8230;嗯嗯</span>
<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">{</span>
 <span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span> i;
 queue list;
 list.<span style="font-family: 'Courier New'; color: rgb(0, 238, 255); ">insert</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>s<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>;
 <span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>i=<span style="font-family: 'Courier New'; color: rgb(0, 0, 221); ">1</span>;i&lt;=n;i++<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>
  <span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">{</span>
   <span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>s==i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>
    <span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">continue</span>;
   dist<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>=map<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>s<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>;
   way<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>=s;
   <span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>dist<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>
   list.<span style="font-family: 'Courier New'; color: rgb(0, 238, 255); ">insert</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>;
  <span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">}</span>
 <span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span> p;
 <span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>!list.<span style="font-family: 'Courier New'; color: rgb(0, 238, 255); ">empty</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>
 <span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">{</span>
  p=list.<span style="font-family: 'Courier New'; color: rgb(0, 238, 255); ">fire</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>;
  <span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>i=<span style="font-family: 'Courier New'; color: rgb(0, 0, 221); ">1</span>;i&lt;=n;i++<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>
   <span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>map<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>p<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>&amp;&amp;<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>dist<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>&gt;dist<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>p<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>+map<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>p<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>||!dist<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>&amp;&amp;i!=s<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>
    <span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">{</span>
     dist<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>=dist<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>p<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>+map<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>p<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>;
     way<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span>=p;
     <span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>!list.<span style="font-family: 'Courier New'; color: rgb(0, 238, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>
      list.<span style="font-family: 'Courier New'; color: rgb(0, 238, 255); ">insert</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span>i<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)</span>;
    <span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">}</span>
 <span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">}</span>
<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">}<span  style="font-family: sans-serif; line-height: 19px; white-space: normal; ">&nbsp;</span></span></pre></span></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/131018.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-23 22:09 <a href="http://www.cppblog.com/MiYu/archive/2010/10/23/131018.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>trie树--详解</title><link>http://www.cppblog.com/MiYu/archive/2010/10/23/131017.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sat, 23 Oct 2010 14:05:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/23/131017.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/131017.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/23/131017.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/131017.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/131017.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;文章作者：yx_th000&nbsp;文章来源：Cherish_yimi (http://www.cnblogs.com/cherish_yimi/) 转载请注明，谢谢合作。关键词：trie trie树 数据结构&nbsp;&nbsp;&nbsp;&nb...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/10/23/131017.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/131017.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-23 22:05 <a href="http://www.cppblog.com/MiYu/archive/2010/10/23/131017.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>前向星+SPFA(转载)</title><link>http://www.cppblog.com/MiYu/archive/2010/10/23/131014.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sat, 23 Oct 2010 14:02:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/23/131014.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/131014.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/23/131014.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/131014.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/131014.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://baiyun.me/"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_44cadeb7-8e99-4853-b65a-0386747ba3d7" style="vertical-align: middle; padding-right: 5px; "><span class="cnblogs_code_collapse" style="border-right-color: rgb(128, 128, 128); border-right-width: 1px; border-right-style: solid; border-top-color: rgb(128, 128, 128); border-top-width: 1px; border-top-style: solid; border-left-color: rgb(128, 128, 128); border-left-width: 1px; border-left-style: solid; border-bottom-color: rgb(128, 128, 128); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(255, 255, 255); padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px; font-family: 'Courier New'; ">代码</span><div id="cnblogs_code_open_44cadeb7-8e99-4853-b65a-0386747ba3d7"><div><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">前向星</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">SPFA<br>我是在做USACO的sweet&nbsp;butter时偶然发现这个东西的。。。<br>这个算法，简单的说就是队列优化的bellman</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">ford,利用了每个点不会更新次数太多的特点发明的此算法（仅为个人理解</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">.</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）<br>SPFA——Shortest&nbsp;Path&nbsp;Faster&nbsp;Algorithm，它可以在O(kE)的时间复杂度内求出源点到其他所有点的最短路径，可以处理负边。SPFA的实现甚至比Dijkstra或者Bellman_Ford还要简单：<br>设Dist代表S到I点的当前最短距离，Fa代表S到I的当前最短路径中I点之前的一个点的编号。开始时Dist全部为</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&#8734;，只有Dist[S]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，Fa全部为0。<br>维护一个队列，里面存放所有需要进行迭代的点。初始时队列中只有一个点S。用一个布尔数组记录每个点是否处在队列中。<br>每次迭代，取出队头的点v，依次枚举从v出发的边v</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">u，设边的长度为len，判断Dist[v]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">len是否小于Dist，若小于则改进Dist，将Fa记为v，并且由于S到u的最短距离变小了，有可能u可以改进其它的点，所以若u不在队列中，就将它放入队尾。这样一直迭代下去直到队列变空，也就是S到所有的最短距离都确定下来，结束算法。<br>SPFA&nbsp;在形式上和宽度优先搜索非常类似，不同的是宽度优先搜索中一个点出了队列就不可能重新进入队列，但是SPFA中一个点可能在出队列之后再次被放入队列，也就是一个点改进过其它的点之后，过了一段时间可能本身被改进，于是再次用来改进其它的点，这样反复迭代下去。设一个点用来作为迭代点对其它点进行改进的平均次数为k，有办法证明对于通常的情况，k在2左右<br>前向星优化：<br>不要把前向星想成什么高深莫测的东西&#8230;&#8230;它其实就是一种邻接表的紧缩存储形式。<br>为什么叫前向星？因为它是将边按照前端点排序，并用一个数组k[i]记录端点i第一次以左端点出现的位置。这样，我们就能用O(E)的空间复杂度存储下一个邻接表，而避免了链表或N</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">2的庞大空间消耗。<br>当然，实际上我们并不需要排序：因为我们只需要知道某一条边应该放到什么位置即可。因而我们还需要一个数组t[i]存储从i出发的边的条数。则需要存储在的位置就可以很轻易地求得。（详见代码）<br>Butter题目代码如下：<br>Program&nbsp;butter(input,output);<br>Type<br>&nbsp;&nbsp;&nbsp;edge</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">record<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x,y,d:longint;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>Var<br>&nbsp;&nbsp;&nbsp;min,res,n,p,c,x,y,i,j,l,r:longint;<br>&nbsp;&nbsp;&nbsp;te,e:array[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">..</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">3000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;of&nbsp;edge;<br>&nbsp;&nbsp;&nbsp;tk,t,k,num,d:array[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">..</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">800</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;of&nbsp;longint;<br>&nbsp;&nbsp;&nbsp;q:array[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">..</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">100000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;of&nbsp;longint;<br>&nbsp;&nbsp;&nbsp;use:array[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">..</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">800</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;of&nbsp;boolean;<br>Procedure&nbsp;swap(var&nbsp;n1,n2:longint);<br>Var<br>&nbsp;&nbsp;&nbsp;tmp:longint;<br>Begin<br>&nbsp;&nbsp;&nbsp;tmp:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n1;n1:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n2;n2:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">tmp;<br>End;<br>Begin<br>&nbsp;&nbsp;&nbsp;assign(input,</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">butter.in</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">);reset(input);<br>&nbsp;&nbsp;&nbsp;readln(n,p,c);<br>&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;to&nbsp;n&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;read(x);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inc(num[x]);<br>&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;to&nbsp;c&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;e[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;readln(x,y,d);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">e[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swap(e[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">].x,e[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">].y);<br>&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;c:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">c</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;to&nbsp;c&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;inc(t[e[i].x]);<br>&nbsp;&nbsp;&nbsp;j:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;k[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;to&nbsp;p&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k[i]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">k[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">t[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];<br>&nbsp;&nbsp;&nbsp;tk:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">k;te:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">e;<br>&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;to&nbsp;c&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e[tk[te[i].x]]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">te[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inc(tk[te[i].x]);<br>&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;min:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">maxlongint;<br>&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;to&nbsp;p&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillchar(q,</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">sizeof</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(q),</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillchar(d,</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">sizeof</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(d),</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">127</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillchar(use,</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">sizeof</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(use),</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">false</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">i;l:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;r:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;d[i]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;use[i]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">true</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repeat<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;j:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">k[q[l]]&nbsp;to&nbsp;k[q[l]]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">t[q[l]]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;d[q[l]]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">e[j].d</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">d[e[j].y]&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[e[j].y]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">d[q[l]]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">e[j].d;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;not&nbsp;use[e[j].y]&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use[e[j].y]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">true</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inc(r);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q[r]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">e[j].y;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use[q[l]]:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">false</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inc(l);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;until&nbsp;l</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">r;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;j:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;to&nbsp;p&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">res</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">d[j]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">num[j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;res</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">min&nbsp;then&nbsp;min:</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">res;<br>&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;assign(output,</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">butter.out</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">);rewrite(output);<br>&nbsp;&nbsp;&nbsp;writeln(min);close(output);<br>End.<br></span></div></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/131014.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-23 22:02 <a href="http://www.cppblog.com/MiYu/archive/2010/10/23/131014.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>松弛操作</title><link>http://www.cppblog.com/MiYu/archive/2010/10/23/131013.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sat, 23 Oct 2010 14:01:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/23/131013.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/131013.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/23/131013.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/131013.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/131013.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://baiyun.me/"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><div><font  color="#800080"><span  style="font-family: arial, 宋体, sans-serif; color: rgb(0, 0, 0); line-height: 24px; font-size: 14px; ">(重定向自松弛技术)<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　单源最短路径算法中使用了松弛（relaxation）操作。对于每个顶点v&#8712;V，都设置一个属性d[v]，用来描述从源点s到v的最短路径上权值的上界，称为最短路径估计（shortest-path estimate）。&#960;[v]代表S到v的当前最短路径中v点之前的一个点的编号,我们用下面的&#920;(V)时间的过程来对最短路径估计和前趋进行初始化。<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　INITIALIZE-SINGLE-SOURCE(G,s)<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　1 for each vertex v&#8712;V[G]<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　2 do d[v]&#8592;&#8734;<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　3 &#960;[v]&#8592;NIL<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　4 d[s]&#8592;0<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　经过初始化以后，对所有v&#8712;V，&#960;[v]=NIL，对v&#8712;V-{s}，有d[s]=0以及d[v]=&#8734;。<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　在松弛一条边(u,v)的过程中，要测试是否可以通过u，对迄今找到的v的最短路径进行改进；如果可以改进的话，则更新d[v]和&#960;[v]。一次松弛操作可以减小最短路径估计的值d[v]，并更新v的前趋域&#960;[v](S到v的当前最短路径中v点之前的一个点的编号)。下面的伪代码对边(u,v)进行了一步松弛操作。<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　RELAX(u, v, w)<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　1 if(d[v]&gt;d[u]+w(u,v))<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　2 then d[v]&#8592;d[u]+w(u,v)<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　3 &#960;[v]&#8592;u<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　每个单源最短路径算法中都会调用INITIALIZE-SINGLE-SOURCE，然后重复对边进行松弛的过程。另外，松弛是改变最短路径和前趋的唯一方式。各个单源最短路径算法间区别在于对每条边进行松弛操作的次数，以及对边执行松弛操作的次序有所不同。在Dijkstra算法以及关于有向无回路图的最短路径算法中，对每条边执行一次松弛操作。在<a target="_blank" href="http://baike.baidu.com/view/1481053.htm" style="text-decoration: underline; color: rgb(19, 110, 194); ">Bellman-Ford算法</a>中，每条边要执行多次松弛操作。<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　procedure relax(u,v,w:integer);//多数情况下不需要单独写成procedure。<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　begin<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　if dis+w&lt;dis[v] then<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　begin<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　dis[v]:=dis+w;<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　pre[v]:=u;<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　end<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　end;</span></font></div><div><font  color="#800080"><span  style="font-family: arial, 宋体, sans-serif; color: rgb(0, 0, 0); line-height: 24px; font-size: 14px; "><br></span></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/131013.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-23 22:01 <a href="http://www.cppblog.com/MiYu/archive/2010/10/23/131013.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>什么是离散化?</title><link>http://www.cppblog.com/MiYu/archive/2010/10/15/129999.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 15 Oct 2010 03:15:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/15/129999.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/129999.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/15/129999.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/129999.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/129999.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span  style="font-family: Tahoma, 'Trebuchet MS', 'Lucida Grande', Verdana, Georgia, sans-serif; line-height: 19px; font-size: 12px; color: rgb(35, 35, 35); ">Matrix67原创 &nbsp;</span>Trackback:&nbsp;<a href="http://www.matrix67.com/blog/archives/108"><font  color="#000000">http://www.matrix67.com/blog/archives/108</font></a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;<span  style="font-family: Tahoma, 'Trebuchet MS', 'Lucida Grande', Verdana, Georgia, sans-serif; line-height: 19px; font-size: 12px; color: rgb(35, 35, 35); ">&nbsp;&nbsp;&nbsp;&nbsp;如果说今年这时候OIBH问得最多的问题是二分图，那么去年这时候问得最多的算是离散化了。对于&#8220;什么是离散化&#8221;，搜索帖子你会发现有各种说法，比如&#8220;排序后处理&#8221;、&#8220;对坐标的近似处理&#8221;等等。哪个是对的呢？哪个都对。关键在于，这需要一些例子和不少的讲解才能完全解释清楚。</span></p><span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span  style="font-family: Tahoma, 'Trebuchet MS', 'Lucida Grande', Verdana, Georgia, sans-serif; line-height: 19px; font-size: 12px; color: rgb(35, 35, 35); ">&nbsp;&nbsp;&nbsp;&nbsp;离散化是程序设计中一个非常常用的技巧，它可以有效的降低时间复杂度。其基本思想就是在众多可能的情况中&#8220;只考虑我需要用的值&#8221;。下面我将用三个例子说明，如何运用离散化改进一个低效的，甚至根本不可能实现的算法。<br><br>&nbsp;&nbsp;&nbsp;&nbsp;《算法艺术与信息学竞赛》中的计算几何部分，黄亮举了一个经典的例子，我认为很适合用来介绍离散化思想。这个问题是UVA10173(http://acm.uva.es/p/v101/10173.html)，题目意思很简单，给定平面上n个点的坐标，求能够覆盖所有这些点的最小矩形面积。这个问题难就难在，这个矩形可以倾斜放置（边不必平行于坐标轴）。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.matrix67.com/blogimage/200609261.GIF" border="0" alt=""><br>&nbsp;&nbsp;&nbsp;&nbsp;这里的倾斜放置很不好处理，因为我们不知道这个矩形最终会倾斜多少度。假设我们知道这个矩形的倾角是&#945;，那么答案就很简单了：矩形面积最小时四条边一定都挨着某个点。也就是说，四条边的斜率已经都知道了的话，只需要让这些边从外面不断逼近这个点集直到碰到了某个点。你不必知道这个具体应该怎么实现，只需要理解这可以通过某种方法计算出来，毕竟我们的重点在下面的过程。<br>&nbsp;&nbsp;&nbsp;&nbsp;我们的算法很显然了：枚举矩形的倾角，对于每一个倾角，我们都能计算出最小的矩形面积，最后取一个最小值。<br>&nbsp;&nbsp;&nbsp;&nbsp;这个算法是否是正确的呢？我们不能说它是否正确，因为它根本不可能实现。矩形的倾角是一个实数，它有无数种可能，你永远不可能枚举每一种情况。我们说，矩形的倾角是一个&#8220;连续的&#8221;变量，它是我们无法枚举这个倾角的根本原因。我们需要一种方法，把这个&#8220;连续的&#8221;变量变成一个一个的值，变成一个&#8220;离散的&#8221;变量。这个过程也就是所谓的离散化。<br>&nbsp;&nbsp;&nbsp;&nbsp;我们可以证明，最小面积的矩形不但要求四条边上都有一个点，而且还要求至少一条边上有两个或两个以上的点。试想，如果每条边上都只有一个点，则我们总可以把这个矩形旋转一点使得这个矩形变&#8220;松&#8221;，从而有余地得到更小的矩形。于是我们发现，矩形的某条边的斜率必然与某两点的连线相同。如果我们计算出了所有过两点的直线的倾角，那么&#945;的取值只有可能是这些倾角或它减去90度后的角（直线按&#8220;\&#8221;方向倾斜时）这么C(n,2)种。我们说，这个&#8220;倾角&#8221;已经被我们 &#8220;离散化&#8221;了。虽然这个算法仍然有优化的余地，但此时我们已经达到了本文开头所说的目的。<br><br>&nbsp;&nbsp;&nbsp;&nbsp;对于某些坐标虽然已经是整数（已经是离散的了）但范围极大的问题，我们也可以用离散化的思想缩小这个规模。最近搞模拟赛Vijos似乎火了一把，我就拿两道Vijos的题开刀。<br>&nbsp;&nbsp;&nbsp;&nbsp;VOJ1056(http://www.vijos.cn/Problem_Show.asp?id=1056) 永远是离散化的经典问题。大意是给定平面上的n个矩形（坐标为整数，矩形与矩形之间可能有重叠的部分），求其覆盖的总面积。平常的想法就是开一个与二维坐标规模相当的二维Boolean数组模拟矩形的&#8220;覆盖&#8221;（把矩形所在的位置填上True）。可惜这个想法在这里有些问题，因为这个题目中坐标范围相当大（坐标范围为-10^8到10^8之间的整数）。但我们发现，矩形的数量n&lt;=100远远小于坐标范围。每个矩形会在横纵坐标上各&#8220;使用&#8221;两个值， 100个矩形的坐标也不过用了-10^8到10^8之间的200个值。也就是说，实际有用的值其实只有这么几个。这些值将作为新的坐标值重新划分整个平面，省去中间的若干坐标值没有影响。我们可以将坐标范围&#8220;离散化&#8221;到1到200之间的数，于是一个200*200的二维数组就足够了。实现方法正如本文开头所说的&#8220;排序后处理&#8221;。对横坐标（或纵坐标）进行一次排序并映射为1到2n的整数，同时记录新坐标的每两个相邻坐标之间在离散化前实际的距离是多少。这道题同样有优化的余地。<br>&nbsp;&nbsp;&nbsp;&nbsp;最后简单讲一下计算几何以外的一个运用实例（实质仍然是坐标的离散）。才考的VOJ1238(http://www.vijos.cn/Problem_Show.asp?id=1238)中，标程开了一个与时间范围一样大的数组来储存时间段的位置。这种方法在空间上来看十分危险。一旦时间取值范围再大一点，盲目的空间开销将导致Memory Limit Exceeded。我们完全可以采用离散化避免这种情况。我们对所有给出的时间坐标进行一次排序，然后同样用时间段的开始点和结束点来计算每个时刻的游戏数，只是一次性加的经验值数将乘以排序后这两个相邻时间点的实际差。这样，一个1..n的数组就足够了。<br><br>&nbsp;&nbsp;&nbsp;&nbsp;离散化的应用相当广泛，以后你会看到还有很多其它的用途。<br><br><span style="color: blue; ">2007.04.05补充：<br>VOJ1056那个例子看来还是有人不明白。<br>我发一张示意图，注意左边的10*7的数组是如何等价地转化为右边两个4*4的数组的</span><br><img src="http://www.matrix67.com/blogimage/200609262.GIF" border="0" alt=""></span></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/129999.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-15 11:15 <a href="http://www.cppblog.com/MiYu/archive/2010/10/15/129999.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[翻译]Wine完全使用指南——从基本到高级[转]</title><link>http://www.cppblog.com/MiYu/archive/2010/10/05/128663.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Tue, 05 Oct 2010 02:28:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/05/128663.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/128663.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/05/128663.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/128663.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/128663.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转载自 : &nbsp;http://forum.ubuntu.org.cn/viewtopic.php?t=72933&nbsp;为了回应最近网友的一些普遍问题，特此作以下说明：&nbsp;1、如果发现wine的注册表没有相应的键值，要自己创建。有时候用命令不行的话，只能手动一个个添加。&nbsp;2、wine还是一个不成熟的软件，经常出现bug，一切使用的问题和风险都只能自己承担。&nbsp;...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/10/05/128663.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/128663.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-05 10:28 <a href="http://www.cppblog.com/MiYu/archive/2010/10/05/128663.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>线段树的一种简化实现[转] by 踏雪赤兔 </title><link>http://www.cppblog.com/MiYu/archive/2010/10/04/128568.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Mon, 04 Oct 2010 08:11:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/04/128568.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/128568.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/04/128568.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/128568.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/128568.html</trackback:ping><description><![CDATA[



<div><br></div><div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;转载自&nbsp;<a href="http://www.cnitblog.com/cockerel/archive/2006/09/13/16806.html">http://www.cnitblog.com/cockerel/archive/2006/09/13/16806.html</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); ">相信对算法设计或者数据结构有一定了解的人对线段树都不会太陌生。它是能够在log(MaxLen)时间内完成线段的添加、删除、查询等操作。但一般的实现都有点复杂（我自写的是要递归的，比较多行）。而线段树应用中有一种是专门针对点的。（点树？）它的实现却非常简单。<br>　　这种数据结构有什么用？我们先来考虑一下下面的需求（全部要求在LogN时间内完成）：如何知道一个点在一个点集里的大小&#8220;排名&#8221;？很简单，开一个点数组，排个序，再二分查找就行了；如何在一个点集内动态增删点？也很简单，弄个平衡树就行了（本来平衡树比线段树复杂得多，但自从世界上有了STL set这么个好东东，就&#8230;&#8230;^_^）那如果我既要动态增删点，也要随时查询到一个点的排名呢？那对不起，可能就要出动到我们的&#8220;点树&#8221;了。<br>　　其实现原理很简单：每当增加（或删除）一个大小为X的点时，就在树上添加（或删除）一条(X,MaxLen)的线段（不含端点），当要查询一个点的排名时，只要看看其上有多少条线段就可以了。针对这一需求，这里有个非常简单的实现（见以下代码，十多行，够短了吧？）其中clear()用于清空点集；add()用于添加一个点；cntLs()返回小于n的点的个数，也就是n的升序排名，类似地cntGt是降序排名。<br>　　这个点树有什么用呢？其中一个应用时在O(NlogN)时间内求出一个排列的逆序数（<a href="http://acm.zju.edu.cn/show_problem.php?pid=1484" style="color: rgb(0, 0, 0); ">http://acm.zju.edu.cn/show_problem.php?pid=1484</a>，你有更好的算法吗？欢迎交流）方法是每读到一个数x，就让逆序数+=cntGt(x);然后再add(x)。<br>　　这个实现还可以进行一些扩展。比如删除del(int n)，只要把add(int n)中的++size换成--size，把a[i/2]++改成a[i/2]--即可。另外还可以通过二分查找功能在O(logN)时间内查到排名第n的点的大小。应该也可以三四行内搞定。</p><div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); width: 566px; word-break: break-all; padding-top: 4px; background-color: rgb(238, 238, 238); font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; color: rgb(75, 75, 75); "><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;<span style="color: rgb(0, 0, 0); ">template</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&lt;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">int</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;N</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&gt;</span>&nbsp;<span style="color: rgb(0, 128, 0); ">//</span>&nbsp;<span style="color: rgb(0, 128, 0); ">表示可用区间为[0,N)，其中N必须是2的幂数;&nbsp;</span>&nbsp;<span style="color: rgb(0, 128, 0); "><br><img id="Codehighlighter1_48_453_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top">&nbsp;</span><span style="color: rgb(0, 0, 255); ">class</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;PointTree</span>&nbsp;<span id="Codehighlighter1_48_453_Open_Text"><span style="color: rgb(0, 0, 0); ">{<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">int</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;a[</span>&nbsp;<span style="color: rgb(0, 0, 0); ">2</span>&nbsp;<span style="color: rgb(0, 0, 0); ">*</span>&nbsp;<span style="color: rgb(0, 0, 0); ">N];<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">int</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;size;&nbsp;<br><img id="Codehighlighter1_97_128_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">void</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;clear()</span>&nbsp;<span id="Codehighlighter1_97_128_Open_Text"><span style="color: rgb(0, 0, 0); ">{&nbsp;memset(</span>&nbsp;<span style="color: rgb(0, 0, 255); ">this</span>&nbsp;<span style="color: rgb(0, 0, 0); ">,</span>&nbsp;<span style="color: rgb(0, 0, 0); ">0</span>&nbsp;<span style="color: rgb(0, 0, 0); ">,</span>&nbsp;<span style="color: rgb(0, 0, 255); ">sizeof</span>&nbsp;<span style="color: rgb(0, 0, 0); ">(</span>&nbsp;<span style="color: rgb(0, 0, 0); ">*</span>&nbsp;<span style="color: rgb(0, 0, 255); ">this</span>&nbsp;<span style="color: rgb(0, 0, 0); ">));}</span>&nbsp;</span><span style="color: rgb(0, 0, 0); ">&nbsp;<br><img id="Codehighlighter1_150_246_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">void</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;add(</span>&nbsp;<span style="color: rgb(0, 0, 255); ">int</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;n)</span>&nbsp;<span id="Codehighlighter1_150_246_Open_Text"><span style="color: rgb(0, 0, 0); ">{<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">int</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;i</span>&nbsp;<span style="color: rgb(0, 0, 0); ">=</span>&nbsp;<span style="color: rgb(0, 0, 0); ">N</span>&nbsp;<span style="color: rgb(0, 0, 0); ">+</span>&nbsp;<span style="color: rgb(0, 0, 0); ">n;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 0); ">++</span>&nbsp;<span style="color: rgb(0, 0, 0); ">size;&nbsp;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">for</span>&nbsp;<span style="color: rgb(0, 0, 0); ">(</span>&nbsp;<span style="color: rgb(0, 0, 0); ">++</span>&nbsp;<span style="color: rgb(0, 0, 0); ">a[i];&nbsp;i</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&gt;</span>&nbsp;<span style="color: rgb(0, 0, 0); ">1</span>&nbsp;<span style="color: rgb(0, 0, 0); ">;&nbsp;i</span>&nbsp;<span style="color: rgb(0, 0, 0); ">/=</span>&nbsp;<span style="color: rgb(0, 0, 0); ">2</span>&nbsp;<span style="color: rgb(0, 0, 0); ">)<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">if</span>&nbsp;<span style="color: rgb(0, 0, 0); ">(</span>&nbsp;<span style="color: rgb(0, 0, 0); ">~</span>&nbsp;<span style="color: rgb(0, 0, 0); ">i</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&amp;</span>&nbsp;<span style="color: rgb(0, 0, 0); ">1</span>&nbsp;<span style="color: rgb(0, 0, 0); ">)&nbsp;a[i</span>&nbsp;<span style="color: rgb(0, 0, 0); ">/</span>&nbsp;<span style="color: rgb(0, 0, 0); ">2</span>&nbsp;<span style="color: rgb(0, 0, 0); ">]</span>&nbsp;<span style="color: rgb(0, 0, 0); ">++</span>&nbsp;<span style="color: rgb(0, 0, 0); ">;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;}</span>&nbsp;</span><span style="color: rgb(0, 0, 0); ">&nbsp;<br><img id="Codehighlighter1_269_397_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">int</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;cntLs(</span>&nbsp;<span style="color: rgb(0, 0, 255); ">int</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;n)</span>&nbsp;<span id="Codehighlighter1_269_397_Open_Text"><span style="color: rgb(0, 0, 0); ">{</span>&nbsp;<span style="color: rgb(0, 128, 0); ">//</span>&nbsp;<span style="color: rgb(0, 128, 0); ">统计小于&nbsp;</span>&nbsp;<span style="color: rgb(0, 128, 0); "><br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;</span><span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">int</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;i</span>&nbsp;<span style="color: rgb(0, 0, 0); ">=</span>&nbsp;<span style="color: rgb(0, 0, 0); ">N</span>&nbsp;<span style="color: rgb(0, 0, 0); ">+</span>&nbsp;<span style="color: rgb(0, 0, 0); ">n,c</span>&nbsp;<span style="color: rgb(0, 0, 0); ">=</span>&nbsp;<span style="color: rgb(0, 0, 0); ">0</span>&nbsp;<span style="color: rgb(0, 0, 0); ">;&nbsp;</span>&nbsp;<span style="color: rgb(0, 128, 0); ">//</span>&nbsp;<span style="color: rgb(0, 128, 0); ">若统计小于等于则c=a[i];&nbsp;</span>&nbsp;<span style="color: rgb(0, 128, 0); "><br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;</span><span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">for</span>&nbsp;<span style="color: rgb(0, 0, 0); ">(;&nbsp;i</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&gt;</span>&nbsp;<span style="color: rgb(0, 0, 0); ">1</span>&nbsp;<span style="color: rgb(0, 0, 0); ">;&nbsp;i</span>&nbsp;<span style="color: rgb(0, 0, 0); ">/=</span>&nbsp;<span style="color: rgb(0, 0, 0); ">2</span>&nbsp;<span style="color: rgb(0, 0, 0); ">)&nbsp;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">if</span>&nbsp;<span style="color: rgb(0, 0, 0); ">(i</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&amp;</span>&nbsp;<span style="color: rgb(0, 0, 0); ">1</span>&nbsp;<span style="color: rgb(0, 0, 0); ">)&nbsp;c</span>&nbsp;<span style="color: rgb(0, 0, 0); ">+=</span>&nbsp;<span style="color: rgb(0, 0, 0); ">a[i</span>&nbsp;<span style="color: rgb(0, 0, 0); ">/</span>&nbsp;<span style="color: rgb(0, 0, 0); ">2</span>&nbsp;<span style="color: rgb(0, 0, 0); ">];<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">return</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;c;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;}</span>&nbsp;</span><span style="color: rgb(0, 0, 0); "><br><img id="Codehighlighter1_419_450_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">int</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;cntGt(</span>&nbsp;<span style="color: rgb(0, 0, 255); ">int</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;n)</span>&nbsp;<span id="Codehighlighter1_419_450_Open_Text"><span style="color: rgb(0, 0, 0); ">{&nbsp;</span>&nbsp;<span style="color: rgb(0, 0, 255); ">return</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;size</span>&nbsp;<span style="color: rgb(0, 0, 0); ">-</span>&nbsp;<span style="color: rgb(0, 0, 0); ">a[N</span>&nbsp;<span style="color: rgb(0, 0, 0); ">+</span>&nbsp;<span style="color: rgb(0, 0, 0); ">n]</span>&nbsp;<span style="color: rgb(0, 0, 0); ">-</span>&nbsp;<span style="color: rgb(0, 0, 0); ">cntLs(n);&nbsp;}</span>&nbsp;</span><span style="color: rgb(0, 0, 0); ">&nbsp;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">}</span>&nbsp;</span><span style="color: rgb(0, 0, 0); ">;&nbsp;</span></div><span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span style="font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); "><br></span></span><span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span style="font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); ">　　嗯~~~为了解</span></span><span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span style="font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); "><a href="http://acm.zju.edu.cn/show_problem.php?pid=2425" style="color: rgb(0, 0, 0); ">http://acm.zju.edu.cn/show_problem.php?pid=2425</a></span></span><span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span style="font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); ">这一题，还是把上述两个扩展给实现了啦，果然不难：</span></span><div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); width: 566px; word-break: break-all; padding-top: 4px; background-color: rgb(238, 238, 238); font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; color: rgb(75, 75, 75); "><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"><span style="color: rgb(0, 0, 0); ">（接上）<br><img id="Codehighlighter1_24_135_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); ">&nbsp;del(</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;n)</span><span id="Codehighlighter1_24_135_Open_Text"><span style="color: rgb(0, 0, 0); ">{<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 0); ">!</span><span style="color: rgb(0, 0, 0); ">a[n</span><span style="color: rgb(0, 0, 0); ">+=</span><span style="color: rgb(0, 0, 0); ">N])</span><span style="color: rgb(0, 0, 255); ">return</span><span style="color: rgb(0, 0, 0); ">;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0); ">--</span><span style="color: rgb(0, 0, 0); ">size;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 0); ">--</span><span style="color: rgb(0, 0, 0); ">a[n];&nbsp;n</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;&nbsp;n</span><span style="color: rgb(0, 0, 0); ">/=</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">)<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 0); ">~</span><span style="color: rgb(0, 0, 0); ">n</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">)</span><span style="color: rgb(0, 0, 0); ">--</span><span style="color: rgb(0, 0, 0); ">a[n</span><span style="color: rgb(0, 0, 0); ">/</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">];<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0); "><br><img id="Codehighlighter1_141_197_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_141_197_Open_Text"><span style="color: rgb(0, 128, 0); ">/*</span><span style="color: rgb(0, 128, 0); ">&nbsp;&nbsp;解决：求点集中第i小的数(由0数起)<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;注意：如果i&gt;=size&nbsp;返回N-1&nbsp;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0); ">*/</span></span><span style="color: rgb(0, 0, 0); ">&nbsp;<br><img id="Codehighlighter1_225_366_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 255); ">operator</span><span style="color: rgb(0, 0, 0); ">[](</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;n)</span><span id="Codehighlighter1_225_366_Open_Text"><span style="color: rgb(0, 0, 0); ">{<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;i</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;<br><img id="Codehighlighter1_262_336_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">while</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">N)</span><span id="Codehighlighter1_262_336_Open_Text"><span style="color: rgb(0, 0, 0); ">{<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(n</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">a[i])&nbsp;i</span><span style="color: rgb(0, 0, 0); ">*=</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">else</span><span style="color: rgb(0, 0, 0); ">&nbsp;n</span><span style="color: rgb(0, 0, 0); ">-=</span><span style="color: rgb(0, 0, 0); ">a[i],&nbsp;i</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">i</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0); "><br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">return</span><span style="color: rgb(0, 0, 0); ">&nbsp;i</span><span style="color: rgb(0, 0, 0); ">-</span><span style="color: rgb(0, 0, 0); ">N;&nbsp;&nbsp;&nbsp;&nbsp;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">};&nbsp;&nbsp;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">附一个测试程序</span><span style="color: rgb(0, 128, 0); "><br><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: rgb(0, 0, 0); ">#include</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">iostream.h</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); "><br><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">T</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">8192</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">&nbsp;t;&nbsp;&nbsp;<br><img id="Codehighlighter1_431_666_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"></span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;main()</span><span id="Codehighlighter1_431_666_Open_Text"><span style="color: rgb(0, 0, 0); ">{<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">&nbsp;c;&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;n;<br><img id="Codehighlighter1_469_636_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">while</span><span style="color: rgb(0, 0, 0); ">(cin</span><span style="color: rgb(0, 0, 0); ">&gt;&gt;</span><span style="color: rgb(0, 0, 0); ">c)</span><span id="Codehighlighter1_469_636_Open_Text"><span style="color: rgb(0, 0, 0); ">{<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(c</span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">c</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">)&nbsp;t.clear();<br><img id="Codehighlighter1_507_629_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">else</span><span id="Codehighlighter1_507_629_Open_Text"><span style="color: rgb(0, 0, 0); ">{<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: rgb(0, 0, 0); ">&gt;&gt;</span><span style="color: rgb(0, 0, 0); ">n;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(c</span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">a</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">)&nbsp;t.add(n);<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(c</span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">d</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">)&nbsp;t.del(n);<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(c</span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">q</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">)&nbsp;cout</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">t[n]</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">endl;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">return</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">;<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">}</span></span><span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;</span></div><span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span style="font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); "><div><span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span style="font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); "><span  style="color: rgb(0, 0, 0); line-height: 24px; "><div>求点集里n的个数了!<div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); width: 593px; word-break: break-all; padding-top: 4px; background-color: rgb(238, 238, 238); "><img id="Codehighlighter1_16_37_Open_Image" src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;cntEQ(</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;n)</span><span id="Codehighlighter1_16_37_Open_Text"><span style="color: rgb(0, 0, 0); ">{<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">return</span><span style="color: rgb(0, 0, 0); ">&nbsp;a[N</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">n];<br><img src="http://www.cnitblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">}</span></span></div></div></span></span></span></div><br></span></span><span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span style="font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); ">P.S.：</span></span><span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span style="font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); "><br></span></span><span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span style="font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); ">　　在写完这篇文章一段时间后，我认识了另一种功能上比较类似的数据结构，叫做&#8220;树状数组&#8221;。它们有不少相似之处：</span></span><ul style="list-style-type: none; list-style-position: initial; list-style-image: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 5px; padding-left: 5px; font-size: 13px; text-align: left; font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; color: rgb(75, 75, 75); "><li>针对点集的处理（添加、删除、查找）；</li><li>相似的时空复杂度（logN时间，2N空间）；</li><li>相似的编程复杂度（都比线段树简短得多）；</li></ul><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(75, 75, 75); ">因此，所有可以用树状数组解决的问题都可以用这个&#8220;点树&#8221;来解决，另外它还有以下好处：</p><ul style="list-style-type: none; list-style-position: initial; list-style-image: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 5px; padding-left: 5px; font-size: 13px; text-align: left; font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; color: rgb(75, 75, 75); "><li>更直观的转移（个人感受，不一定要同意）；</li><li>同时支持自下而上和自上而下两种方向的查找和更新，而后者树状数组不支持，所以树状数组不提供某些功能，比如说O(logN)求点集中第k小数。</li></ul><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span style="font-family: verdana, Arial, helvetica, sans-seriff; line-height: 19px; font-size: 12px; color: rgb(48, 48, 48); ">posted on 2006-09-13 19:54&nbsp;<a href="http://www.cnitblog.com/cockerel/" style="color: rgb(0, 0, 0); ">踏雪赤兔</a></span>&nbsp;</p></div><img src ="http://www.cppblog.com/MiYu/aggbug/128568.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-04 16:11 <a href="http://www.cppblog.com/MiYu/archive/2010/10/04/128568.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Floyd-Warshall算法详解（转）</title><link>http://www.cppblog.com/MiYu/archive/2010/09/25/127655.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sat, 25 Sep 2010 12:03:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/09/25/127655.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/127655.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/09/25/127655.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/127655.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/127655.html</trackback:ping><description><![CDATA[<span  style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; ">Floyd-Warshall算法，简称Floyd算法，用于求解任意两点间的最短距离，时间复杂度为O(n^3)。我们平时所见的Floyd算法的一般形式如下：</span><div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); width: 1109px; word-break: break-all; padding-top: 4px; background-color: rgb(238, 238, 238); color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; line-height: 20px; "><span style="color: rgb(0, 128, 128); ">1</span>&nbsp;<span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); ">&nbsp;Floyd(){<br></span><span style="color: rgb(0, 128, 128); ">2</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;i,j,k;<br></span><span style="color: rgb(0, 128, 128); ">3</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(k</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;k</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;k</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">4</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;i</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;i</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">5</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(j</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;j</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;j</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">6</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(dist[i][k]</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">dist[k][j]</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">dist[i][j])<br></span><span style="color: rgb(0, 128, 128); ">7</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i][j]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">dist[i][k]</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">dist[k][j];<br></span><span style="color: rgb(0, 128, 128); ">8</span>&nbsp;<span style="color: rgb(0, 0, 0); ">}</span></div><p style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; ">&nbsp; 注意下第6行这个地方，如果dist[i][k]或者dist[k][j]不存在，程序中用一个很大的数代替。最好写成if(dist[i][k]!=INF &amp;&amp; dist[k][j]!=INF &amp;&amp; dist[i][k]+dist[k][j]&lt;dist[i][j])，从而防止溢出所造成的错误。<br>&nbsp; 上面这个形式的算法其实是Floyd算法的精简版，而真正的Floyd算法是一种基于DP(Dynamic Programming)的最短路径算法。<br>&nbsp; 设图G中n 个顶点的编号为1到n。令c [i, j, k]表示从i 到j 的最短路径的长度，其中k 表示该路径中的最大顶点，也就是说c[i,j,k]这条最短路径所通过的中间顶点最大不超过k。因此，如果G中包含边&lt;i, j&gt;，则c[i, j, 0] =边&lt;i, j&gt; 的长度；若i= j ，则c[i,j,0]=0；如果G中不包含边&lt;i, j&gt;，则c (i, j, 0)= +&#8734;。c[i, j, n] 则是从i 到j 的最短路径的长度。<br>&nbsp; 对于任意的k&gt;0，通过分析可以得到：中间顶点不超过k 的i 到j 的最短路径有两种可能：该路径含或不含中间顶点k。若不含，则该路径长度应为c[i, j, k-1]，否则长度为 c[i, k, k-1] +c [k, j, k-1]。c[i, j, k]可取两者中的最小值。<br>&nbsp; 状态转移方程：c[i, j, k]=min{c[i, j, k-1], c [i, k, k-1]+c [k, j, k-1]}，k＞0。<br>&nbsp; 这样，问题便具有了最优子结构性质，可以用动态规划方法来求解。</p><p style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; "></p><div align="center" src_cetemp="/images/cppblog_com/mythit/dist.jpg" style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; "><img height="225" alt="" src="http://www.cppblog.com/images/cppblog_com/mythit/dist.jpg" width="510" border="0"></div><span  style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; ">&nbsp; 为了进一步理解，观察上面这个有向图：若k=0, 1, 2, 3，则c[1,3,k]= +&#8734;；c[1,3,4]= 28；若k = 5, 6, 7，则c [1,3,k] = 10；若k=8, 9, 10，则c[1,3,k] = 9。因此1到3的最短路径长度为9。</span><span  style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; "><br></span><span  style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; ">&nbsp; 下面通过程序来分析这一DP过程，对应上面给出的有向图：</span><span  style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; "><br></span><p style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; ">&nbsp;</p><div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); width: 1109px; word-break: break-all; padding-top: 4px; background-color: rgb(238, 238, 238); color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; line-height: 20px; "><span style="color: rgb(0, 128, 128); ">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 0); ">#include&nbsp;</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">iostream</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); "><br></span><span style="color: rgb(0, 128, 128); ">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">using</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 255); ">namespace</span><span style="color: rgb(0, 0, 0); ">&nbsp;std;<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 0); "><br></span><span style="color: rgb(0, 128, 128); ">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">const</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;INF&nbsp;</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 0); ">100000</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;n</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">10</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">],dist[</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">];<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); ">&nbsp;init(){<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;i,j;<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;i</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;i</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(j</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;j</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;j</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">10</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map[i][j]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); ">j)</span><span style="color: rgb(0, 0, 0); ">?</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">:INF;<br></span><span style="color: rgb(0, 128, 128); ">11</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">4</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">20</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">5</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">12</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">4</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">8</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">4</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">6</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">6</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">13</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: rgb(0, 0, 0); ">4</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">7</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">4</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">5</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">7</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">5</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">8</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">14</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: rgb(0, 0, 0); ">6</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">7</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">8</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">8</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">6</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">15</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: rgb(0, 0, 0); ">8</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">10</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">9</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">7</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">10</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">9</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">16</span>&nbsp;<span style="color: rgb(0, 0, 0); ">}<br></span><span style="color: rgb(0, 128, 128); ">17</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); ">&nbsp;floyd_dp(){<br></span><span style="color: rgb(0, 128, 128); ">18</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;i,j,k;<br></span><span style="color: rgb(0, 128, 128); ">19</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;i</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;i</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">20</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(j</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;j</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;j</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">21</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i][j][</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">map[i][j];<br></span><span style="color: rgb(0, 128, 128); ">22</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(k</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;k</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;k</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">23</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;i</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;i</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">24</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(j</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;j</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;j</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">){<br></span><span style="color: rgb(0, 128, 128); ">25</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i][j][k]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">dist[i][j][k</span><span style="color: rgb(0, 0, 0); ">-</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">];<br></span><span style="color: rgb(0, 128, 128); ">26</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(dist[i][k][k</span><span style="color: rgb(0, 0, 0); ">-</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">dist[k][j][k</span><span style="color: rgb(0, 0, 0); ">-</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">dist[i][j][k])<br></span><span style="color: rgb(0, 128, 128); ">27</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i][j][k]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">dist[i][k][k</span><span style="color: rgb(0, 0, 0); ">-</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">dist[k][j][k</span><span style="color: rgb(0, 0, 0); ">-</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">];<br></span><span style="color: rgb(0, 128, 128); ">28</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: rgb(0, 128, 128); ">29</span>&nbsp;<span style="color: rgb(0, 0, 0); ">}<br></span><span style="color: rgb(0, 128, 128); ">30</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;main(){<br></span><span style="color: rgb(0, 128, 128); ">31</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;k,u,v;<br></span><span style="color: rgb(0, 128, 128); ">32</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;init();<br></span><span style="color: rgb(0, 128, 128); ">33</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;floyd_dp();<br></span><span style="color: rgb(0, 128, 128); ">34</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">while</span><span style="color: rgb(0, 0, 0); ">(cin</span><span style="color: rgb(0, 0, 0); ">&gt;&gt;</span><span style="color: rgb(0, 0, 0); ">u</span><span style="color: rgb(0, 0, 0); ">&gt;&gt;</span><span style="color: rgb(0, 0, 0); ">v,u</span><span style="color: rgb(0, 0, 0); ">||</span><span style="color: rgb(0, 0, 0); ">v){<br></span><span style="color: rgb(0, 128, 128); ">35</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(k</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">;k</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;k</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">){<br></span><span style="color: rgb(0, 128, 128); ">36</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(dist[u][v][k]</span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); ">INF)&nbsp;cout</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">+&#8734;</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">endl;<br></span><span style="color: rgb(0, 128, 128); ">37</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">else</span><span style="color: rgb(0, 0, 0); ">&nbsp;cout</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">dist[u][v][k]</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">endl;<br></span><span style="color: rgb(0, 128, 128); ">38</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: rgb(0, 128, 128); ">39</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: rgb(0, 128, 128); ">40</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">return</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">41</span>&nbsp;<span style="color: rgb(0, 0, 0); ">}</span></div><p style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; ">&nbsp; 输入 1 3<br>&nbsp; 输出 +&#8734;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +&#8734;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +&#8734;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +&#8734;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 28<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9</p><p style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; ">&nbsp; Floyd-Warshall算法不仅能求出任意2点间的最短路径，还可以保存最短路径上经过的节点。下面用精简版的Floyd算法实现这一过程，程序中的图依然对应上面的有向图。<br></p><div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); width: 1109px; word-break: break-all; padding-top: 4px; background-color: rgb(238, 238, 238); color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; line-height: 20px; "><span style="color: rgb(0, 128, 128); ">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 0); ">#include&nbsp;</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">iostream</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); "><br></span><span style="color: rgb(0, 128, 128); ">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">using</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 255); ">namespace</span><span style="color: rgb(0, 0, 0); ">&nbsp;std;<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 0); "><br></span><span style="color: rgb(0, 128, 128); ">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">const</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;INF&nbsp;</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 0); ">100000</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;n</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">10</span><span style="color: rgb(0, 0, 0); ">,path[</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">],dist[</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">],map[</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">11</span><span style="color: rgb(0, 0, 0); ">];<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); ">&nbsp;init(){<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;i,j;<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;i</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;i</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(j</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;j</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;j</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">10</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map[i][j]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); ">j)</span><span style="color: rgb(0, 0, 0); ">?</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">:INF;<br></span><span style="color: rgb(0, 128, 128); ">11</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">4</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">20</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">5</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">12</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">4</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">8</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">4</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">6</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">6</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">13</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: rgb(0, 0, 0); ">4</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">7</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">4</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">5</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">7</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">5</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">8</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">14</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: rgb(0, 0, 0); ">6</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">3</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">7</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">8</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">8</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">6</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">15</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: rgb(0, 0, 0); ">8</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">10</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">9</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">7</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">,map[</span><span style="color: rgb(0, 0, 0); ">10</span><span style="color: rgb(0, 0, 0); ">][</span><span style="color: rgb(0, 0, 0); ">9</span><span style="color: rgb(0, 0, 0); ">]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">16</span>&nbsp;<span style="color: rgb(0, 0, 0); ">}<br></span><span style="color: rgb(0, 128, 128); ">17</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); ">&nbsp;floyd(){<br></span><span style="color: rgb(0, 128, 128); ">18</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;i,j,k;<br></span><span style="color: rgb(0, 128, 128); ">19</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;i</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;i</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">20</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(j</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;j</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;j</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">21</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i][j]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">map[i][j],path[i][j]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">22</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(k</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;k</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;k</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">23</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;i</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;i</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">24</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(j</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">;j</span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); ">n;j</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)<br></span><span style="color: rgb(0, 128, 128); ">25</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(dist[i][k]</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">dist[k][j]</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">dist[i][j])<br></span><span style="color: rgb(0, 128, 128); ">26</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i][j]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">dist[i][k]</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">dist[k][j],path[i][j]</span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); ">k;<br></span><span style="color: rgb(0, 128, 128); ">27</span>&nbsp;<span style="color: rgb(0, 0, 0); ">}<br></span><span style="color: rgb(0, 128, 128); ">28</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); ">&nbsp;output(</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;i,</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;j){<br></span><span style="color: rgb(0, 128, 128); ">29</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(i</span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); ">j)&nbsp;</span><span style="color: rgb(0, 0, 255); ">return</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">30</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(path[i][j]</span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">)&nbsp;cout</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">j</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">31</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">else</span><span style="color: rgb(0, 0, 0); ">{<br></span><span style="color: rgb(0, 128, 128); ">32</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(i,path[i][j]);<br></span><span style="color: rgb(0, 128, 128); ">33</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(path[i][j],j);<br></span><span style="color: rgb(0, 128, 128); ">34</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: rgb(0, 128, 128); ">35</span>&nbsp;<span style="color: rgb(0, 0, 0); ">}<br></span><span style="color: rgb(0, 128, 128); ">36</span>&nbsp;<span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;main(){<br></span><span style="color: rgb(0, 128, 128); ">37</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;u,v;<br></span><span style="color: rgb(0, 128, 128); ">38</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;init();<br></span><span style="color: rgb(0, 128, 128); ">39</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;floyd();<br></span><span style="color: rgb(0, 128, 128); ">40</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">while</span><span style="color: rgb(0, 0, 0); ">(cin</span><span style="color: rgb(0, 0, 0); ">&gt;&gt;</span><span style="color: rgb(0, 0, 0); ">u</span><span style="color: rgb(0, 0, 0); ">&gt;&gt;</span><span style="color: rgb(0, 0, 0); ">v,u</span><span style="color: rgb(0, 0, 0); ">||</span><span style="color: rgb(0, 0, 0); ">v){<br></span><span style="color: rgb(0, 128, 128); ">41</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(dist[u][v]</span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); ">INF)&nbsp;cout</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">No&nbsp;path</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">endl;<br></span><span style="color: rgb(0, 128, 128); ">42</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">else</span><span style="color: rgb(0, 0, 0); ">{<br></span><span style="color: rgb(0, 128, 128); ">43</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">u</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 0); ">'</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">44</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(u,v);<br></span><span style="color: rgb(0, 128, 128); ">45</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">endl;<br></span><span style="color: rgb(0, 128, 128); ">46</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: rgb(0, 128, 128); ">47</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: rgb(0, 128, 128); ">48</span>&nbsp;<span style="color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255); ">return</span><span style="color: rgb(0, 0, 0); ">&nbsp;</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">;<br></span><span style="color: rgb(0, 128, 128); ">49</span>&nbsp;<span style="color: rgb(0, 0, 0); ">}</span></div><p style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; ">&nbsp; 输入 1 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp; 输出 1 2 5 8 6 3</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/127655.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-09-25 20:03 <a href="http://www.cppblog.com/MiYu/archive/2010/09/25/127655.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>多重背包O(N*V)算法详解（使用单调队列）(转载)</title><link>http://www.cppblog.com/MiYu/archive/2010/09/03/125762.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 03 Sep 2010 02:59:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/09/03/125762.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125762.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/09/03/125762.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125762.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125762.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 多重背包O(N*V)算法详解（使用单调队列）&nbsp;多重背包问题：有N种物品和容量为V的背包，若第i种物品，容量为v[i]，价值为w[i]，共有n[i]件。怎样装才能使背包内的物品总价值最大？&nbsp;网上关于&#8220;多重背包&#8221;的资料倒是不少，但是关于怎么实现O(N*V)算法的资料，真得好少呀，关于&#8220;单调队列&#8221;那部分算法，又没说明得很清楚，看了几遍没...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/09/03/125762.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/125762.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-09-03 10:59 <a href="http://www.cppblog.com/MiYu/archive/2010/09/03/125762.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>fill memset for 小测试</title><link>http://www.cppblog.com/MiYu/archive/2010/09/02/125610.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Thu, 02 Sep 2010 02:47:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/09/02/125610.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125610.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/09/02/125610.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125610.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125610.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;做ACM题目的时候 , 经常使用到 &nbsp;fill,&nbsp;memset &nbsp;,&nbsp;for&nbsp;操作对 数据进行初始化操作, 在测试数据不大,而且数组范围也不大的情况下,这几种操作的时间差距不是很明显. &nbsp;但是!...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/09/02/125610.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/125610.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-09-02 10:47 <a href="http://www.cppblog.com/MiYu/archive/2010/09/02/125610.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>学习笔记 -- 关于 STL 中的 heap ( 堆 )</title><link>http://www.cppblog.com/MiYu/archive/2010/09/01/125539.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 01 Sep 2010 09:18:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/09/01/125539.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125539.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/09/01/125539.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125539.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125539.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C ++ &nbsp;STL 中与heap 有关的操作有 如下几个 :&nbsp;			make_heap(), &nbsp; pop_heap(), &nbsp;push_heap(), &nbsp;sort_heap(), &nbsp;is_heap...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/09/01/125539.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/125539.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-09-01 17:18 <a href="http://www.cppblog.com/MiYu/archive/2010/09/01/125539.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>STL 学习笔记 ( 二. vector )</title><link>http://www.cppblog.com/MiYu/archive/2010/08/31/125459.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Tue, 31 Aug 2010 14:12:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/31/125459.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125459.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/31/125459.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125459.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125459.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vector&nbsp;&#8211;	一. &nbsp;vector可以模拟动态数组&nbsp;&#8211;	二. &nbsp;vector的元素可以是任意类型T，但必须具备赋值和拷贝能力（具有public&nbsp;&nbsp;&nbsp; &nb...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/08/31/125459.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/125459.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-31 22:12 <a href="http://www.cppblog.com/MiYu/archive/2010/08/31/125459.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>STL 学习笔记 ( 一. 概述 )</title><link>http://www.cppblog.com/MiYu/archive/2010/08/31/125455.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Tue, 31 Aug 2010 13:47:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/31/125455.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125455.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/31/125455.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125455.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125455.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &#8226;&nbsp;MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;几种常用的容器:&nbsp;map, vector, list, queue,stack, string ( 这个字符串也算个容器 )&nbsp;&nbsp;STL容器的共通操作&nbsp;&#8211;一. 初始化（initi...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/08/31/125455.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/125455.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-31 21:47 <a href="http://www.cppblog.com/MiYu/archive/2010/08/31/125455.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>标准模板库(STL)学习---List容器 ( 转载 )</title><link>http://www.cppblog.com/MiYu/archive/2010/08/30/125262.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Mon, 30 Aug 2010 03:31:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/30/125262.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125262.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/30/125262.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125262.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125262.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><div><font  color="#800080">定义一个list<br><br>　　我们可以象这样来定义一个STL的list：&nbsp;<br><br><table border="1" bordercolor="#ffcc66" width="90%" bgcolor="#dadacf" align="center"><tbody><tr><td>#include ＜string＞<br>#include ＜list＞<br>int main (void)&nbsp;<br>{<br>　list＜string＞ Milkshakes;<br>　return 0;<br>}</td></tr></tbody></table><br>　　这就行了，你已经定义了一个list。简单吗？list＜string＞ Milkshakes这句是你声明了list＜string＞模板类 的一个实例，然后就是实例化该类的一个对象。但是我们别急着做这个。在这一步其实你只需要知道你定义了 一个字符串的list。你需要包含提供STL list类的头文件。我用gcc 2.7.2在我的Linux上编译这个测试程序，例如：&nbsp;<br><br>　　g++ test1.cpp -o test1 注意iostream.h这个头文件已经被STL的头文件放弃了。这就是为什么这个例子中没有它的原因。&nbsp;<br><br>　　现在我们有了一个list，我们可以看实使用它来装东西了。我们将把一个字符串加到这个list里。有一个非常 重要的东西叫做list的值类型。值类型就是list中的对象的类型。在这个例子中,这个list的值类型就是字符串,string ， 这是因为这个list用来放字符串。&nbsp;<br><br>　　使用list的成员函数push_back和push_front插入一个元素到list中：<br><br><table border="1" bordercolor="#ffcc66" width="90%" bgcolor="#dadacf" align="center"><tbody><tr><td>#include ＜string＞<br>#include ＜list＞<br><br>int main (void)&nbsp;<br>{<br>　list＜string＞ Milkshakes;<br>　Milkshakes.push_back("Chocolate");<br>　Milkshakes.push_back("Strawberry");<br>　Milkshakes.push_front("Lime");<br>　Milkshakes.push_front("Vanilla");<br>　return 0;<br>}</td></tr></tbody></table><br>　　我们现在有个4个字符串在list中。list的成员函数push_back()把一个对象放到一个list的后面，而 push_front()把对象放到前面。我通常把一些错误信息push_back()到一个list中去，然后push_front()一个标题到list中， 这样它就会在这个错误消息以前打印它了。<br><br>The list member function empty()list的成员函数empty()<br><br>　　知道一个list是否为空很重要。如果list为空，empty()这个成员函数返回真。 我通常会这样使用它。通篇程序我都用push_back()来把错误消息放到list中去。然后，通过调用empty() 我就可以说出这个程序是否报告了错误。如果我定义了一个list来放信息，一个放警告，一个放严重错误， 我就可以通过使用empty()轻易的说出到底有那种类型的错误发生了。&nbsp;<br><br>　　我可以整理这些list，然后在打印它们之前，用标题来整理它们，或者把它们排序成类。&nbsp;<br><br><table border="1" bordercolor="#ffcc66" width="90%" bgcolor="#dadacf" align="center"><tbody><tr><td>/*<br>|| Using a list to track and report program messages and status&nbsp;<br>*/<br>#include ＜iostream.h＞<br>#include ＜string＞<br>#include ＜list＞<br>int main (void)&nbsp;<br>{<br>　#define OK 0&nbsp;<br>　#define INFO 1<br>　#define WARNING 2<br>　int return_code;<br>　list＜string＞ InfoMessages;<br>　list＜string＞ WarningMessages;<br><br>　// during a program these messages are loaded at various points<br>　InfoMessages.push_back("Info: Program started");<br>　// do work...<br>　WarningMessages.push_back("Warning: No Customer records have been found");<br>　// do work...<br><br>　return_code = OK;&nbsp;<br><br>　if (!InfoMessages.empty()) {<br>　　// there were info messages<br>　　InfoMessages.push_front("Informational Messages:");<br>　　// ... print the info messages list, we'll see how later<br>　　return_code = INFO;<br>　}<br><br>　if (!WarningMessages.empty()) {<br>　　// there were warning messages<br>　　WarningMessages.push_front("Warning Messages:");<br>　　// ... print the warning messages list, we'll see how later<br>　　return_code = WARNING;&nbsp;<br>　}<br><br>　// If there were no messages say so.<br>　if (InfoMessages.empty() &amp;&amp; WarningMessages.empty()) {<br>　　cout ＜＜ "There were no messages " ＜＜ endl;<br>　}<br><br>　return return_code;<br>}</td></tr></tbody></table><br></font></div><strong>用for循环来处理list中的元素</strong><br><br>　　我们想要遍历一个list，比如打印一个中的所有对象来看看list上不同操作的结果。要一个元素一个元素的遍历一个list， 我们可以这样做：&nbsp;<br><br><table border="1" bordercolor="#ffcc66" width="90%" bgcolor="#dadacf" align="center"><tbody><tr><td>/*<br>|| How to print the contents of a simple STL list. Whew!&nbsp;<br>*/<br>#include ＜iostream.h＞<br>#include ＜string＞<br>#include ＜list＞<br><br>int main (void)&nbsp;<br>{<br>　list＜string＞ Milkshakes;<br>　list＜string＞::iterator MilkshakeIterator;<br><br>　Milkshakes.push_back("Chocolate");<br>　Milkshakes.push_back("Strawberry");<br>　Milkshakes.push_front("Lime");<br>　Milkshakes.push_front("Vanilla");<br><br>　// print the milkshakes<br>　Milkshakes.push_front("The Milkshake Menu");<br>　Milkshakes.push_back("*** Thats the end ***");<br>　for (MilkshakeIterator=Milkshakes.begin(); MilkshakeIterator!=Milkshakes.end(); ++MilkshakeIterator)&nbsp;<br>　{<br>　　// dereference the iterator to get the element<br>　　cout ＜＜ *MilkshakeIterator ＜＜ endl;<br>　}&nbsp;<br>}</td></tr></tbody></table><br>　　这个程序定义了一个iterator，MilkshakeIterator。我们把它指向了这个list的第一个元素。 这可以调用Milkshakes.begin()来作到，它会返回一个指向list开头的iterator。然后我们把它和Milkshakes.end()的 返回值来做比较，当我们到了那儿的时候就停下来。&nbsp;<br><br>　　容器的end()函数会返回一个指向容器的最后一个位置的iterator。当我们到了那里，就停止操作。 我们不能不理容器的end()函数的返回值。我们仅知道它意味着已经处理到了这个容器的末尾，应该停止处理了。 所有的STL容器都要这样做。&nbsp;<br><br>　　在上面的例子中，每一次执行for循环，我们就重复引用iterator来得到我们打印的字符串。&nbsp;<br><br>　　在STL编程中，我们在每个算法中都使用一个或多个iterator。我们使用它们来存取容器中的对象。 要存取一个给定的对象，我们把一个iterator指向它，然后间接引用这个iterator。&nbsp;<br><br>　　这个list容器，就象你所想的，它不支持在iterator加一个数来指向隔一个的对象。 就是说，我们不能用Milkshakes.begin()+2来指向list中的第三个对象，因为STL的list是以双链的list来实现的， 它不支持随机存取。vector和deque(向量和双端队列)和一些其他的STL的容器可以支持随机存取。&nbsp;<br><br>　　上面的程序打印出了list中的内容。任何人读了它都能马上明白它是怎麽工作的。它使用标准的iterator和标准 的list容器。没有多少程序员依赖它里面装的东西， 仅仅是标准的C++。这是一个向前的重要步骤。这个例子使用STL使我们的软件更加标准。&nbsp;<br><br>　　<strong>用STL的通用算法for_each来处理list中的元素</strong><br><br>　　使用STL list和 iterator，我们要初始化、比较和给iterator增量来遍历这个容器。STL通用的for_each 算法能够减轻我们的工作。&nbsp;<br><br><table border="1" bordercolor="#ffcc66" width="90%" bgcolor="#dadacf" align="center"><tbody><tr><td>/*<br>|| How to print a simple STL list MkII<br>*/<br>#include ＜iostream.h＞<br>#include ＜string＞<br>#include ＜list＞<br>#include ＜algorithm＞<br><br>PrintIt (string&amp; StringToPrint) {<br>　cout ＜＜ StringToPrint ＜＜ endl;<br>}<br><br>int main (void) {<br>　list＜string＞ FruitAndVegetables;<br>　FruitAndVegetables.push_back("carrot");<br>　FruitAndVegetables.push_back("pumpkin");<br>　FruitAndVegetables.push_back("potato");<br>　FruitAndVegetables.push_front("apple");<br>　FruitAndVegetables.push_front("pineapple");<br><br>　for_each (FruitAndVegetables.begin(), FruitAndVegetables.end(), PrintIt);<br>}</td></tr></tbody></table><br>　　在这个程序中我们使用STL的通用算法for_each()来遍历一个iterator的范围，然后调用PrintIt()来处理每个对象。 我们不需要初始化、比较和给iterator增量。for_each()为我们漂亮的完成了这些工作。我们执行于对象上的 操作被很好的打包在这个函数以外了，我们不用再做那样的循环了，我们的代码更加清晰了。&nbsp;<br><br><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">　　for_each算法引用了iterator范围的概念，这是一个由起始iterator和一个末尾iterator指出的范围。 起始iterator指出操作由哪里开始，末尾iterator指明到哪结束，但是它不包括在这个范围内。</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;<strong>用STL的通用算法count()来统计list中的元素个数</strong></p><br>　　STL的通用算法count()和count_it()用来给容器中的对象记数。就象for_each()一样，count()和count_if() 算法也是在iterator范围内来做的。&nbsp;<br><br>　　让我们在一个学生测验成绩的list中来数一数满分的个数。这是一个整型的List。&nbsp;<br><br><table border="1" bordercolor="#ffcc66" width="90%" bgcolor="#dadacf" align="center"><tbody><tr><td>/*<br>|| How to count objects in an STL list<br>*/<br>#include ＜list＞<br>#include ＜algorithm＞<br>#<br>int main (void)&nbsp;<br>{<br>　list＜int＞ Scores;<br>　#<br>　Scores.push_back(100); Scores.push_back(80);<br>　Scores.push_back(45); Scores.push_back(75);<br>　Scores.push_back(99); Scores.push_back(100);<br>　#<br>　int NumberOf100Scores(0);&nbsp;<br>　count (Scores.begin(), Scores.end(), 100, NumberOf100Scores);<br>　#<br>　cout ＜＜ "There were " ＜＜ NumberOf100Scores ＜＜ " scores of 100" ＜＜ endl;<br>}</td></tr></tbody></table><br>　　count()算法统计等于某个值的对象的个数。上面的例子它检查list中的每个整型对象是不是100。每次容器中的对象等于100，它就给NumberOf100Scores加1。这是程序的输出：&nbsp;<br><br><table border="1" bordercolor="#ffcc66" width="90%" bgcolor="#dadacf" align="center"><tbody><tr><td>There were 2 scores of 100</td></tr></tbody></table><br>　　<strong>用STL的通用算法count_if()来统计list中的元素个数</strong><br><br>　　count_if()是count()的一个更有趣的版本。他采用了STL的一个新组件，函数对象。count_if() 带一个函数对象的参数。函数对象是一个至少带有一个operator()方法的类。有些STL算法作为参数接收 函数对象并调用这个函数对象的operator()方法。&nbsp;<br><br>　　函数对象被约定为STL算法调用operator时返回true或false。它们根据这个来判定这个函数。举个例子会 说的更清楚些。count_if()通过传递一个函数对象来作出比count()更加复杂的评估以确定一个对象是否应该被 记数。在这个例子里我们将数一数牙刷的销售数量。我们将提交包含四个字符的销售码和产品说明的销售记录。&nbsp;<br><br><table border="1" bordercolor="#ffcc66" width="90%" bgcolor="#dadacf" align="center"><tbody><tr><td>/*<br>|| Using a function object to help count things<br>*/<br>#include ＜string＞<br>#include ＜list＞<br>#include ＜algorithm＞<br><br>const string ToothbrushCode("0003");<br><br>class IsAToothbrush&nbsp;<br>{<br>　public:&nbsp;<br>　　bool operator() ( string&amp; SalesRecord )&nbsp;<br>　　{<br>　　　return SalesRecord.substr(0,4)==ToothbrushCode;<br>　　}&nbsp;<br>};<br><br>int main (void)&nbsp;<br>{<br>　list＜string＞ SalesRecords;<br><br>　SalesRecords.push_back("0001 Soap");<br>　SalesRecords.push_back("0002 Shampoo");<br>　SalesRecords.push_back("0003 Toothbrush");<br>　SalesRecords.push_back("0004 Toothpaste");<br>　SalesRecords.push_back("0003 Toothbrush");<br><br>　int NumberOfToothbrushes(0);&nbsp;<br>　count_if (SalesRecords.begin(), SalesRecords.end(),&nbsp;<br>　IsAToothbrush(), NumberOfToothbrushes);<br><br>　cout ＜＜ "There were "&nbsp;<br>　＜＜ NumberOfToothbrushes&nbsp;<br>　＜＜ " toothbrushes sold" ＜＜ endl;<br>}</td></tr></tbody></table><br>　　这是这个程序的输出：&nbsp;<br><br>　　There were 2 toothbrushes sold 这个程序是这样工作的：定义一个函数对象类IsAToothbrush，这个类的对象能判断出卖出的是否是牙刷 。如果这个记录是卖出牙刷的记录的话，函数调用operator()返回一个true，否则返回false。&nbsp;<br><br>　　count_if()算法由第一和第二两个iterator参数指出的范围来处理容器对象。它将对每个 IsAToothbrush()返回true的容器中的对象增加NumberOfToothbrushes的值。&nbsp;<br><br>　　最后的结果是NumberOfToothbrushes这个变量保存了产品代码域为"0003"的记录的个数，也就是牙刷的个数。&nbsp;<br><br>　　注意count_if()的第三个参数IsAToothbrush()，它是由它的构造函数临时构造的一个对象。你可以把IsAToothbrush类的一个临时对象 传递给count_if()函数。count_if()将对该容器的每个对象调用这个函数。&nbsp;<br><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><strong>使用count_if()的一个更加复杂的函数对象</strong><br><br>　　我们可以更进一步的研究一下函数对象。假设我们需要传递更多的信息给一个函数对象。我们不能通过 调用operator来作到这点，因为必须定义为一个list的中的对象的类型。 然而我们通过为IsAToothbrush指出一个非缺省的构造函数就可以用任何我们所需要的信息来初始化它了。 例如，我们可能需要每个牙刷有一个不定的代码。我们可以把这个信息加到下面的函数对象中：&nbsp;<br><br><table border="1" bordercolor="#ffcc66" width="90%" bgcolor="#dadacf" align="center"><tbody><tr><td>/*<br>|| Using a more complex function object<br>*/<br>#include ＜iostream.h＞<br>#include ＜string＞<br>#include ＜list＞<br>#include ＜algorithm＞<br><br>class IsAToothbrush&nbsp;<br>{<br>　public:<br>　　IsAToothbrush(string&amp; InToothbrushCode) :&nbsp;<br>　　ToothbrushCode(InToothbrushCode) {}<br>　　bool operator() (string&amp; SalesRecord)&nbsp;<br>　　{<br>　　　return SalesRecord.substr(0,4)==ToothbrushCode;<br>　　}&nbsp;<br>　private:<br>　　string ToothbrushCode;&nbsp;<br>};<br><br>int main (void)&nbsp;<br>{<br>　list＜string＞ SalesRecords;<br><br>　SalesRecords.push_back("0001 Soap");<br>　SalesRecords.push_back("0002 Shampoo");<br>　SalesRecords.push_back("0003 Toothbrush");<br>　SalesRecords.push_back("0004 Toothpaste");<br>　SalesRecords.push_back("0003 Toothbrush");<br><br>　string VariableToothbrushCode("0003");<br><br>　int NumberOfToothbrushes(0);&nbsp;<br>　count_if (SalesRecords.begin(), SalesRecords.end(),&nbsp;<br>　IsAToothbrush(VariableToothbrushCode),<br>　NumberOfToothbrushes);<br>　cout ＜＜ "There were "<br>　＜＜ NumberOfToothbrushes&nbsp;<br>　＜＜ " toothbrushes matching code "<br>　＜＜ VariableToothbrushCode<br>　＜＜ " sold"&nbsp;<br>　＜＜ endl;<br>}</td></tr></tbody></table><br>　　程序的输出是：&nbsp;<br><br>　　There were 2 toothbrushes matching code 0003 sold 这个例子演示了如何向函数对象传递信息。你可以定义任意你想要的构造函数，你可以再函数对象中做任何你 想做的处理，都可以合法编译通过。&nbsp;<br><br><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">　　你可以看到函数对象真的扩展了基本记数算法。&nbsp;</p></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/125262.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-30 11:31 <a href="http://www.cppblog.com/MiYu/archive/2010/08/30/125262.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>线段树入门 (zz)</title><link>http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sun, 29 Aug 2010 13:22:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125199.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125199.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125199.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 从简单说起，线段树其实可以理解成一种特殊的二叉树。但是这种二叉树较为平衡，和静态二叉树一样，都是提前已经建立好的树形结构。针对性强，所以效率要高。这里又想到了一句题外话：动态和静态的差别。动态结构较为灵活，但是速度较慢；静态结构节省内存，速度较快。接着回到线段树上来，线段树是建立在线段的基础上，每个结点都代表了一条线段&nbsp;[a , b]。长度为1的线段成为元线段。非元线段都有两个子结点，左...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/125199.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-29 21:22 <a href="http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>霍夫曼编码</title><link>http://www.cppblog.com/MiYu/archive/2010/08/27/124964.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 27 Aug 2010 11:20:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/27/124964.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124964.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/27/124964.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124964.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124964.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_04f99516-bbed-4a4d-a8ad-2cc60a559eaa" style="vertical-align: middle; padding-right: 5px; "><span class="cnblogs_code_collapse" style="border-right-color: rgb(128, 128, 128); border-right-width: 1px; border-right-style: solid; border-top-color: rgb(128, 128, 128); border-top-width: 1px; border-top-style: solid; border-left-color: rgb(128, 128, 128); border-left-width: 1px; border-left-style: solid; border-bottom-color: rgb(128, 128, 128); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(255, 255, 255); padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px; font-family: 'Courier New'; ">代码</span><div id="cnblogs_code_open_04f99516-bbed-4a4d-a8ad-2cc60a559eaa"><div><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">霍夫曼编码是一种被广泛应用而且非常有效的数据压缩技术，根据待压缩数据的特征，一个可压缩掉20</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%~</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">90</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">。这里考虑的数据指的是字符串序列。要理解霍夫曼编码，先要理解霍夫曼树，即最优二叉树，是一类带权路径长度最短的树。<br><br>路径是指从树中一个结点到另一个结点之间的通路，路径上的分支数目称为路径长度。<br><br>树的路径长度是从树根到每一个叶子之间的路径长度之和。结点的带权路径长度为从该结点到树根之间的路径长度与该结点权的乘积，树的带权路径长度为树中所有叶子结点的带权路径长度之和.<br><br>霍夫曼树是指所有叶子结点的二叉树中带权路径长度最小的二叉树.<br><br>当给定了n个叶子结点的权值后，构造出的最优二叉树的结点数目m就确定了，即m</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">2n</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,所以可用一维结构数组来存储最优二叉树<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">#define</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;MAXLEAFNUM&nbsp;50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*最优二叉树中最大叶子树目*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;node{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;ch;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">当前结点表示的字符，对于非叶子结点，此域不用</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;weight;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">当前结点的权值</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;parent;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">当前结点的父结点的下标，为0时表示无父结点</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;lchild,rchild;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">当前结点的左，右孩子结点的下标，为0时表示无孩子结点</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>}HuffmanTree[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;MAXLEAFNUM];<br><br><br>typedef&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">HuffmanCode[MAXLEAFNUM&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]；<br><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">创建最优二叉树</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;createHTree(HuffmanTree&nbsp;HT,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">c,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">w,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;n)<br><br>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">数组c[0..n-1]和w[0..n-1]存放了n个字符及其概率，构造霍夫树HT</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i,&nbsp;s1,&nbsp;s2;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(n&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">根据n个权值构造n棵只有根结点的二叉树</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;&nbsp;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].ch&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;c[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].weight&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;w[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].parent&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;HT[i].lchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;HT[i].rchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(;&nbsp;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">i)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].parent&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].lchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].rchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">构造霍夫曼树</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;&nbsp;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">从HT[1..i-1]中选择parent为0且weight最小的两棵树，其序号为s1和s2</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select(HT,i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,s1,s2);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[s1].parent&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[s2].parent&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].lchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;s1;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].rchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;s2;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].weight&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;HT[s1].weight&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;HT[s2].weight;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>}<br>复制代码<br>霍夫曼算法(构造靍夫曼树)<br>　　对应于霍夫曼树的算法也叫做霍夫曼算法。此算法的思想是：<br>　　（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）设给定的一组权值为{W1，W2，W3，&#8230;&#8230;Wn}，据此生成森林F</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">{T1，T2，T3，&#8230;&#8230;Tn},F&nbsp;中的每棵二叉树只有一个带权为W1的根节点（i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，&#8230;&#8230;n）。<br>　　（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）在F中选取两棵根节点的权值最小和次小的二叉树作为左右构造一棵新的二叉树，新二叉树根节点的权值为其左、右子树根节点的权值之和。<br>　　（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">3</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）在F中删除这两棵最小和次小的二叉树，同时将新生成的二叉树并入森林中。<br>　　（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">4</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）重复（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">3</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）过程直到F中只有一棵二叉树为止。<br>霍夫曼树的应用非常广，在不同的应用中叶子节点的权值可以作不同的解释。霍夫曼树应用于信息编码中，权值可以看成某个符号出现的频率；应用到判定过程中，权值可以看成某类数据出现的频率；应用到排序过程中，权值可以看成是已排好次序而等待合并的序列长度等。<br><br><br>应用霍夫曼编码<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;假设有一个包含100&nbsp;000个字符的数据文件要压缩存储。各字符在该文件中的出现频度见表1。仅有6种不同字符出现过，字符a出现了45000次。<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f<br><br>频度（千字）&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">45</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">13</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">12</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">16</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">9</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">5</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>固定代码字&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">001</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">010</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">011</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">100</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">101</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>变长代码字&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">101</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">100</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">111</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1101</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1100</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>表1&nbsp;&nbsp;&nbsp;一个字符编码问题。大小为100&nbsp;000个字符的一个数据文件仅包含字符a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">~</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">f，每个字符出现的频度如表中所示。如果对每个字符赋予一个三位的编码，则该文件可被编码为300000位。如果利用表中的可变长度编码，该文件可被编码为224000位。<br><br>可以用很多种方式来表示这样一个文件。采用固定长度编码，则需要三位二进制数字来表示六个字符：a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，b</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">001</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，&#8230;，f</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">101</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">。这种方法需要300&nbsp;000来对整个原文件编码。<br></span></div></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/124964.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-27 19:20 <a href="http://www.cppblog.com/MiYu/archive/2010/08/27/124964.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>常用算法讲解---迭代法</title><link>http://www.cppblog.com/MiYu/archive/2010/08/27/124963.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 27 Aug 2010 11:18:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/27/124963.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124963.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/27/124963.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124963.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124963.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;<span  style="font-family: 'Courier New'; font-size: 13px; "><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_f92fe2de-3c49-4e47-a431-e6cc4efc2d35" style="vertical-align: middle; padding-right: 5px; "><span class="cnblogs_code_collapse" style="border-right-color: rgb(128, 128, 128); border-right-width: 1px; border-right-style: solid; border-top-color: rgb(128, 128, 128); border-top-width: 1px; border-top-style: solid; border-left-color: rgb(128, 128, 128); border-left-width: 1px; border-left-style: solid; border-bottom-color: rgb(128, 128, 128); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(255, 255, 255); padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px; ">代码</span></span></p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><div id="cnblogs_code_open_f92fe2de-3c49-4e47-a431-e6cc4efc2d35"><div><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">要使计算机能完成人们预定的工作，首先必须为如何完成预定的工作设计一个算法，然后再根据算法编写程序。计算机程序要对问题的每个对象和处理规则给出正确详尽的描述，其中程序的数据结构和变量用来描述问题的对象，程序结构、函数和语句用来描述问题的算法。算法数据结构是程序的两个重要方面。<br>算法是问题求解过程的精确描述，一个算法由有限条可完全机械地执行的、有确定结果的指令组成。指令正确地描述了要完成的任务和它们被执行的顺序。计算机按算法指令所描述的顺序执行算法的指令能在有限的步骤内终止，或终止于给出问题的解，或终止于指出问题对此输入数据无解。<br>通常求解一个问题可能会有多种算法可供选择，选择的主要标准是算法的正确性和可靠性，简单性和易理解性。其次是算法所需要的存储空间少和执行更快等。<br>算法设计是一件非常困难的工作，经常采用的算法设计技术主要有迭代法、穷举搜索法、递推法、贪婪法、回溯法、分治法、动态规划法等等。另外，为了更简洁的形式设计和藐视算法，在算法设计时又常常采用递归技术，用递归描述算法。<br>一、迭代法&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;迭代法是用于求方程或方程组近似根的一种常用的算法设计方法。设方程为f(x)</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，用某种数学方法导出等价的形式x</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">g(x)，然后按以下步骤执行：<br>（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）&nbsp;&nbsp;&nbsp;选一个方程的近似根，赋给变量x0；<br>（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）&nbsp;&nbsp;&nbsp;将x0的值保存于变量x1，然后计算g(x1)，并将结果存于变量x0；<br>（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">3</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）&nbsp;&nbsp;&nbsp;当x0与x1的差的绝对值还小于指定的精度要求时，重复步骤（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）的计算。<br>若方程有根，并且用上述方法计算出来的近似根序列收敛，则按上述方法求得的x0就认为是方程的根。上述算法用C程序的形式表示为：<br>【算法】迭代法求方程的根<br>{&nbsp;&nbsp;&nbsp;&nbsp;x0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">初始近似根；<br>&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">x0；<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">g(x1)；&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">按特定的方程计算新的近似根</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;fabs(x0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">x1)</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">Epsilon)；<br>&nbsp;&nbsp;&nbsp;printf(&#8220;方程的近似根是</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">f\n&#8221;，x0)；<br>}<br>迭代算法也常用于求方程组的根，令<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">（x0，x1，&#8230;，xn</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）<br>设方程组为：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xi</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">gi(X)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(I</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，&#8230;，n</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br>则求方程组根的迭代算法可描述如下：<br>【算法】迭代法求方程组的根<br>&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x[i]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">初始近似根;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">do</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y[i]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">x[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x[i]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">gi(X);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(delta</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0.0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(fabs(y[i]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">x[i])</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">delta)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delta</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">fabs(y[i]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">x[i])；<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(delta</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">Epsilon)；<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;变量x[</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">d]的近似根是&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">f&#8221;，I，x[i])；<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;\n&#8221;)；<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;具体使用迭代法求根时应注意以下两种可能发生的情况：<br>（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）&nbsp;&nbsp;&nbsp;如果方程无解，算法求出的近似根序列就不会收敛，迭代过程会变成死循环，因此在使用迭代算法前应先考察方程是否有解，并在程序中对迭代的次数给予限制；<br>（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）&nbsp;&nbsp;&nbsp;方程虽然有解，但迭代公式选择不当，或迭代的初始近似根选择不合理，也会导致迭代失败。</span></div></div></div>
<img src ="http://www.cppblog.com/MiYu/aggbug/124963.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-27 19:18 <a href="http://www.cppblog.com/MiYu/archive/2010/08/27/124963.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>常用算法讲解---穷举搜索法</title><link>http://www.cppblog.com/MiYu/archive/2010/08/27/124962.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 27 Aug 2010 11:13:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/27/124962.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124962.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/27/124962.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124962.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124962.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt=""></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><div><font  color="#800080"><span  style="font-family: Verdana, Helvetica, Arial, sans-serif; color: rgb(68, 68, 68); line-height: 19px; font-size: 12px; border-collapse: collapse; "><div class="t_msgfontfix" style="word-wrap: break-word; line-height: normal; min-height: 100px; "><table cellspacing="0" cellpadding="0" style="word-wrap: break-word; empty-cells: show; border-collapse: collapse; line-height: normal; table-layout: fixed; margin-left: 1px; width: 1152px; "><tbody style="word-wrap: break-word; line-height: normal; "><tr style="word-wrap: break-word; line-height: normal; "><td class="t_msgfont" id="postmessage_9832" style="word-wrap: break-word; color: rgb(68, 68, 68); font: normal normal normal 12px/1.6em Verdana, Helvetica, Arial, sans-serif; line-height: 1.6em; font-size: 14px; ">穷举搜索法&nbsp;<br style="word-wrap: break-word; line-height: normal; ">&nbsp; &nbsp;&nbsp;&nbsp;穷举搜索法是对可能是解的众多候选解按某种顺序进行逐一枚举和检验，并从众找出那些符合要求的候选解作为问题的解。<br style="word-wrap: break-word; line-height: normal; ">【问题】&nbsp; &nbsp;将A、B、C、D、E、F这六个变量排成如图所示的三角形，这六个变量分别取[1，6]上的整数，且均不相同。求使三角形三条边上的变量之和相等的全部解。如图就是一个解。<br style="word-wrap: break-word; line-height: normal; ">程序引入变量a、b、c、d、e、f，并让它们分别顺序取1至6的证书，在它们互不相同的条件下，测试由它们排成的如图所示的三角形三条边上的变量之和是否相等，如相等即为一种满足要求的排列，把它们输出。当这些变量取尽所有的组合后，程序就可得到全部可能的解。细节见下面的程序。<br style="word-wrap: break-word; line-height: normal; ">【程序1】<div class="blockcode" style="word-wrap: break-word; line-height: normal; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 10px; padding-right: 0px; padding-bottom: 5px; padding-left: 10px; width: 586px; border-left-color: rgb(204, 204, 204); background-image: url(http://www.cppleyuan.com/images/default/codebg.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(247, 247, 247); overflow-x: hidden; overflow-y: hidden; background-position: 0px 0px; background-repeat: no-repeat repeat; "><div id="code0" style="word-wrap: break-word; line-height: normal; "><ol style="word-wrap: break-word; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 10px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; "># include &lt;stdio.h&gt;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">void main()<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">{&nbsp; &nbsp;int a,b,c,d,e,f;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;for (a=1;a&lt;=6;a++)&nbsp; &nbsp;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;for (b=1;b&lt;=6;b++)&nbsp; &nbsp;&nbsp; &nbsp;{<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (b==a)&nbsp; &nbsp;&nbsp; &nbsp;continue;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;for (c=1;c&lt;=6;c++)&nbsp; &nbsp;&nbsp; &nbsp;{<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (c==a)||(c==b)&nbsp; &nbsp;continue;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;for (d=1;d&lt;=6;d++)&nbsp; &nbsp;&nbsp; &nbsp;{<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (d==a)||(d==b)||(d==c)&nbsp; &nbsp; continue;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">for (e=1;e&lt;=6;e++)&nbsp; &nbsp;&nbsp; &nbsp;{<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;if (e==a)||(e==b)||(e==c)||(e==d)&nbsp; &nbsp; continue;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">f=21-(a+b+c+d+e);<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">if ((a+b+c==c+d+e))&amp;&amp;(a+b+c==e+f+a))&nbsp; &nbsp;{<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">printf(&#8220;%6d,a);<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;printf(&#8220;%4d%4d&#8221;,b,f);<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;printf(&#8220;%2d%4d%4d&#8221;,c,d,e);<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;scanf(&#8220;%*c&#8221;);<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;}</li></ol></div><em style="word-wrap: break-word; text-align: left; font-style: normal; line-height: normal; margin-left: 43px; color: rgb(102, 102, 102); font-size: 12px; cursor: pointer; ">复制代码</em></div>按穷举法编写的程序通常不能适应变化的情况。如问题改成有9个变量排成三角形，每条边有4个变量的情况，程序的循环重数就要相应改变。<br style="word-wrap: break-word; line-height: normal; ">&nbsp; &nbsp;对一组数穷尽所有排列，还有更直接的方法。将一个排列看作一个长整数，则所有排列对应着一组整数。将这组整数按从小到大的顺序排列排成一个整数，从对应最小的整数开始。按数列的递增顺序逐一列举每个排列对应的每个整数，这能更有效地完成排列的穷举。从一个排列找出对应数列的下一个排列可在当前排列的基础上作部分调整来实现。倘若当前排列为1，2，4，6，5，3，并令其对应的长整数为124653。要寻找比长整数124653更大的排列，可从该排列的最后一个数字顺序向前逐位考察，当发现排列中的某个数字比它前一个数字大时，如本例中的6比它的前一位数字4大，这说明还有对应更大整数的排列。但为了顺序从小到大列举出所有的排列，不能立即调整得太大，如本例中将数字6与数字4交换得到的排列126453就不是排列124653的下一个排列。为了得到排列124653的下一个排列，应从已经考察过的那部分数字中选出比数字大，但又是它们中最小的那一个数字，比如数字5，与数字4交换。该数字也是从后向前考察过程中第一个比4大的数字。5与4交换后，得到排列125643。在前面数字1，2，5固定的情况下，还应选择对应最小整数的那个排列，为此还需将后面那部分数字的排列顺序颠倒，如将数字6，4，3的排列顺序颠倒，得到排列1，2，5，3，4，6，这才是排列1，2，4，6，5，3的下一个排列。按以上想法编写的程序如下。<br style="word-wrap: break-word; line-height: normal; ">【程序2】<div class="blockcode" style="word-wrap: break-word; line-height: normal; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 10px; padding-right: 0px; padding-bottom: 5px; padding-left: 10px; width: 586px; border-left-color: rgb(204, 204, 204); background-image: url(http://www.cppleyuan.com/images/default/codebg.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(247, 247, 247); overflow-x: hidden; overflow-y: hidden; background-position: 0px 0px; background-repeat: no-repeat repeat; "><div id="code1" style="word-wrap: break-word; line-height: normal; "><ol style="word-wrap: break-word; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 10px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; "># include &lt;stdio.h&gt;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; "># define SIDE_N&nbsp; &nbsp;3<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; "># define LENGTH&nbsp; &nbsp;3<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; "># define VARIABLES&nbsp; &nbsp;6<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">int A,B,C,D,E,F;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">int *pt[]={&amp;A,&amp;B,&amp;C,&amp;D,&amp;E,&amp;F};<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">int *side[SIDE_N][LENGTH]={&amp;A,&amp;B,&amp;C,&amp;C,&amp;D,&amp;E,&amp;E,&amp;F,&amp;A};<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">int side_total[SIDE_N];<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">main{}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">{&nbsp; &nbsp;int i,j,t,equal;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;for (j=0;j&lt;VARIABLES;j++)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;*pt[j]=j+1;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;while(1)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;{&nbsp; &nbsp;for (i=0;i&lt;SIDE_N;i++)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;{&nbsp; &nbsp;for (t=j=0;j&lt;LENGTH;j++)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;t+=*side[i][j];<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;side_total[i]=t;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;for (equal=1,i=0;equal&amp;&amp;i&lt;SIDE_N-1;i++)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (side_total[i]!=side_total[i+1]&nbsp; &nbsp;equal=0;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;if (equal)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;{&nbsp; &nbsp;for (i=1;i&lt;VARIABLES;i++)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;printf(&#8220;%4d&#8221;,*pt[i]);<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;printf(&#8220;\n&#8221;);<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;scanf(&#8220;%*c&#8221;);<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;for (j=VARIABLES-1;j&gt;0;j--)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (*pt[j]&gt;*pt[j-1])&nbsp; &nbsp;break;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;if (j==0)&nbsp; &nbsp;break;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;for (i=VARIABLES-1;i&gt;=j;i--)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (*pt[i]&gt;*pt[i-1])&nbsp; &nbsp;break;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;t=*pt[j-1];* pt[j-1] =* pt[i]; *pt[i]=t;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;for (i=VARIABLES-1;i&gt;j;i--,j++)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;{&nbsp; &nbsp;t=*pt[j]; *pt[j] =* pt[i]; *pt[i]=t;&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">}</li></ol></div><em style="word-wrap: break-word; text-align: left; font-style: normal; line-height: normal; margin-left: 43px; color: rgb(102, 102, 102); font-size: 12px; cursor: pointer; ">复制代码</em></div>从上述问题解决的方法中，最重要的因素就是确定某种方法来确定所有的候选解。下面再用一个示例来加以说明。<br style="word-wrap: break-word; line-height: normal; ">【问题】&nbsp; &nbsp;背包问题<br style="word-wrap: break-word; line-height: normal; ">问题描述：有不同价值、不同重量的物品n件，求从这n件物品中选取一部分物品的选择方案，使选中物品的总重量不超过指定的限制重量，但选中物品的价值之和最大。<br style="word-wrap: break-word; line-height: normal; ">设n个物品的重量和价值分别存储于数组w[ ]和v[ ]中，限制重量为tw。考虑一个n元组（x0，x1，&#8230;，xn-1），其中xi=0 表示第i个物品没有选取，而xi=1则表示第i个物品被选取。显然这个n元组等价于一个选择方案。用枚举法解决背包问题，需要枚举所有的选取方案，而根据上述方法，我们只要枚举所有的n元组，就可以得到问题的解。<br style="word-wrap: break-word; line-height: normal; ">显然，每个分量取值为0或1的n元组的个数共为2n个。而每个n元组其实对应了一个长度为n的二进制数，且这些二进制数的取值范围为0～2n-1。因此，如果把0～2n-1分别转化为相应的二进制数，则可以得到我们所需要的2n个n元组。<br style="word-wrap: break-word; line-height: normal; ">【算法】<div class="blockcode" style="word-wrap: break-word; line-height: normal; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 10px; padding-right: 0px; padding-bottom: 5px; padding-left: 10px; width: 586px; border-left-color: rgb(204, 204, 204); background-image: url(http://www.cppleyuan.com/images/default/codebg.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(247, 247, 247); overflow-x: hidden; overflow-y: hidden; background-position: 0px 0px; background-repeat: no-repeat repeat; "><div id="code2" style="word-wrap: break-word; line-height: normal; "><ol style="word-wrap: break-word; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 10px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">maxv=0;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">for (i=0;i&lt;2n;i++)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">{&nbsp; &nbsp;B[0..n-1]=0;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;把i转化为二进制数，存储于数组B中;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;temp_w=0;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;temp_v=0;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;for (j=0;j&lt;n;j++)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;{&nbsp; &nbsp;if (B[j]==1)<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;{&nbsp; &nbsp;temp_w=temp_w+w[j];<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;temp_v=temp_v+v[j];<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;if ((temp_w&lt;=tw)&amp;&amp;(temp_v&gt;maxv))<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;{&nbsp; &nbsp;maxv=temp_v;<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;保存该B数组；<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">&nbsp; &nbsp;}<br style="word-wrap: break-word; line-height: normal; "></li><li style="word-wrap: break-word; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; line-height: 1.8em; list-style-type: decimal-leading-zero; font-family: Monaco, Consolas, '&quot;Lucida Console&quot;', '&quot;Courier New&quot;', serif; font-size: 12px; ">}</li></ol></div><em style="word-wrap: break-word; text-align: left; font-style: normal; line-height: normal; margin-left: 43px; color: rgb(102, 102, 102); font-size: 12px; cursor: pointer; ">复制代码</em></div></td></tr></tbody></table></div><div id="post_rate_div_9832" style="word-wrap: break-word; line-height: normal; "></div><div class="useraction" style="word-wrap: break-word; line-height: normal; height: 50px; width: 110px; clear: both; display: block; margin-top: 20px; margin-right: auto; margin-bottom: 20px; margin-left: auto; "></div></span></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/124962.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-27 19:13 <a href="http://www.cppblog.com/MiYu/archive/2010/08/27/124962.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 部分分类</title><link>http://www.cppblog.com/MiYu/archive/2010/08/26/124804.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Thu, 26 Aug 2010 04:08:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/26/124804.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124804.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/26/124804.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124804.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124804.html</trackback:ping><description><![CDATA[
<span style="border-collapse: collapse; font-family: arial; font-size: 14px; line-height: 21px; "><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">模拟题, 枚举<br>1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 1202 1205 1209 1212(大数取模) 1216（链表）1218 1219 1225 1228 1229 1230 1234 1235 1236 1237 1239 1250<br>1256 1259 1262 1263 1265 1266 1276 1279 1282 1283 1287 1296 1302 1303 1304 1305 1306 1309 1311 1314<br>复杂模拟</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">搜索，递归求解<br>1010 1016 1026 1043(双广) 1044 (BFS+DFS) 1045 1067 1072 1104 1175 1180 1195 1208 1226 1238 1240 1241 1242 1258 1271 1312 1317<br>博奕<br>1079</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">动态规划<br>1003 1024 1025 1028 1051 1058 1059 1069 1074 1078 1080 1081 1085 1087 1114 1158 1159 1160 1171 1176 1181 1203 1224 1227 1231 1244 1248 1253 1254 1283 1300</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">数学，递推，规律<br>1005 1006 1012 1014 1018 1019 1021 1023 1027 1030 1032 1038 1041 1046 1059 1060 1061 1065 1066 1071(微积分) 1097 1098 1099 1100 1108 1110 1112 1124 1130 1131 1132 1134 1141 1143 1152 1155(物理题) 1163 1165 1178 1194 1196(lowbit) 1210 1214 1200 1221 1223 1249 1261 1267 1273 1290 1291 1292 1294 1297 1313 1316<br>数论&nbsp;<br>1164 1211 1215 1222 1286 1299</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">计算几何<br>1086 1115 1147&nbsp;<br>贪心<br>1009 1052 1055 1257</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">并查集<br>1198 1213 1232 1272&nbsp;<br>线段树,离散化<br>1199 1255&nbsp;<br>图论<br>最短路相关的问题 1142 1162 1217 1301&nbsp;<br>二分图问题 1054 1068 1150 1151 1281<br>其他<br>1053 (huffman) 1102(MST) 1116（欧拉回路） 1233(MST) 1269（强连通）<br>数据结构<br>1103（堆+模拟）1166（数状树组）1247 1251 1285（Topol） 1298<br>汉诺塔系列<br>1207&nbsp;<br>最近顶点对 1007</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">1500 DP<br>1501 DP<br>1502 DP or 记忆化<br>1503 DP<br>1504 模拟<br>1505 DP<br>1506 DP<br>1507 2分匹配<br>1508 记忆化容易点<br>1509 模拟<br>1510 DP<br>1511 搜索可以过<br>1512 左偏树<br>1513 DP<br>1514 DP<br>1515 DFS<br>1516 DP<br>1517 博奕<br>1518 搜索<br>1519 DP（不确定）<br>1520 树状DP<br>1521 数学题，母函数什么的。其实都可以过<br>1522 稳定婚姻<br>1523 DP<br>1524 博弈<br>1525 博弈<br>1526 Maxflow<br>1527 博弈<br>1528 2分匹配<br>1529 简单题<br>1530 最大团<br>1531 差分约束<br>1532 Maxflow 入门题<br>1533 KM Or 最小费用流<br>1534 差分约束<br>1535 差分约束<br>1536 博弈<br>1537 模拟 加置换群的理论 CODE可以短些，其实没必要。。。<br>1538 很有意思的题目。据说是Microsoft亚洲总裁面试的题目<br>1539 搜索<br>1540 线段树<br>1541 树状数组<br>1542 离散，线段树<br>1543 线段树<br>1544 简单的<br>1545 DP&nbsp;<a target="_blank" style="color: rgb(76, 125, 8); text-decoration: none; ">http://acm.hdu.edu.cn/forum/htm_data/18/0608/2050.html</a><br>1546 搜索<br>1547 模拟<br>1548 模拟&nbsp;<br>1551 2分答案<br>1553&nbsp;<br>1554<br>1555 简单<br>1556 技巧。数学<br>1557 搜索<br>1558 并查 + 线段判交<br>1559 DP<br>1560 减支 + 搜索<br>1561 树状DP<br>1562 暴力 between 1000 and 9999<br>1563 简单<br>1564 博弈。<br>1565 状态DP<br>1566 数学<br>1567 模拟<br>1568 大数<br>1569 最小割<br>1570 数学<br>1571 最段路<br>1572 搜索<br>1573 数学<br>1574 DP<br>1575 2分<br>1576 数论<br>1577 模拟，处理精度<br>1579 记忆化<br>1580 DP<br>1582 搜索&nbsp;<br>1583 模拟<br>1584 搜索<br>1585&nbsp;<br>1586<br>1587 简单题目<br>1591 模拟<br>1592 简单<br>1593 数学<br>1594 数学<br>1595 图论<br>1596 图论<br>1597 图论<br>1598 图论<br>1599 图论</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><br></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span  style="border-collapse: separate; font-family: verdana, sans-serif; "><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">基础题：1000、1001、1004、1005、1008、1012、1013、1014、1017、1019、1021、1028、1029、1032、1037、1040、1048、1056、1058、1061、1070、1076、1089、1090、1091、1092、1093、1094、1095、1096、1097、1098、1106、1108、1157、1163、1164、1170、1194、1196、1197、1201、1202、1205、1219、1234、1235、1236、1248、1266、1279、1282、1283、1302、1303、1323、1326、1330、1334、1335、1339、1390、1391、1393、1395、1397、1405、1406、1407、1408、1412、1418、1420、1465、1491、1555、1562、1563、1570、1587、1673、1678、1708、1718、1720、1785、1799、1859、1862、1877、1898、1976、1977、1985、1994、2000、2001、2002、2003、2004、2005、2006、2007、2008、2009、2010、2011、2012、2013、2014、2015、2016、2017、2018、2019、2020、2021、2022、2023、2024、2025、2026、2027、2028、2029、2030、2031、2032、2033、2034、2035、2039、2040、2042、2043、2048、2049、2051、2053、2055、2056、2057、2060、2061、2071、2073、2075、2076、2078、2081、2083、2088、2090、2092、2093、2095、2096、2097、2098、2099、2101、2103、2106、2107、2109、2113、2114、2115、2123、2131、2132、2133、2135、2136、2137、2138、2139、2143、2148、2153、2156、2161、2162、2164、2178、2186、2192、2200、2201、2212、2304、2309、2317、2401、2500、2502、2503、2504、2519、2520、2521、2523、2524、2535、2537、2539、2547、2548、2549、2550、2551、2552、2555、2560、2561、2562、2566、2567、2568、2700、2710、</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><br>DP：1003、1024、1029、1069、1074、1087、1114、1159、1160、1171、1176、1203、1231、1257、1260、1284、1421、1789、1978、2059、2084、2159、2191、2544、2571、2602、2709、</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">搜索：1010、1015、1016、1026、1072、1075、1175、1180、1181、1238、1239、1240、1241、1242、1253、1254、1312、1372、1548、1597、1671、1677、1728、1800、1983、2102、2141、2553、2563、2605、2612、2614、1616、2717</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">贪心：1009、1045、1049、1050、1051、1052、1257、1800、2037、2111、2124、2187、2391、2570</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">数学题：1018、1065、1071、1115、1141、1162、1212、1220、1492、1593、1701、1722、1798、1840、1999、2036、2080、2086、2089、2105、2108、2134、2303、2393、2438、2529、2547、2548、2552、2554、2601、2603、2701、</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">递推：1133、1143、1207、1249、1267、1284、1290、1297、1396、1992、1995、1996、2013、2014、2044、2045、2046、2047、2050、2064、2065、2067、2068、2070、2077、2085、2151、2154、2160、2190、2501、2512、2563、2569、2709、2716、</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">字符串：1020、1039、1043、1062、1073、1075、1088、1113、1161、1200、1251、1256、1288、1321、1328、1379、1804、1860、1982、1984、2017、2024、2025、2026、2027、2043、2052、2054、2072、2074、2087、2131、2137、2140、2163、2203、2206、2352、2500、2549、2564、2565、2567、2572、2609、2607、2707、2708、2719、2721、2723、</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">大数：1002、1042、1133、1250、1297、1715、1753、1865、2100、</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">胡搞：1022、1027、1030、1035、1128、1165、1209、1210、1215、1222、1228、1229、1230、1237、1259、1276、1286、1337、1342、1361、1370、1506、1577、1597、1702、1716、1727、1868、1870、1896、1981、1986、1987、1988、1997、1998、1999、2058、2062、2089、2090、2094、2104、2116、2117、2135、2175、2183、2184、2197、2303、2368、2370、2374、2511、2522、2527、2600、2615、2703、2711、2714、2715、2725、</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">博弈：1077、1404、1517、1524、1525、1527、1536、1564、1729、1730、1846、1847、1848、1849、1850、2147、2149、2176、2177、2188</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">母函数：1085、1171、1398、2079、2082、2110、2152、2189、2566、</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">hash：1264、1280、1425、1496、1800、2522、2600、</p></span></p></span><img src ="http://www.cppblog.com/MiYu/aggbug/124804.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-26 12:08 <a href="http://www.cppblog.com/MiYu/archive/2010/08/26/124804.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>树状数组 小结</title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124726.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 12:19:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124726.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124726.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124726.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124726.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124726.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">今天学习了 树状数组, 自己小结下 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><span style="font-family: 'Times New Roman'; line-height: normal; font-size: medium; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">简单介绍下 树状数组 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; 　　&nbsp;<span style="font-family: 宋体; font-size: 10pt; ">树状数组是一个查询和修改复杂度都为</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">log(n)</span><span style="font-family: 宋体; font-size: 10pt; ">的数据结构，假设数组</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">a[1...n]</span><span style="font-family: 宋体; font-size: 10pt; ">，那么查询</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">a[1] +&nbsp;</span><span style="font-family: 宋体; font-size: 10pt; ">&#8230;&#8230;</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp;+ a[i]</span><span style="font-family: 宋体; font-size: 10pt; ">　的时间是</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">log</span><span style="font-family: 宋体; font-size: 10pt; ">级别的，而且是一个在线的数据结构，支持随时修改某个元素的值，复杂度也为</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">log</span><span style="font-family: 宋体; font-size: 10pt; ">级别。</span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">来观察一下这个图：</span><span lang="EN-US" style="font-family: Arial; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: Arial; font-size: 12pt; ">&nbsp;<o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><font face="Arial" size="3"><span style="font-family: 'Courier New'; font-size: 13px; "><img src="http://images.cnblogs.com/cnblogs_com/miyu/3.jpg" border="0" alt="" width="462" height="293" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><br></span></font></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: Arial; font-size: 12pt; ">&nbsp;<o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">令这棵树的结点编号为</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C1</span><span style="font-family: 宋体; font-size: 10pt; ">，</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C2</span><span style="font-family: 宋体; font-size: 10pt; ">&#8230;&#8230;</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">Cn</span><span style="font-family: 宋体; font-size: 10pt; ">。令每个结点的值为这棵树的值的总和，那么容易发现：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C1 = A1</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C2 = A1 + A2</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C3 = A3</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C4 = A1 + A2 + A3 + A4</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C5 = A5</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C6 = A5 + A6</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C7 = A7</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">&#8230;&#8230;</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C16 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 + A11 + A12 + A13 + A14 + A15 + A16</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">这里有一个有趣的性质，下午推了一下发现：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">设节点编号为</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">x</span><span style="font-family: 宋体; font-size: 10pt; ">，那么这个节点管辖的区间为</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">2^k</span><span style="font-family: 宋体; font-size: 10pt; ">（其中</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">k</span><span style="font-family: 宋体; font-size: 10pt; ">为</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">x</span><span style="font-family: 宋体; font-size: 10pt; ">二进制末尾</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">0</span><span style="font-family: 宋体; font-size: 10pt; ">的个数）个元素。因为这个区间最后一个元素必然为</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">Ax</span><span style="font-family: 宋体; font-size: 10pt; ">，所以很明显：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">Cn = A(n &#8211; 2^k + 1) +&nbsp;</span><span style="font-family: 宋体; font-size: 10pt; ">&#8230;&#8230;</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">&nbsp;+ An</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">算这个</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">2^k</span><span style="font-family: 宋体; font-size: 10pt; ">有一个快捷的办法，定义一个函数如下即可：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">int lowbit(int x){</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 21.75pt; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">return x &amp; (x ^ (x &#8211; 1));</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">}</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">当想要查询一个</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">SUM(n)</span><span style="font-family: 宋体; font-size: 10pt; ">时，可以依据如下算法即可：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">step1:</span><span style="font-family: 宋体; font-size: 10pt; ">　令</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">sum = 0</span><span style="font-family: 宋体; font-size: 10pt; ">，转第二步；</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">step2:</span><span style="font-family: 宋体; font-size: 10pt; ">　假如</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">n &lt;= 0</span><span style="font-family: 宋体; font-size: 10pt; ">，算法结束，返回</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">sum</span><span style="font-family: 宋体; font-size: 10pt; ">值，否则</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">sum = sum + Cn</span><span style="font-family: 宋体; font-size: 10pt; ">，转第三步；</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">step3:<span style="font-family: 'Courier New'; ">&nbsp;&nbsp;</span></span><span style="font-family: 宋体; font-size: 10pt; ">令</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">n = n &#8211; lowbit(n)</span><span style="font-family: 宋体; font-size: 10pt; ">，转第二步。</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">可以看出，这个算法就是将这一个个区间的和全部加起来，为什么是效率是</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">log(n)</span><span style="font-family: 宋体; font-size: 10pt; ">的呢？以下给出证明：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">n = n &#8211; lowbit(n)</span><span style="font-family: 宋体; font-size: 10pt; ">这一步实际上等价于将</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">n</span><span style="font-family: 宋体; font-size: 10pt; ">的二进制的最后一个</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">1</span><span style="font-family: 宋体; font-size: 10pt; ">减去。而</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">n</span><span style="font-family: 宋体; font-size: 10pt; ">的二进制里最多有</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">log(n)</span><span style="font-family: 宋体; font-size: 10pt; ">个</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">1</span><span style="font-family: 宋体; font-size: 10pt; ">，所以查询效率是</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">log(n)</span><span style="font-family: 宋体; font-size: 10pt; ">的。</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">那么修改呢，修改一个节点，必须修改其所有祖先，最坏情况下为修改第一个元素，最多有</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">log(n)</span><span style="font-family: 宋体; font-size: 10pt; ">的祖先。所以修改算法如下（给某个结点</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">i</span><span style="font-family: 宋体; font-size: 10pt; ">加上</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">x</span><span style="font-family: 宋体; font-size: 10pt; ">）：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">step1:&nbsp;</span><span style="font-family: 宋体; font-size: 10pt; ">当</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">i &gt; n</span><span style="font-family: 宋体; font-size: 10pt; ">时，算法结束，否则转第二步；</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">step2: Ci = Ci + x</span><span style="font-family: 宋体; font-size: 10pt; ">，</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">&nbsp;i = i + lowbit(i)</span><span style="font-family: 宋体; font-size: 10pt; ">转第一步。</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">i = i +lowbit(i)</span><span style="font-family: 宋体; font-size: 10pt; ">这个过程实际上也只是一个把末尾</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">1</span><span style="font-family: 宋体; font-size: 10pt; ">补为</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">0</span><span style="font-family: 宋体; font-size: 10pt; ">的过程。</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">//</span><span style="font-family: 宋体; font-size: 10pt; ">修改过程必须满足减法规则！</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">所以整个程序如下：</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 新宋体; font-size: 10pt; color: blue; "></span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">const int MAX = 50000;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#define lowbit(x) ((x)&amp;(-x))</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int com[ MAX + 1 ],N,T;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">void modify ( int pos, int val ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; while ( pos &lt;= N ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;com[pos] += val;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos = pos + lowbit(pos); &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int quy ( int x ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;int sum = 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( x &gt; 0 ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + com[x];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = x - lowbit(x);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return sum;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 新宋体; font-size: 10pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><font face="新宋体" size="3"><span style="font-family: 'Courier New'; font-size: 13px; ">初始化 : &nbsp;　　&nbsp;for ( int i = 1; i &lt;= N; ++ i ){</span></font></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><font face="'Courier New'" size="3"><span style="font-family: 'Courier New'; font-size: 13px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf ( "%d",&amp;x );</span></font></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><font face="'Courier New'" size="3"><span style="font-family: 'Courier New'; font-size: 13px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modify ( i, x );&nbsp;</span></font></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><font face="'Courier New'" size="3"><span style="font-family: 'Courier New'; font-size: 13px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</span></font></p></span><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">------------------------------------------------------------------------------------</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-size: 24pt; ">为什么要用树状数组 ?</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-size: 24pt; ">原因如下:&nbsp;</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p:colorscheme colors="#ffffff,#000000,#969696,#000000,#00cc99,#3333cc,#ccccff,#b2b2b2"><div v:shape="_x0000_s1026" class="O"><div><span style="font-family: 'Times New Roman'; font-size: 22px; "><span style="color: rgb(255, 51, 0); position: absolute; left: -3.47656%; top: 0.39em; font-family: Wingdings; font-size: 15px; ">|</span></span><span style="font-family: 宋体; font-size: 28pt; ">&nbsp;&nbsp;1. 首先我们得知道一个问题</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">,</span><span style="font-family: 宋体; font-size: 28pt; ">那就是线段树得作用并</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 28pt; ">不只</span><span style="font-family: 宋体; font-size: 28pt; ">是用来存储线段的</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">,</span><span style="font-family: 宋体; font-size: 28pt; ">也可以存储点的值等等<span style="font-family: 'Times New Roman'; ">. &nbsp;</span></span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><font face="'Times New Roman'"><br></font><span style="color: rgb(255, 51, 0); position: absolute; left: -3.4375%; top: 0.39em; font-family: Wingdings; font-size: 10px; ">|</span><span style="font-family: 宋体; font-size: 28pt; ">&nbsp;&nbsp;2. 对于</span><span style="font-family: 宋体; font-size: 28pt; ">静态的</span><span style="font-family: 宋体; font-size: 28pt; ">线段树</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">,</span><span style="font-family: 宋体; font-size: 28pt; ">空间上需要的数组有</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">:</span><span style="font-family: 宋体; font-size: 28pt; ">当前结点</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 28pt; ">的数</span><span style="font-family: 宋体; font-size: 28pt; ">据值</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">,</span><span style="font-family: 宋体; font-size: 28pt; ">左儿子</span><span style="font-family: 宋体; font-size: 28pt; ">编号</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">,</span><span style="font-family: 宋体; font-size: 28pt; ">右儿子编号</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">.</span><span style="font-family: 宋体; font-size: 28pt; ">至少这么三个数组</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">&nbsp;&nbsp;&nbsp;&nbsp;3.&nbsp;</span><span style="color: rgb(255, 51, 0); position: absolute; left: -3.80469%; top: 0.39em; font-family: Wingdings; font-size: 10px; ">|</span><span style="font-family: 宋体; font-size: 28pt; ">&nbsp;而在</span><span style="font-family: 宋体; font-size: 28pt; ">时间上虽然是</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">NlogN</span><span style="font-family: 宋体; font-size: 28pt; ">的复杂度</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">,</span><span style="font-family: 宋体; font-size: 28pt; ">但是系数很大</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">.</span><span style="color: rgb(255, 51, 0); position: absolute; left: -3.4375%; top: 0.39em; font-family: Wingdings; font-size: 10px; ">|</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 28pt; ">&nbsp;&nbsp;4. 实现起</span><span style="font-family: 宋体; font-size: 28pt; ">来的时候编程复</span><span style="font-family: 宋体; font-size: 28pt; ">杂度大</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">,</span><span style="font-family: 宋体; font-size: 28pt; ">空间复杂度大</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">,</span><span style="font-family: 宋体; font-size: 28pt; ">时间</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 28pt; ">效率</span><span style="font-family: 宋体; font-size: 28pt; ">也不是</span><span style="font-family: 宋体; font-size: 28pt; ">很理想</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">.&nbsp;</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;<span style="font-size: 24pt; color: red; ">所以这个时候树状数组成了一个很好的选择.</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p:colorscheme colors="#ffffff,#000000,#969696,#000000,#00cc99,#3333cc,#ccccff,#b2b2b2"><div v:shape="_x0000_s1026" class="O"><div><span style="font-family: 华文彩云; font-size: 32pt; ">先看一个例题:</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div><span style="font-family: 黑体; font-size: 32pt; ">数列操作</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; ">:</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></div></p:colorscheme><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; "><span>&nbsp;&nbsp; &nbsp;</span></span><span style="font-family: 宋体; font-size: 32pt; ">给定一个初始值都为</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; ">0</span><span style="font-family: 宋体; font-size: 32pt; ">的序列</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; ">,</span><span style="font-family: 宋体; font-size: 32pt; ">动态地修改一些</span>&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 32pt; ">位置上的数字</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; ">,</span><span style="font-family: 宋体; font-size: 32pt; ">加上一个数</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; ">,</span><span style="font-family: 宋体; font-size: 32pt; ">减去一个数</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; ">,</span><span style="font-family: 宋体; font-size: 32pt; ">或者乘上</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 32pt; ">一个数</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; ">,</span><span style="font-family: 宋体; font-size: 32pt; ">然后动态地提出问题</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; ">,</span><span style="font-family: 宋体; font-size: 32pt; ">问题的形式是求出</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 32pt; ">一段数字的和</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; ">.<span style="font-size: 14px; font-family: verdana, 'courier new'; ">&nbsp;</span></span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p:colorscheme colors="#ffffff,#000000,#969696,#000000,#00cc99,#3333cc,#ccccff,#b2b2b2"></p:colorscheme><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 32pt; color: rgb(255, 153, 0); ">如果直接用朴素方法做的话</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; color: rgb(255, 153, 0); ">,</span><span style="font-family: 宋体; font-size: 32pt; color: rgb(255, 153, 0); ">修改的复杂度是</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; color: rgb(255, 153, 0); ">O(1),</span>&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 32pt; color: rgb(255, 153, 0); ">询问的复杂度是</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; color: rgb(255, 153, 0); ">O(N),M</span><span style="font-family: 宋体; font-size: 32pt; color: rgb(255, 153, 0); ">次询问的复杂度是</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; color: rgb(255, 153, 0); ">M*N.</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; color: rgb(255, 153, 0); ">M,N</span><span style="font-family: 宋体; font-size: 32pt; color: rgb(255, 153, 0); ">的范围可以有</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 32pt; color: rgb(255, 153, 0); ">100000</span><span style="font-family: 宋体; font-size: 32pt; color: rgb(255, 153, 0); ">以上!!!!</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-size: 24pt; color: rgb(255, 0, 0); ">&nbsp;有没有更好的方法???</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-size: 24pt; ">&nbsp;</span><span style="font-size: 24pt; color: rgb(255, 0, 255); ">呵呵, 肯定有啦, 就是我要说的树状数组!!! &nbsp;</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-size: 24pt; ">具体的树状数组的解释请看上面, &nbsp;那么这个2<sup>k</sup>怎么求? 是怎么</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-size: 24pt; ">来的? &nbsp;</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p:colorscheme colors="#ffffff,#000000,#969696,#000000,#00cc99,#3333cc,#ccccff,#b2b2b2"><div v:shape="_x0000_s1026" class="O"><div><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">K</span><span style="font-family: 宋体; font-size: 28pt; ">的计算可以这样</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">:</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div><span style="font-family: 'Times New Roman'; font-size: 22px; "><span style="color: rgb(255, 51, 0); position: absolute; left: -5.54688%; top: 0.39em; font-family: Wingdings; font-size: 15px; ">|</span></span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">2<sup>k</sup>=x and (x &amp; (x-1))</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div><span style="font-family: 'Times New Roman'; font-size: 22px; "><span style="color: rgb(255, 51, 0); position: absolute; left: -5.54688%; top: 0.39em; font-family: Wingdings; font-size: 15px; ">|</span></span><span style="font-family: 宋体; font-size: 28pt; ">以</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">6</span><span style="font-family: 宋体; font-size: 28pt; ">为例</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div><span style="font-family: 'Times New Roman'; font-size: 22px; "><span style="color: rgb(255, 51, 0); position: absolute; left: -5.54688%; top: 0.39em; font-family: Wingdings; font-size: 15px; ">|</span></span><span style="font-family: 'Times New Roman'; font-size: 28pt; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>(6)</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 18pt; position: relative; top: 0.37em; ">10</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">=(0110)</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 18pt; position: relative; top: 0.37em; ">2</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div><span style="font-family: 'Times New Roman'; font-size: 22px; "><span style="color: rgb(255, 51, 0); position: absolute; left: -5.54688%; top: 0.39em; font-family: Wingdings; font-size: 15px; ">|</span></span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">xor<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>6-1=(5)</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 18pt; position: relative; top: 0.37em; ">10</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">=(0101)</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 18pt; position: relative; top: 0.37em; ">2</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div><span style="font-family: 'Times New Roman'; font-size: 22px; "><span style="color: rgb(255, 51, 0); position: absolute; left: -5.54688%; top: 0.39em; font-family: Wingdings; font-size: 15px; ">|</span></span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>(0011)</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 18pt; position: relative; top: 0.37em; ">2</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div><span style="font-family: 'Times New Roman'; font-size: 22px; "><span style="color: rgb(255, 51, 0); position: absolute; left: -5.54688%; top: 0.39em; font-family: Wingdings; font-size: 15px; ">|</span></span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">and<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>(6)</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 18pt; position: relative; top: 0.37em; ">10</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; ">=(0110)</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 18pt; position: relative; top: 0.37em; ">2</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div><span style="font-family: 'Times New Roman'; font-size: 22px; "><span style="color: rgb(255, 51, 0); position: absolute; left: -5.54688%; top: 0.39em; font-family: Wingdings; font-size: 15px; ">|</span></span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 28pt; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>(0010)</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 18pt; position: relative; top: 0.37em; ">2</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-size: 24pt; ">所以:&nbsp;</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-size: 24pt; ">由数字的机器码可以更简单的优化成: &nbsp;</span><span style="font-family: 'Times New Roman'; font-size: 37px; ">x &amp; (-x)</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">对于上面那一题</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 24pt; color: rgb(255, 0, 255); ">,</span><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">每次修改与询问都是对</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 24pt; color: rgb(255, 0, 255); ">C</span><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">数组</span><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">做处理</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 24pt; color: rgb(255, 0, 255); ">.</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">空间复杂度有</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 24pt; color: rgb(255, 0, 255); ">3N</span><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">降为</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 24pt; color: rgb(255, 0, 255); ">N,</span><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">时间效率也有所</span><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">提高</span><span lang="EN-US" style="font-family: 'Times New Roman'; font-size: 24pt; color: rgb(255, 0, 255); ">.</span><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">编程</span><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">复杂</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 宋体; font-size: 24pt; color: rgb(255, 0, 255); ">度更是降了不少.</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p:colorscheme colors="#ffffff,#000000,#969696,#000000,#00cc99,#3333cc,#ccccff,#b2b2b2"></p:colorscheme><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></div></p:colorscheme></div></p:colorscheme><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124726.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 20:19 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124726.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>STL MAP 详解 (zz)</title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124656.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 03:53:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124656.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124656.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124656.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124656.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124656.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;由于STL是一个统一的整体，map的很多用法都和STL中其它的东 西结合在一起；map中由于它内部有序，由红黑树保证，因此很多函数执行的时间复杂度都是log2N的，如果用map函数可以实现的功能，而STL Algorithm也可以完成该功能，...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/08/25/124656.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/124656.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 11:53 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124656.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>scanf/sscanf 格式的特殊控制</title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124653.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 03:38:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124653.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124653.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124653.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124653.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124653.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_8f07b54f-693e-4f39-aa42-e4d328fd8ba7" style="vertical-align: middle; padding-right: 5px; "><span class="cnblogs_code_collapse" style="border-right-color: rgb(128, 128, 128); border-right-width: 1px; border-right-style: solid; border-top-color: rgb(128, 128, 128); border-top-width: 1px; border-top-style: solid; border-left-color: rgb(128, 128, 128); border-left-width: 1px; border-left-style: solid; border-bottom-color: rgb(128, 128, 128); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(255, 255, 255); padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px; font-family: 'Courier New'; ">代码</span><div id="cnblogs_code_open_8f07b54f-693e-4f39-aa42-e4d328fd8ba7"><div><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">scanf中一种很少见但很有用的转换字符：[...]和[&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">...]。<br>#include</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">stdio.h</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;main()&nbsp;<br>{&nbsp;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;strings[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">100</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];&nbsp;<br>scanf(</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%[1234567890]</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,strings);&nbsp;<br>printf(</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%s</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,strings);<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;<br>}&nbsp;<br>运行，输入：1234werew后，结果是：</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1234</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">。<br>通过运行可以发现它的作用是：如果输入的字符属于方括号内字符串中某个字符，那么就提取该字符；如果一经发现不属于就结束提取。该方法会自动加上一个字符串结束符到已经提取的字符后面。&nbsp;<br>scanf(</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%[^1234567890]</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,strings);&nbsp;它的作用是：如果一经发现输入的字符属于方括号内字符串中某个字符，那么就结束提取；如果不属于就提取该字符。该方法会自动加上一个字符串结束符到已经提取的字符后面。&nbsp;<br>注意：方括号两边不能空格，如：scanf(</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%[&nbsp;1234567890&nbsp;]</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,strings);&nbsp;scanf(</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%[&nbsp;^1234567890&nbsp;]</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,strings);&nbsp;不让空格也会算在里面的。<br>用这种方法还可以解决scanf的输入中不能有空格的问题。只要用<br>scanf(</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%[^\n]</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,strings);&nbsp;就可以了。很神奇吧。<br><br>ANSI&nbsp;C&nbsp;标准向&nbsp;scanf()&nbsp;增加了一种新特性，称为扫描集(scanset)。&nbsp;扫描集定义一个字符集合，可由&nbsp;scanf()&nbsp;读入其中允许的字符并赋给对应字符数组。&nbsp;扫描集合由一对方括号中的一串字符定义，左方括号前必须缀以百分号。&nbsp;例如，以下的扫描集使&nbsp;scanf()&nbsp;读入字符&nbsp;A、B&nbsp;和&nbsp;C：<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[ABC]<br><br>&nbsp;&nbsp;&nbsp;&nbsp;使用扫描集时，scanf()&nbsp;连续吃进集合中的字符并放入对应的字符数组，直到发现不在集合中的字符为止(即扫描集仅读匹配的字符)。返回时，数组中放置以&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">null</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;结尾、由读入字符组成的字符串。<br><br>&nbsp;&nbsp;&nbsp;&nbsp;用字符&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;可以说明补集。把&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;字符放为扫描集的第一字符时，构成其它字符组成的命令的补集合，指示&nbsp;scanf()&nbsp;只接受未说明的其它字符。<br>&nbsp;&nbsp;&nbsp;&nbsp;对于许多实现来说，用连字符可以说明一个范围。&nbsp;例如，以下扫描集使&nbsp;scanf()&nbsp;接受字母&nbsp;A&nbsp;到&nbsp;Z：<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[A</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">Z]<br>&nbsp;&nbsp;&nbsp;&nbsp;重要的是要注意扫描集是区分大小写的。因此，希望扫描大、小写字符时，应该分别说明大、小写字母。<br><br>搜集一些特殊用法：<br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[&nbsp;]&nbsp;的用法：</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[&nbsp;]表示要读入一个字符集合,&nbsp;如果[&nbsp;后面第一个字符是&#8221;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&#8221;，则表示反意思。<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;]内的字符串可以是1或更多字符组成。空字符集（</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[]）是违反规定的，可<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;导致不可预知的结果。</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]也是违反规定的。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">z]&nbsp;读取在&nbsp;a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">z&nbsp;之间的字符串，如果不在此之前则停止，如<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;s[]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">hello,&nbsp;my&nbsp;friend&#8221;&nbsp;;&nbsp;//&nbsp;注意:&nbsp;,逗号在不&nbsp;a-z之间</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sscanf(&nbsp;s,&nbsp;&#8220;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">z]&#8221;,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">string</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;)&nbsp;;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;string=hello</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">z]&nbsp;读取不在&nbsp;a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">z&nbsp;之间的字符串，如果碰到a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">z之间的字符则停止，如<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;s[]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">HELLOkitty&#8221;&nbsp;;&nbsp;//&nbsp;注意:&nbsp;,逗号在不&nbsp;a-z之间</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sscanf(&nbsp;s,&nbsp;&#8220;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">z]&#8221;,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">string</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;)&nbsp;;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;string=HELLO</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;前面带&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;号表示不保存变量。跳过符合条件的字符串。<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;s[]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">notepad=1.0.0.1001</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;szfilename&nbsp;[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">32</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">""</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sscanf(&nbsp;s,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%*[^=]</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;szfilename&nbsp;)&nbsp;;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;szfilename=NULL,因为没保存</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sscanf(&nbsp;s,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%*[^=]=%s</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;szfilename&nbsp;)&nbsp;;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;szfilename=1.0.0.1001</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">40c&nbsp;读取40个字符<br><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;读取字符串直到碰到&#8217;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&#8217;号，&#8217;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&#8217;后面可以带更多字符,如：<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;s[]</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">notepad=1.0.0.1001</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;szfilename&nbsp;[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">32</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">""</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sscanf(&nbsp;s,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%[^=]</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;szfilename&nbsp;)&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;szfilename=notepad&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果参数格式是：</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">[</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">:]&nbsp;，那么也可以从&nbsp;notepad:</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1.0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">.</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">.1001读取notepad</span></div></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124653.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 11:38 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124653.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>牛顿迭代法 求解方程</title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124652.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 03:37:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124652.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124652.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124652.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124652.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124652.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: ax^3+bX^2+cx+d=0根的关系:x1 + x2 + x3 = - b / a;x1 * x2 + x1 * x3 + x2 * x3 = c / a;x1 * x2 * x3 = - d / a;牛顿迭代解方程(x0附近的根)01double&nbsp;Newton_Iterative(double&nbsp;a,double&nbsp;b,double&nbsp;c,double&nb...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/08/25/124652.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/124652.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 11:37 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124652.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ZOJ POJ 题目分类</title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124650.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 03:36:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124650.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124650.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124650.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124650.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124650.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font color="#800080"><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><h3>ZOJ POJ 题目分类</h3><p clearfix="" nbw-act="" fc06"="" style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><div fc05="" fc11="" nbw-blog="" ztag="" js-fs2"=""><br>ZOJ题目分类<br>初学者题：<br><br>1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 1334 1337 1338 1350 1365 1382 1383 1394 1402 1405 1414 1494 1514 1622 1715 1730 1755 1760 1763 1796 1813 1879 1889 1904 1915 1949 2001 2022 2099 2104 2108 2172 2176 2201 2208 2321 2345 2351 2376 2388 2405 2417 2433&nbsp;<br><br>模拟问题：<br><br>1006 1009 1012 1016 1019 1023 1026 1028 1038 1042 1045 1051 1056 1057 1058 1061 1065 1066 1068 1072 1073 1078 1087 1088 1097 1098 1099 1103 1111 1121 1124 1126 1128 1133 1138 1146 1152 1154 1160 1175 1178 1187 1194 1207 1222 1224 1244 1259 1267 1274 1275 1277 1278 1279 1281 1282 1294 1295 1300 1308 1317 1324 1339 1351 1362 1392 1393 1397 1398 1399 1400 1402 1432 1434 1444 1452 1475 1487 1493 1497 1517 1526 1527 1530 1531 1552 1569 1573 1592 1601 1610 1623 1631 1641 1652 1657 1659 1682 1692 1700 1702 1707 1708 1712 1728 1732 1737 1746 1747 1750 1752 1754 1758 1764 1768 1774 1797 1799 1804 1807 1811 1822 1824 1831 1834 1837 1838 1842 1844 1845 1854 1858 1862 1870 1881 1884 1889 1896 1906 1921 1951 1969 1978 2000 2022 2040 2046 2047 2051 2072 2084 2101 2112 2131 2133 2138 2148 2153 2156 2160 2164 2172 2178 2184 2185 2187 2189 2193 2196 2201 2204 2208 2211 2212 2220 2229 2233 2239 2240 2261 2262 2269 2277 2288 2301 2309 2311 2312 2316 2320 2321 2322 2328 2330 2350 2389 2405 2410 2414 2420 2421 2483 2508 2560 2569 2572 2593 2613 2617 2680 2681 2731 2732 2743&nbsp;<br><br>动态规划：<br><br>1013 1022 1025 1027 1074 1076 1093 1094 1100 1107 1108 1136 1149 1183 1196 1200 1206 1227 1234 1245 1249 1250 1276 1303 1346 1353 1366 1368 1387 1424 1425 1428 1446 1448 1449 1454 1459 1462 1463 1470 1474 1475 1483 1484 1490 1499 1503 1512 1515 1520 1524 1539 1540 1554 1563 1567 1579 1602 1607 1611 1629 1638 1642 1651 1666 1695 1713 1717 1731 1733 1736 1738 1743 1756 1757 1787 1792 1800 1819 1853 1864 1877 1880 1893 1913 1918 1925 1953 1985 1986 1988 1991 1995 2002 2014 2025 2042 2058 2059 2067 2068 2069 2081 2096 2127 2136 2142 2144 2156 2180 2189 2202 2206 2213 2224 2227 2242 2244 2254 2255 2264 2271 2278 2280 2281 2283 2284 2297 2319 2337 2338 2341 2349 2353 2354 2366 2372 2374 2397 2401 2402 2414 2422 2424 2432 2498 2501 2521 2522 2527 2536 2547 2561 2563 2565 2568 2581 2591 2598 2604 2621 2624 2625 2626 2641 2642 2667 2673 2683 2685 2692 2702 2710 2711 2734 2739 2744 2745&nbsp;<br><br>字符串处理问题：<br><br>1002 1004 1005 1008 1016 1019 1046 1048 1049 1050 1051 1052 1053 1054 1055 1056 1061 1063 1086 1089 1091 1094 1099 1101 1103 1111 1115 1117 1118 1120 1123 1125 1126 1129 1130 1136 1139 1143 1150 1151 1152 1154 1159 1160 1168 1170 1177 1178 1179 1180 1181 1184 1188 1189 1190 1191 1192 1195 1197 1243 1295 1315 1325 1392 1582 1698 1707 1720 1729 1808 1831 1854 1858 1905 1963 1969 1970 1984&nbsp;<br><br>搜索问题：&nbsp;<br><br>1002 1003 1008 1031 1038 1039 1041 1060 1063 1069 1080 1083 1088 1089 1103 1144 1155 1190 1204 1217 1229 1249 1297 1301 1344 1355 1361 1412 1415 1435 1443 1457 1479 1505 1518 1530 1593 1649 1671 1675 1686 1709 1711 1719 1742 1832 1909 1935 1940 1977 1984 2031 2033 2043 2053 2093 2103 2110 2128 2165 2233 2241 2252 2276 2288 2355 2372 2374 2412 2416 2418 2437 2440 2442 2466 2471 2475 2477 2509 2515 2531 2534 2580 2588 2594 2631 2633 2688&nbsp;<br><br>数论问题：<br><br>1007 1028 1088 1113 1133 1160 1222 1278 1284 1312 1314 1385 1489 1526 1530 1569 1577 1596 1601 1652 1657 1712 1797 1842 1889 1906 1951 2000 2022 2028 2060 2095 2105 2156 2189 2212 2233 2277 2288 2305 2316 2320 2330 2360 2371 2400 2410 2414&nbsp;<br><br>几何问题：<br><br>1010 1032 1037 1041 1081 1090 1104 1123 1139 1165 1199 1426 1439 1460 1472 1597 1608 1648 1683 1910 2015 2102 2107 2157 2228 2234 2318 2335 2347 2352 2361 2370 2375 2394 2403&nbsp;<br><br>树型结构问题：<br><br>1011 1038 1043 1062 1141 1159 1167 1203 1319 1335 1387 1406 1481 1511 1542 1586 1610 1635 1674 1700 1752 1788 1805 1809 1900 1944 1955 1959 1965 1990 2243 2425&nbsp;<br><br>图表问题：<br><br>1015 1030 1082 1084 1085 1105 1119 1127 1130 1140 1203 1311 1377 1420 1453 1465 1492 1589 1798 1802 1919 1935 2016 2236 2238 2281 2326&nbsp;<br><br>匹配问 题：&nbsp;<br><br>1002 1059 1077 1137 1140 1157 1197 1231 1364 1516 1525 1576 1626 1654 1882 2067 2192 2221 2223 2333 2362 2404<br><br><br>pku题目分类&nbsp;<br>麻 烦题：<br>1697, 1712, 1713, 1720, 1729, 1765, 1772, 1858, 1872, 1960, 1963, 2050, 2122, 2162, 2219, 2237,<br><br>简单题目：<br>1000, 1003, 1004, 1005, 1007, 1046, 1207, 1226, 1401, 1504, 1552, 1607, 1657, 1658, 1674, 1799, 1862, 1906, 1922, 1929, 1931, 1969, 1976, 2000, 2005, 2017, 2027, 2070, 2101, 2105, 2109, 2116, 2136, 2160, 2190, 2232, 2234, 2275, 2301, 2350, 2363, 2389, 2393, 2413, 2419,&nbsp;<br>推荐：<br>1063, 1064, 1131, 1140, 1715, 2163,&nbsp;<br><br>杂题：<br>1014, 1218, 1316, 1455, 1517, 1547, 1580, 1604, 1663, 1678, 1749, 1804, 2013, 2014, 2056, 2059, 2100, 2188, 2189, 2218, 2229, 2249, 2290, 2302, 2304, 2309, 2313, 2316, 2323, 2326, 2368, 2369, 2371, 2402, 2405, 2407,&nbsp;<br>推荐：<br>1146, 1147, 1148, 1171, 1389, 1433, 1468, 1519, 1631, 1646, 1672, 1681, 1700, 1701, 1705, 1728, 1735, 1736, 1752, 1754, 1755, 1769, 1781, 1787, 1796, 1797, 1833, 1844, 1882, 1933, 1941, 1978, 2128, 2166, 2328, 2383, 2420,&nbsp;<br><br>高精度：<br>1001, 1220, 1405, 1503,&nbsp;<br><br>排序：<br>1002, 1318, 1877, 1928, 1971, 1974, 1990, 2001, 2002, 2092, 2379, 2388, 2418,&nbsp;<br>推荐：<br>1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 2377, 2380,&nbsp;<br><br>搜索<br>容易：<br>1128, 1166, 1176, 1231, 1256, 1270, 1321, 1543, 1606, 1664, 1731, 1742, 1745, 1847, 1915, 1950, 2038, 2157, 2182, 2183, 2381, 2386, 2426,&nbsp;<br>不易：<br>1024, 1054, 1117, 1167, 1708, 1746, 1775, 1878, 1903, 1966, 2046, 2197, 2349,&nbsp;<br>推荐：<br>1011, 1190, 1191, 1416, 1579, 1632, 1639, 1659, 1680, 1683, 1691, 1709, 1714, 1753, 1771, 1826, 1855, 1856, 1890, 1924, 1935, 1948, 1979, 1980, 2170, 2288, 2331, 2339, 2340,&nbsp;<br><br>数据结构<br>容易：<br>1182, 1656, 2021, 2023, 2051, 2153, 2227, 2236, 2247, 2352, 2395,&nbsp;<br>不易：<br>1145, 1177, 1195, 1227, 1661, 1834,&nbsp;<br>推荐：<br>1330, 1338, 1451, 1470, 1634, 1689, 1693, 1703, 1724, 1988, 2004, 2010, 2119, 2274,&nbsp;<br><br>动态规划<br>容 易：<br>1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 1952, 1953, 1958, 1959, 1962, 1975, 1989, 2018, 2029, 2033, 2063, 2081, 2082, 2181, 2184, 2192, 2231, 2279, 2329, 2336, 2346, 2353, 2355, 2356, 2385, 2392, 2424,&nbsp;<br>不易：<br>1019, 1037, 1080, 1112, 1141, 1170, 1192, 1239, 1655, 1695, 1707, 1733, 1737, 1837, 1850, 1920, 1934, 1937, 1964, 2039, 2138, 2151, 2161, 2178,&nbsp;<br>推荐：<br>1015, 1635, 1636, 1671, 1682, 1692, 1704, 1717, 1722, 1726, 1732, 1770, 1821, 1853, 1949, 2019, 2127, 2176, 2228, 2287, 2342, 2374, 2378, 2384, 2411,&nbsp;<br><br>字 符串：<br>1488, 1598, 1686, 1706, 1747, 1748, 1750, 1760, 1782, 1790, 1866, 1888, 1896, 1951, 2003, 2121, 2141, 2145, 2159, 2337, 2359, 2372, 2406, 2408,&nbsp;<br><br>贪心：<br>1042, 1065, 1230, 1323, 1477, 1716, 1784,&nbsp;<br><br>图 论<br>容易：<br>1161, 1164, 1258, 1175, 1308, 1364, 1776, 1789, 1861, 1939, 1940, 1943, 2075, 2139, 2387, 2394, 2421,&nbsp;<br>不易：<br>1041, 1062, 1158, 1172, 1201, 1275, 1718, 1734, 1751, 1904, 1932, 2173, 2175, 2296,&nbsp;<br>网络 流：<br>1087, 1273, 1698, 1815, 2195,&nbsp;<br>匹配：<br>1274, 1422, 1469, 1719, 2060, 2239,&nbsp;<br>Euler：<br>1237, 1637, 1394, 2230,&nbsp;<br>推荐：<br>2049, 2186,&nbsp;<br><br>计算几何<br>容易：<br>1319, 1654, 1673, 1675, 1836, 2074, 2137, 2318,&nbsp;<br>不 易：<br>1685, 1687, 1696, 1873, 1901, 2172, 2333,&nbsp;<br>凸包：<br>1113, 1228, 1794, 2007, 2187,&nbsp;<br><br>模拟<br>容易：<br>1006, 1008, 1013, 1016, 1017, 1169, 1298, 1326, 1350, 1363, 1676, 1786, 1791, 1835, 1970, 2317, 2325, 2390,&nbsp;<br>不易：<br>1012, 1082, 1099, 1114, 1642, 1677, 1684, 1886,&nbsp;<br><br>数 学<br>容易：<br>1061, 1091, 1142, 1289, 1305, 1306, 1320, 1565, 1665, 1666, 1730, 1894, 1914, 2006, 2042, 2142, 2158, 2174, 2262, 2305, 2321, 2348,&nbsp;<br>不 易：<br>1067, 1183, 1430, 1759, 1868, 1942, 2167, 2171, 2327,&nbsp;<br>推荐：<br>1423, 1450, 1640, 1702, 1710, 1721, 1761, 1830, 1930, 2140,</div></span></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124650.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 11:36 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124650.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 题目分类</title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124651.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 03:36:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124651.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124651.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124651.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124651.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124651.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); "><span style="font-family: verdana, sans-serif; color: rgb(0, 0, 0); font-size: 14px; "><h3 class="title pre fs1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span class="tcnt" style="font-family: 'Courier New'; ">HDOJ 题目分类</span></h3><p class="tdep clearfix nbw-act fc06" style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><div class="bct fc05 fc11 nbw-blog ztag js-fs2"><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-family: 'Courier New'; line-height: 21px; ">/*<br>&nbsp;* 一:简单题<br>&nbsp;*/</span>&nbsp;<br>1000：&nbsp;&nbsp; &nbsp;入门用；<br>1001：&nbsp;&nbsp; &nbsp;用高斯求和公式要防溢出<br>1004：1012：<br>1013：&nbsp;&nbsp; &nbsp;对9取余好了<br>1017：1021：<br>1027：&nbsp;&nbsp; &nbsp;用STL中的next_permutation（）<br>1029：1032：1037：1039：1040：1056：1064：1065：<br>1076：&nbsp;&nbsp; &nbsp;闰年</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">1084：<br>1085：<br>1089，1090，1091，1092，1093，1094， 1095， 1096：全是A+B<br>1108：1157：1196：<br>1197：&nbsp;&nbsp; &nbsp;进制<br>1202：1215：1219：1228：1229：1234：1235：1236：1256：1259：1262：1279：1280：1283：</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">1239：</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">1321：1395：1406：<br>1407：&nbsp;&nbsp; &nbsp;因为xyz是对称的，所以yz没必要从1开始找起<br>1408：1412：1420：<br>1425：&nbsp;&nbsp; &nbsp;n^2的复杂度是被卡掉了的<br>1562：1587：1594：1701：1713：1718：1785：1860：1862：1877：1976：1984：1985：<br>HDU 11页 入门必做<br>2101：2304：2309：2502：2503：2504：2519：2520：2547：2548：2549：2550：2561：2562：2564：2565：</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">2566：2567：2568：2950：2959：3040：<br><br><br><span style="font-family: 'Courier New'; line-height: 21px; ">/*<br>&nbsp;* 二:图论<br>&nbsp;*/</span>&nbsp;<br>//1.搜索：<br>*1010：搜索&nbsp;&nbsp; &nbsp;<br>1015： 搜索&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;5个for暴力也可以?????<br>*1016：搜索&nbsp;&nbsp; &nbsp;<br>*1072：搜索：&nbsp;&nbsp; &nbsp;bfs过<br>1240：搜 索&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;最基本的bfs,三维而已，注意输入坐标是x,y,z,但map[z][y][x]&nbsp;<br>*1241：搜索<br>*1242：搜 索<br>1253：搜索&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bfs,bfs走出来的路是最短的<br>*1312：搜索<br>*1342：搜索&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;dfs可以，6个for也可以<br>1372：搜索&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;太经典了,8 个方向bfs&nbsp;<br>*1496：搜索&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;很好的一个题<br>*1548：搜索||图论&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;一维上的bfs,开始走过的没标记，导致mle，以为stl没优化好，自己写了个动态分配的链队还是mle，后来标记重新submit证明用stl的内 存比自己写的链队要小...；据说最短路也可以做~<br>*1728：搜索</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">*2181: 搜索<br>*2660：搜索<br>*2952：搜索&nbsp;<br>2717：搜索&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bfs,注意走过的要标记，否则mle<br>* 2102：搜索<br>//2.最小生成树<br>？1102：&nbsp;&nbsp; &nbsp;最小生成树<br>？1162： 最小生成树<br>*1233：&nbsp;&nbsp; &nbsp;最小生成树<br>*1301：&nbsp;&nbsp; &nbsp;最小生成树<br>*1875：&nbsp;&nbsp; &nbsp;最小生成树<br>1863：&nbsp;&nbsp; &nbsp;最小生成树<br>//3.最短路<br>*1690：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;最短路<br>*1874：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;最短路<br>*2544：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;最短路<br>//4.并查<br>*1213：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;并查<br>*1232：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;并查<br>*1272：并查<br>*1856：&nbsp;&nbsp; &nbsp;并查&nbsp;<br>//5.其他</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">1053：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Huffman树</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">1285：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; top排序<br>*1878：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;欧拉回路<br>*3118：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;二分图<br><br><br><span style="font-family: 'Courier New'; line-height: 21px; ">/*<br>&nbsp;* 三.字符串：<br>&nbsp;*/</span>&nbsp;<br>1020 1048 1062 1088 1106 1113 1161 1200 1591 1870 1982 2140 2203&nbsp;&nbsp; s1连两个<br>2206&nbsp;&nbsp; 这题数据很叼专<br>2265 2487 2673 3125<br>*1686&nbsp;&nbsp; &nbsp;kmp<br>*1075&nbsp;&nbsp; &nbsp;字典树&nbsp;<br>*1181&nbsp;&nbsp; &nbsp;字典树<br>*1800&nbsp;&nbsp; 字典树&nbsp;&nbsp; &nbsp;据说hash也可以过<br>1804<br>*1251&nbsp;&nbsp; 字典树<br><br><br><span style="font-family: 'Courier New'; line-height: 21px; ">/*<br>&nbsp;*四:模拟题：<br>&nbsp;*/</span>&nbsp;<br>1008：<br>*1022：&nbsp;&nbsp; &nbsp;模拟栈<br>1049：1170：<br>1237：&nbsp;&nbsp; &nbsp;队，加减入队，乘除算出来<br>*1873：2816：2832：2919：2957：1702：<br><br><br><span style="font-family: 'Courier New'; line-height: 21px; ">/*<br>&nbsp;* 五.数论：<br>&nbsp;*/</span>&nbsp;<br>1014：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;互质就行，和1222相似<br>1019：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;多个数的lcm<br>？1098：&nbsp;&nbsp; &nbsp;<br>1222：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;同 1014，互质就行<br>3123：<br>？2854：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;打表过的<br><br><br><span style="font-family: 'Courier New'; line-height: 21px; ">/*<br>&nbsp;* 六.计算几何：<br>&nbsp;*/</span>&nbsp;<br>？1086：线 段交点&nbsp;<br>？1115：&nbsp;&nbsp; &nbsp;多边形重心<br>1221：&nbsp;&nbsp; &nbsp;圆和矩形相交<br>*1392：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;凸包<br>？2105： 多边形的重心<br>？2108：&nbsp;&nbsp; &nbsp;判断多边形的凹凸性&nbsp;<br>？2202：&nbsp;&nbsp; &nbsp;凸包&nbsp;<br>？2857：&nbsp;&nbsp; &nbsp;对称问题<br>？3124： 投机过的<br><br><br><span style="font-family: 'Courier New'; line-height: 21px; ">/*<br>&nbsp;* 七.数学题:<br>&nbsp;*/</span>&nbsp;<br>1005：&nbsp;&nbsp; &nbsp;看出有周期<br>？1018：&nbsp;&nbsp; &nbsp;striling公式求阶乘的位数<br>？1023：&nbsp;&nbsp; &nbsp;Catalan数求出栈种数<br>？1060：&nbsp;&nbsp; &nbsp;leftmost digit<br>？1061：&nbsp;&nbsp; &nbsp;rightmost digit<br>1071：&nbsp;&nbsp; &nbsp;积分&nbsp;<br>？1130：&nbsp;&nbsp; &nbsp;Catalan数求二叉树的种数<br>？1133：&nbsp;&nbsp; &nbsp;Catalan数应用<br>？1134：&nbsp;&nbsp; &nbsp;Catalan数<br>？1178：&nbsp;&nbsp; &nbsp;求指数底数&nbsp;<br>1205：&nbsp;&nbsp; &nbsp;1239：？1249：？1290：<br>？1465： 错排<br>？1466：1555：1570：<br>1593：&nbsp;&nbsp; &nbsp;找同心圆<br>？1597：1703：1717：1798：？2114：？2200：2289：？2501：？2563：？2569：？2922：<br><br><span style="font-family: 'Courier New'; line-height: 21px; "><br>/*<br>&nbsp;* 八.DP:<br>&nbsp;*/</span>&nbsp;<br>*1003：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;最大子段和<br>*1058：<br>*1159：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lcs(最长公共字串，不连续)<br>*1160： &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;先排序<br>*1238：<br>*1257：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;最小拦截系统<br>*1501：<br>*2084：&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;经典<br>*2668：&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;最长不同字串<br><br><br><span style="font-family: 'Courier New'; line-height: 21px; ">/*<br>&nbsp;* 九.其他：<br>&nbsp;*/</span>&nbsp;<br>1002：大数加法&nbsp;&nbsp; &nbsp;模板~<br>1041：大数<br>1042：大数阶乘<br>1212： 大数<br>1250：大数<br>1715：大数<br>？1865：大数&nbsp;&nbsp; &nbsp;Fibonacci</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; text-indent: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">1895：hash<br>1009：贪心&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;贪比例大的<br>*1050：贪心<br>*1097：二分&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;二分球a^b%m<br>1144：hash<br>1201：杂题&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;日期<br>1563：hash<br>？1564：博弈<br>*2602： 背包<br>*2817：二分求幂<br>2946：hash<br>？2954：博弈<br>*2986：高精度<br>*1028 &nbsp;&nbsp; &nbsp;整数的拆分种数<br><br>//分类不是绝对的<br>//"*" 表示好题，需要多次回味<br>//"?"表示结论是正确的，但还停留在模块阶 段，需要理解，证明。<br>//简单题看到就可以敲的</p></div></span></span></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124651.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 11:36 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124651.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>