﻿<?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++博客-yx-随笔分类-图论</title><link>http://www.cppblog.com/csu-yx-2013/category/18790.html</link><description>Algorithm Study And So On</description><language>zh-cn</language><lastBuildDate>Sat, 14 Sep 2013 13:30:33 GMT</lastBuildDate><pubDate>Sat, 14 Sep 2013 13:30:33 GMT</pubDate><ttl>60</ttl><item><title>poj 3255 Roadblocks 次短路</title><link>http://www.cppblog.com/csu-yx-2013/archive/2012/09/03/189318.html</link><dc:creator>yx</dc:creator><author>yx</author><pubDate>Mon, 03 Sep 2012 14:39:00 GMT</pubDate><guid>http://www.cppblog.com/csu-yx-2013/archive/2012/09/03/189318.html</guid><wfw:comment>http://www.cppblog.com/csu-yx-2013/comments/189318.html</wfw:comment><comments>http://www.cppblog.com/csu-yx-2013/archive/2012/09/03/189318.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/csu-yx-2013/comments/commentRss/189318.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/csu-yx-2013/services/trackbacks/189318.html</trackback:ping><description><![CDATA[&nbsp; &nbsp;这个题是求次短路。有个不错的解法，是根据一个结论，替换调最短路里面的一条边肯定能得到次短路。<br />&nbsp; &nbsp;那么，只要枚举所有边就可以了。比如，假设开始点为s，目标点是d，设最短路为dis(s,d)。对于边(u,v)，<br />dis(s, u) + w(u, v) + dis(v, d) 大于dis(s, d)，则该路径就可能是次短路。求出最小的大于dis(s,d)的值就可以了。<br />&nbsp; &nbsp;方式是从s开始和从d开始进行2次单源多终点最短路径算法。然后枚举边即可。<br />&nbsp; &nbsp;<br />&nbsp; &nbsp;该算法可以这样理解。因为替换最短路径里面的边，路径的长度只会变大或者不变。如果存在让更短路径变小的边，<br />这本身就与最短路径是矛盾的。所以替换2条或者更多的边只会让路径变得更大。因此，只需考虑替换一条边的情况<br />即可。<br /><br />&nbsp; &nbsp;代码如下：<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include&nbsp;&lt;stdio.h&gt;<br />#include&nbsp;&lt;<span style="color: #0000FF; ">string</span>.h&gt;<br />#include&nbsp;&lt;algorithm&gt;<br />#include&nbsp;&lt;queue&gt;<br />#include&nbsp;&lt;vector&gt;<br /><span style="color: #0000FF; ">using</span>&nbsp;<span style="color: #0000FF; ">namespace</span>&nbsp;std;<br /><br /><span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;MAX_N&nbsp;=&nbsp;5000&nbsp;+&nbsp;10;<br /><span style="color: #0000FF; ">struct</span>&nbsp;Edge<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nE;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nDis;<br />&nbsp;&nbsp;&nbsp;&nbsp;Edge(<span style="color: #0000FF; ">int</span>&nbsp;e,&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;d):nE(e),&nbsp;nDis(d)&nbsp;{}<br />};<br />vector&lt;Edge&gt;&nbsp;graph[MAX_N];<br /><span style="color: #0000FF; ">bool</span>&nbsp;bVisit[MAX_N];<br /><span style="color: #0000FF; ">int</span>&nbsp;nSDis[MAX_N];<br /><span style="color: #0000FF; ">int</span>&nbsp;nEDis[MAX_N];<br /><br /><span style="color: #0000FF; ">struct</span>&nbsp;Node<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nN;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nDis;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">bool</span>&nbsp;<span style="color: #0000FF; ">operator</span>&nbsp;&lt;&nbsp;(<span style="color: #0000FF; ">const</span>&nbsp;Node&amp;&nbsp;node)&nbsp;<span style="color: #0000FF; ">const</span><br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;nDis&nbsp;&gt;&nbsp;node.nDis;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />};<br /><br /><span style="color: #0000FF; ">int</span>&nbsp;ShortestPath(<span style="color: #0000FF; ">int</span>&nbsp;nS,&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nE,&nbsp;<span style="color: #0000FF; ">int</span>*&nbsp;nDis,&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nN)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;priority_queue&lt;Node&gt;&nbsp;pq;<br />&nbsp;&nbsp;&nbsp;&nbsp;memset(bVisit,&nbsp;<span style="color: #0000FF; ">false</span>,&nbsp;<span style="color: #0000FF; ">sizeof</span>(bVisit));<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;(<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;&lt;=&nbsp;nN;&nbsp;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nDis[i]&nbsp;=&nbsp;0x7fffffff;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;nDis[nS]&nbsp;=&nbsp;0;<br />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;head;<br />&nbsp;&nbsp;&nbsp;&nbsp;head.nDis&nbsp;=&nbsp;0,&nbsp;head.nN&nbsp;=&nbsp;nS;<br />&nbsp;&nbsp;&nbsp;&nbsp;pq.push(head);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(pq.empty()&nbsp;==&nbsp;<span style="color: #0000FF; ">false</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;head&nbsp;=&nbsp;pq.top();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pq.pop();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nU&nbsp;=&nbsp;head.nN;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(bVisit[nU])&nbsp;<span style="color: #0000FF; ">continue</span>;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bVisit[nU]&nbsp;=&nbsp;<span style="color: #0000FF; ">true</span>;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;(<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;graph[nU].size();&nbsp;++i)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nV&nbsp;=&nbsp;graph[nU][i].nE;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nLen&nbsp;=&nbsp;head.nDis&nbsp;+&nbsp;graph[nU][i].nDis;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(nLen&nbsp;&lt;&nbsp;nDis[nV])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nDis[nV]&nbsp;=&nbsp;nLen;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;node;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.nDis&nbsp;=&nbsp;nLen;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.nN&nbsp;=&nbsp;nV;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pq.push(node);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;nDis[nE];<br />}<br /><br /><span style="color: #0000FF; ">int</span>&nbsp;Second(<span style="color: #0000FF; ">int</span>&nbsp;nS,&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nE,&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nN)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nShortest&nbsp;=&nbsp;ShortestPath(nS,&nbsp;nE,&nbsp;nSDis,&nbsp;nN);<br />&nbsp;&nbsp;&nbsp;&nbsp;ShortestPath(nE,&nbsp;nS,&nbsp;nEDis,&nbsp;nN);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nAns&nbsp;=&nbsp;0x7fffffff;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;(<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;&lt;=&nbsp;nN;&nbsp;++i)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;(<span style="color: #0000FF; ">int</span>&nbsp;j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;graph[i].size();&nbsp;++j)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nU&nbsp;=&nbsp;i;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nV&nbsp;=&nbsp;graph[i][j].nE;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nLen&nbsp;=&nbsp;nSDis[i]&nbsp;+&nbsp;graph[i][j].nDis&nbsp;+&nbsp;nEDis[nV];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(nLen&nbsp;!=&nbsp;nShortest)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nAns&nbsp;=&nbsp;min(nAns,&nbsp;nLen);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;nAns;<br />}<br /><br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nN,&nbsp;nR;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nA,&nbsp;nB,&nbsp;nD;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(scanf("%d%d",&nbsp;&amp;nN,&nbsp;&amp;nR)&nbsp;==&nbsp;2)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;(<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;&lt;=&nbsp;nN;&nbsp;++i)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;graph[i].clear();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(nR--)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d%d%d",&nbsp;&amp;nA,&nbsp;&amp;nB,&nbsp;&amp;nD);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;graph[nA].push_back(Edge(nB,&nbsp;nD));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;graph[nB].push_back(Edge(nA,&nbsp;nD));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\n",&nbsp;Second(1,&nbsp;nN,&nbsp;nN));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}</div><img src ="http://www.cppblog.com/csu-yx-2013/aggbug/189318.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/csu-yx-2013/" target="_blank">yx</a> 2012-09-03 22:39 <a href="http://www.cppblog.com/csu-yx-2013/archive/2012/09/03/189318.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CSU OJ - 1219: 建食堂 (所有结点间的最短路径)</title><link>http://www.cppblog.com/csu-yx-2013/archive/2011/12/04/161448.html</link><dc:creator>yx</dc:creator><author>yx</author><pubDate>Sun, 04 Dec 2011 14:20:00 GMT</pubDate><guid>http://www.cppblog.com/csu-yx-2013/archive/2011/12/04/161448.html</guid><wfw:comment>http://www.cppblog.com/csu-yx-2013/comments/161448.html</wfw:comment><comments>http://www.cppblog.com/csu-yx-2013/archive/2011/12/04/161448.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/csu-yx-2013/comments/commentRss/161448.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/csu-yx-2013/services/trackbacks/161448.html</trackback:ping><description><![CDATA[链接:<a href="http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1219">http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1219<br /><br />这个题</a>就是求出所有结点的距离之后,再找出某个结点,该结点离其它结点的最大距离是所有结点中是最小的...<br />解法1:深搜出所有结点间的距离,但是会超时,即使深搜的过程使用中记忆化搜索(就是用2维数组保存已经搜出的答案,如果后面的搜索需要用到直接使用即可)...<br />解法2:Floyd算法,3重循环直接找出所有结点之间的最短距离<br />解法3:对每一个结点应用一次迪杰斯特拉算法,找出所有结点与其它结点间的最短距离...<br /><br />解法2:<br /><div><div>#include &lt;stdio.h&gt;</div><div>#include &lt;string.h&gt;</div><div></div><div>#define MAX &nbsp;(100 + 10)</div><div>#define INF (1000000 + 10)</div><div></div><div>int nN, nM;</div><div>int nDis[MAX][MAX];</div><div></div><div>void SearchAll()</div><div>{</div><div>&nbsp; &nbsp; for (int k = 0; k &lt; nN; ++k)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; nN; ++i)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; { &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j &lt; nN; ++j)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nDis[i][k] + nDis[k][j] &lt; nDis[i][j])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nDis[i][j] = nDis[j][i] = nDis[i][k] + nDis[k][j];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>}</div><div></div><div>int main()</div><div>{</div><div>&nbsp; &nbsp; while (scanf("%d%d", &amp;nN, &amp;nM) == 2)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; nN; ++i)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j &lt; nN; ++j)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == j)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nDis[i][j] = 0;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nDis[i][j] = INF;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; while (nM--)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nX, nY, nK;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf("%d%d%d", &amp;nX, &amp;nY, &amp;nK);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nDis[nX][nY] = nDis[nY][nX] = nK;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; SearchAll();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; bool bOk = false;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int nMin = 1 &lt;&lt; 30;</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; nN; ++i)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nTemp = 0;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int j = 0;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( ; j &lt; nN; ++j)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == j) continue;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nDis[i][j] == INF)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nDis[i][j] &gt; nTemp)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nTemp = nDis[i][j];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j == nN)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bOk = true;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nTemp &lt; nMin)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nMin = nTemp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (bOk)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("%d\n", nMin);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; else</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Can not\n");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; return 0;</div><div>}</div></div><div><br />关于Floyd算法,可以这样理解...比如刚开始只取2个结点i,j,它们的距离一定是dis(i,j),但是还有其它结点,需要把其它结点也慢慢加进来,所以最外层关于k的循环意思就是从0至nN-1,把所有其它结点加进来,比如加入0号结点后,距离dis(i,0)+dis(0,j)可能会比dis(i,j)小,如果是这样就更新dis(i,j),然后后面加入1号结点的时候,实际上是在已经加入0号结点的基础上进行的处理了,效果变成dis(i,0,1,j),可能是最小的,而且中间的0,1也可能是不存在的,当然是在dis(i,j)原本就是最小的情况下...<br />这个算法可以用下面这个图片描述...<br /><img src="http://www.cppblog.com/images/cppblog_com/csu-yx/Floyd最短路.JPG" width="352" height="288" alt="" /><br /><br />解法3:<br /><div><div>#include &lt;stdio.h&gt;</div><div>#include &lt;string.h&gt;</div><div></div><div>#define MAX &nbsp;(100 + 10)</div><div>#define INF (1000000 + 10)</div><div></div><div>int nN, nM;</div><div>int nDis[MAX][MAX];</div><div></div><div>void Search(int nSource)</div><div>{</div><div>&nbsp; &nbsp; bool bVisit[MAX];</div><div></div><div>&nbsp; &nbsp; memset(bVisit, false, sizeof(bVisit));</div><div>&nbsp; &nbsp; bVisit[nSource] = true;</div><div></div><div>&nbsp; &nbsp; for (int i = 0; i &lt; nN - 1; ++i)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int nMin = INF;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int nMinPos = 0;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j &lt; nN; ++j)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!bVisit[j] &amp;&amp; nDis[nSource][j] &lt; nMin)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nMin = nDis[nSource][j];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nMinPos = j;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (bVisit[nMinPos] == false)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bVisit[nMinPos] = true;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j &lt; nN; ++j)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nDis[nSource][nMinPos] + nDis[nMinPos][j] &lt; nDis[nSource][j])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nDis[nSource][j] = nDis[nSource][nMinPos] + nDis[nMinPos][j];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>}</div><div></div><div>void SearchAll()</div><div>{</div><div>&nbsp; &nbsp; for (int k = 0; k &lt; nN; ++k)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Search(k);</div><div>&nbsp; &nbsp; }</div><div>}</div><div></div><div>int main()</div><div>{</div><div>&nbsp; &nbsp; while (scanf("%d%d", &amp;nN, &amp;nM) == 2)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; nN; ++i)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j &lt; nN; ++j)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == j)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nDis[i][j] = 0;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nDis[i][j] = INF;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; while (nM--)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nX, nY, nK;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf("%d%d%d", &amp;nX, &amp;nY, &amp;nK);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nDis[nX][nY] = nDis[nY][nX] = nK;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; SearchAll();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; bool bOk = false;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int nMin = 1 &lt;&lt; 30;</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; nN; ++i)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nTemp = 0;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int j = 0;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( ; j &lt; nN; ++j)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == j) continue;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nDis[i][j] == INF)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nDis[i][j] &gt; nTemp)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nTemp = nDis[i][j];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j == nN)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bOk = true;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nTemp &lt; nMin)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nMin = nTemp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (bOk)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("%d\n", nMin);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; else</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Can not\n");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; return 0;</div><div>}</div></div><div></div><div>迪杰斯特拉算法的核心思想是维护一个源点顶点集合,任何最短路径一定是从这个顶点集合发出的...<br />初始化时,这个集合就是源点...<br />我们从该其它结点中选出一个结点,该结点到源点的距离最小...<br />显然,这个距离就是源点到该结点的最短距离了,我们已经找到了答案的一部分了...然后,我们就把该结点加入前面所说的顶点集合...<br />现在顶点集合更新了,我们必须得更新距离了...由于新加入的结点可能发出边使得原来源点到某些结点的距离更小,也就是我们的源点变大了,边也变多了,所以我们的最短距离集合的值也必须变化了...<br />该算法一直循环nN-1次,直至所有的点都加入源点顶点集合...</div></div><img src ="http://www.cppblog.com/csu-yx-2013/aggbug/161448.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/csu-yx-2013/" target="_blank">yx</a> 2011-12-04 22:20 <a href="http://www.cppblog.com/csu-yx-2013/archive/2011/12/04/161448.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>