﻿<?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++博客-DjvuLee-文章分类-算法设计</title><link>http://www.cppblog.com/DjvuLee/category/18687.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 23 Feb 2012 11:44:51 GMT</lastBuildDate><pubDate>Thu, 23 Feb 2012 11:44:51 GMT</pubDate><ttl>60</ttl><item><title>最小花费[THU@2011研究生机试题]</title><link>http://www.cppblog.com/DjvuLee/articles/166222.html</link><dc:creator>DjvuLee</dc:creator><author>DjvuLee</author><pubDate>Wed, 22 Feb 2012 03:40:00 GMT</pubDate><guid>http://www.cppblog.com/DjvuLee/articles/166222.html</guid><wfw:comment>http://www.cppblog.com/DjvuLee/comments/166222.html</wfw:comment><comments>http://www.cppblog.com/DjvuLee/articles/166222.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/DjvuLee/comments/commentRss/166222.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/DjvuLee/services/trackbacks/166222.html</trackback:ping><description><![CDATA[<div><a>//@Jobdo</a> DY问题
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;INF&nbsp;-1</span><span style="color: #008000">//</span><span style="color: #008000">定义为无穷大</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />typedef&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;T;<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />T&nbsp;</span><span style="color: #000000">**</span><span style="color: #000000">m;<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />T&nbsp;L[</span><span style="color: #000000">4</span><span style="color: #000000">];</span><span style="color: #008000">//</span><span style="color: #008000">距离</span><span style="color: #008000"><br /></span><span style="color: #008080">11</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000">T&nbsp;C[</span><span style="color: #000000">4</span><span style="color: #000000">];</span><span style="color: #008000">//</span><span style="color: #008000">花费</span><span style="color: #008000"><br /></span><span style="color: #008080">12</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000">T&nbsp;A,B,n;<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />T&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">v;</span><span style="color: #008000">//</span><span style="color: #008000">第一站到其他站的距离</span><span style="color: #008000"><br /></span><span style="color: #008080">14</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000"><br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />T&nbsp;MinCost();<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img id="Codehighlighter1_211_585_Open_Image" onclick="this.style.display='none'; Codehighlighter1_211_585_Open_Text.style.display='none'; Codehighlighter1_211_585_Closed_Image.style.display='inline'; Codehighlighter1_211_585_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_211_585_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_211_585_Closed_Text.style.display='none'; Codehighlighter1_211_585_Open_Image.style.display='inline'; Codehighlighter1_211_585_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_211_585_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_211_585_Open_Text"><span style="color: #000000">{&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img id="Codehighlighter1_243_560_Open_Image" onclick="this.style.display='none'; Codehighlighter1_243_560_Open_Text.style.display='none'; Codehighlighter1_243_560_Closed_Image.style.display='inline'; Codehighlighter1_243_560_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_243_560_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_243_560_Closed_Text.style.display='none'; Codehighlighter1_243_560_Open_Image.style.display='inline'; Codehighlighter1_243_560_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">L[</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">L[</span><span style="color: #000000">2</span><span style="color: #000000">]</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">L[</span><span style="color: #000000">3</span><span style="color: #000000">])</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_243_560_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_243_560_Open_Text"><span style="color: #000000">{</span><span style="color: #008000">//</span><span style="color: #008000">输入三种路程</span><span style="color: #008000"><br /></span><span style="color: #008080">19</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">3</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;输入三种票价</span><span style="color: #008000"><br /></span><span style="color: #008080">20</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">C[i];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">A</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">B</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">n;<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img id="Codehighlighter1_331_366_Open_Image" onclick="this.style.display='none'; Codehighlighter1_331_366_Open_Text.style.display='none'; Codehighlighter1_331_366_Closed_Image.style.display='inline'; Codehighlighter1_331_366_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_331_366_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_331_366_Closed_Text.style.display='none'; Codehighlighter1_331_366_Open_Image.style.display='inline'; Codehighlighter1_331_366_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(A</span><span style="color: #000000">==</span><span style="color: #000000">B)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_331_366_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_331_366_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">0</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br /></span><span style="color: #008080">26</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">27</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(A</span><span style="color: #000000">&gt;</span><span style="color: #000000">B)<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swap(A,B);<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v</span><span style="color: #000000">=</span><span style="color: #000000">(T</span><span style="color: #000000">*</span><span style="color: #000000">)malloc(</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(T)</span><span style="color: #000000">*</span><span style="color: #000000">(n</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">));<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v[</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;</span><span style="color: #008000">//</span><span style="color: #008000">第一站到第一站的距离</span><span style="color: #008000"><br /></span><span style="color: #008080">31</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(T&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">2</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;i</span><span style="color: #000000">++</span><span style="color: #000000">)</span><span style="color: #008000">//</span><span style="color: #008000">读入第一站到其他各站的距离&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">32</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">v[i];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">33</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">MinCost()</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /></span><span style="color: #008080">34</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(v);<br /></span><span style="color: #008080">35</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #008000">//</span><span style="color: #008000">end&nbsp;while</span><span style="color: #008000"><br /></span><span style="color: #008080">36</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: #000000"><br /></span><span style="color: #008080">37</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">38</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">39</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />T&nbsp;MinCost()<br /></span><span style="color: #008080">40</span><span style="color: #000000"><img id="Codehighlighter1_599_1434_Open_Image" onclick="this.style.display='none'; Codehighlighter1_599_1434_Open_Text.style.display='none'; Codehighlighter1_599_1434_Closed_Image.style.display='inline'; Codehighlighter1_599_1434_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_599_1434_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_599_1434_Closed_Text.style.display='none'; Codehighlighter1_599_1434_Open_Image.style.display='inline'; Codehighlighter1_599_1434_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_599_1434_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_599_1434_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">41</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;m</span><span style="color: #000000">=</span><span style="color: #000000">(T&nbsp;</span><span style="color: #000000">**</span><span style="color: #000000">)malloc(</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(T&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">)</span><span style="color: #000000">*</span><span style="color: #000000">(B</span><span style="color: #000000">+</span><span style="color: #000000">10</span><span style="color: #000000">));</span><span style="color: #008000">//</span><span style="color: #008000">+10多分配几个空间</span><span style="color: #008000"><br /></span><span style="color: #008080">42</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">B</span><span style="color: #000000">+</span><span style="color: #000000">10</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">43</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m[i]</span><span style="color: #000000">=</span><span style="color: #000000">(T&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">)malloc(</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(T)</span><span style="color: #000000">*</span><span style="color: #000000">(B</span><span style="color: #000000">+</span><span style="color: #000000">10</span><span style="color: #000000">));<br /></span><span style="color: #008080">44</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">45</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;len</span><span style="color: #000000">=</span><span style="color: #000000">B</span><span style="color: #000000">-</span><span style="color: #000000">A</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;</span><span style="color: #008000">//</span><span style="color: #008000">A<img src="http://www.cppblog.com/Images/dot.gif" alt="" />B之间的站数</span><span style="color: #008000"><br /></span><span style="color: #008080">46</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(T&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">A;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">B;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">47</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m[i][i]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;</span><span style="color: #008000">//</span><span style="color: #008000">m[i][j]:i站到j站的最小花费</span><span style="color: #008000"><br /></span><span style="color: #008080">48</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">49</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(T&nbsp;pace</span><span style="color: #000000">=</span><span style="color: #000000">2</span><span style="color: #000000">;pace</span><span style="color: #000000">&lt;=</span><span style="color: #000000">len;pace</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">50</span><span style="color: #000000"><img id="Codehighlighter1_831_1348_Open_Image" onclick="this.style.display='none'; Codehighlighter1_831_1348_Open_Text.style.display='none'; Codehighlighter1_831_1348_Closed_Image.style.display='inline'; Codehighlighter1_831_1348_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_831_1348_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_831_1348_Closed_Text.style.display='none'; Codehighlighter1_831_1348_Open_Image.style.display='inline'; Codehighlighter1_831_1348_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_831_1348_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_831_1348_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">51</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(T&nbsp;pos</span><span style="color: #000000">=</span><span style="color: #000000">A;pos</span><span style="color: #000000">&lt;=</span><span style="color: #000000">B</span><span style="color: #000000">-</span><span style="color: #000000">pace</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;pos</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">52</span><span style="color: #000000"><img id="Codehighlighter1_870_1345_Open_Image" onclick="this.style.display='none'; Codehighlighter1_870_1345_Open_Text.style.display='none'; Codehighlighter1_870_1345_Closed_Image.style.display='inline'; Codehighlighter1_870_1345_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_870_1345_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_870_1345_Closed_Text.style.display='none'; Codehighlighter1_870_1345_Open_Image.style.display='inline'; Codehighlighter1_870_1345_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_870_1345_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_870_1345_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">53</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;distance</span><span style="color: #000000">=</span><span style="color: #000000">v[pace</span><span style="color: #000000">+</span><span style="color: #000000">pos</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">-</span><span style="color: #000000">v[pos];<br /></span><span style="color: #008080">54</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(distance</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;distance</span><span style="color: #000000">&lt;=</span><span style="color: #000000">L[</span><span style="color: #000000">1</span><span style="color: #000000">])<br /></span><span style="color: #008080">55</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m[pos][pace</span><span style="color: #000000">+</span><span style="color: #000000">pos</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">C[</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #008080">56</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(distance</span><span style="color: #000000">&gt;</span><span style="color: #000000">L[</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;distance</span><span style="color: #000000">&lt;=</span><span style="color: #000000">L[</span><span style="color: #000000">2</span><span style="color: #000000">])<br /></span><span style="color: #008080">57</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m[pos][pace</span><span style="color: #000000">+</span><span style="color: #000000">pos</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">C[</span><span style="color: #000000">2</span><span style="color: #000000">];<br /></span><span style="color: #008080">58</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(distance</span><span style="color: #000000">&gt;</span><span style="color: #000000">L[</span><span style="color: #000000">2</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;distance</span><span style="color: #000000">&lt;=</span><span style="color: #000000">L[</span><span style="color: #000000">3</span><span style="color: #000000">])<br /></span><span style="color: #008080">59</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m[pos][pace</span><span style="color: #000000">+</span><span style="color: #000000">pos</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">C[</span><span style="color: #000000">3</span><span style="color: #000000">];<br /></span><span style="color: #008080">60</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(distance</span><span style="color: #000000">&gt;</span><span style="color: #000000">L[</span><span style="color: #000000">3</span><span style="color: #000000">])<br /></span><span style="color: #008080">61</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m[pos][pace</span><span style="color: #000000">+</span><span style="color: #000000">pos</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=-</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">62</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">63</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(T&nbsp;k</span><span style="color: #000000">=</span><span style="color: #000000">pos</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;k</span><span style="color: #000000">&lt;</span><span style="color: #000000">pos</span><span style="color: #000000">+</span><span style="color: #000000">pace</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;k</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">64</span><span style="color: #000000"><img id="Codehighlighter1_1215_1341_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1215_1341_Open_Text.style.display='none'; Codehighlighter1_1215_1341_Closed_Image.style.display='inline'; Codehighlighter1_1215_1341_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1215_1341_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1215_1341_Closed_Text.style.display='none'; Codehighlighter1_1215_1341_Open_Image.style.display='inline'; Codehighlighter1_1215_1341_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1215_1341_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_1215_1341_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">65</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;tp</span><span style="color: #000000">=</span><span style="color: #000000">m[pos][k]</span><span style="color: #000000">+</span><span style="color: #000000">m[k][pos</span><span style="color: #000000">+</span><span style="color: #000000">pace</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #008080">66</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(m[pos][pace</span><span style="color: #000000">+</span><span style="color: #000000">pos</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">==-</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;tp</span><span style="color: #000000">&lt;</span><span style="color: #000000">m[pos][pace</span><span style="color: #000000">+</span><span style="color: #000000">pos</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">])<br /></span><span style="color: #008080">67</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m[pos][pace</span><span style="color: #000000">+</span><span style="color: #000000">pos</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">tp;<br /></span><span style="color: #008080">68</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">69</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">70</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">71</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;result</span><span style="color: #000000">=</span><span style="color: #000000">m[A][B];<br /></span><span style="color: #008080">72</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(T&nbsp;t</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;t</span><span style="color: #000000">&lt;</span><span style="color: #000000">B</span><span style="color: #000000">+</span><span style="color: #000000">10</span><span style="color: #000000">;t</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">73</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(m[t]);<br /></span><span style="color: #008080">74</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;free(m);<br /></span><span style="color: #008080">75</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">76</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;result;<br /></span><span style="color: #008080">77</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />}</span></span></div></div>  <div class="vimiumHUD" style="right: 150px; opacity: 0; display: none; "></div><img src ="http://www.cppblog.com/DjvuLee/aggbug/166222.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/DjvuLee/" target="_blank">DjvuLee</a> 2012-02-22 11:40 <a href="http://www.cppblog.com/DjvuLee/articles/166222.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>