﻿<?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/14427.html</link><description>                             ______________白白の屋</description><language>zh-cn</language><lastBuildDate>Sun, 24 Oct 2010 13:01:15 GMT</lastBuildDate><pubDate>Sun, 24 Oct 2010 13:01:15 GMT</pubDate><ttl>60</ttl><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>前向星+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>PKU 1258 POJ 1258Agri-Net ( MST Kruskarl 并查集 ) ACM 1258 IN PKU</title><link>http://www.cppblog.com/MiYu/archive/2010/10/16/130139.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sat, 16 Oct 2010 08:30:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/16/130139.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/130139.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/16/130139.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/130139.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/130139.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><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; "><span class="Apple-tab-span" style="white-space: pre; ">	</span><a href="http://poj.org/problem?id=1258">http://poj.org/problem?id=1258</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; ">题目描述:</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: Simsun; color: rgb(0, 0, 0); line-height: normal; font-size: medium; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><div class="ptt" lang="en-US" style="text-align: center; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Agri-Net</div><div class="plm" style="text-align: center; font-family: Arial, Helvetica, sans-serif; font-size: 12pt; "><table align="center"><tbody><tr><td><strong>Time Limit:</strong>&nbsp;1000MS</td><td width="10px"></td><td><strong>Memory Limit:</strong>&nbsp;10000K</td></tr><tr><td><strong>Total Submissions:</strong>&nbsp;16557</td><td width="10px"></td><td><strong>Accepted:</strong>&nbsp;6745</td></tr></tbody></table></div><p class="pst" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Description</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; ">Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.&nbsp;<br>Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.&nbsp;<br>Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.&nbsp;<br>The distance between any two farms will not exceed 100,000.&nbsp;<br></div><p class="pst" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Input</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; ">The input includes several cases. For each case, the first line contains the number of farms, N (3 &lt;= N &lt;= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.</div><p class="pst" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Output</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; ">For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.</div><p class="pst" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Sample Input</p><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; ">4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
</pre><p class="pst" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Sample Output</p><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; ">28</pre><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; "><br></pre><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; ">题目分析 :</pre><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; "> <span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>MST 水题, 用KRUSKARL 或 PRIM 轻松能过.</pre><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; ">代码如下 :</pre><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; "><font face="'Courier New', Courier, monospace">/*
Mail to   : miyubai@gamil.com
MyBlog    : http://baiyun.me
Link      : http://www.cnblogs.com/MiYu  || http://www.cppblog.com/MiYu
Author By : MiYu
Test      : 1
Complier  : g++ mingw32-3.4.2
Program   : Agri-Net
Doc Name  : PKU_1258
*/
//#pragma warning( disable:4789 )
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
#include &lt;string&gt;
#include &lt;set&gt;
#include &lt;map&gt;
#include &lt;utility&gt;
#include &lt;queue&gt;
#include &lt;stack&gt;
#include &lt;list&gt;
#include &lt;vector&gt;
#include &lt;cstdio&gt;
#include &lt;cstdlib&gt;
#include &lt;cstring&gt;
#include &lt;cmath&gt;
#include &lt;ctime&gt;
using namespace std;
const int MAX = 105;
int st[105];
int N, x, y, w;
struct vv {
       int x, y, val;
       void set ( int a, int b, int w ) { x=a,y=b,val=w; }

}v[10005];       
bool cmp ( const vv &amp;a, const vv &amp;b ) {
          return a.val &lt; b.val;     
}
void creat () {
     for ( int i = 0; i &lt;= N; ++ i )
          st[i] = i;     
}
int find ( int x ) {
    return x != st[x] ? find ( st[x] ) : x;    
}
void merge ( int x, int y ) {
     x = find ( x );
     y = find ( y );
     if ( x == y ) return;
     st[y] = x;     
}
int main ()
{
    while ( scanf ( "%d", &amp;N )==1) {
           creat ();
           memset ( v, 0, sizeof ( v ) );
           int c = 0;
           for ( int i = 1; i &lt;= N; ++ i ) {
                for ( int j = 1; j &lt;= N; ++ j ) {
                     scanf ( "%d", &amp;w );  
                     v[c++].set ( i,j,w );  
                }    
           }     
           sort ( v, v + c, cmp );
           int sum = 0;
           for ( int i = 0 ; i &lt; c; ++ i ) {
                if ( find ( v[i].x ) != find ( v[i].y ) ) {
                    sum += v[i].val;  
                    merge ( v[i].x, v[i].y );
                }    
           }
           printf ( "%d\n", sum );
    }
    return 0;
}</font>
</pre><div><br></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/130139.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-16 16:30 <a href="http://www.cppblog.com/MiYu/archive/2010/10/16/130139.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></channel></rss>