﻿<?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++博客-cucumber</title><link>http://www.cppblog.com/cucumber/</link><description>cucumber</description><language>zh-cn</language><lastBuildDate>Fri, 10 Apr 2026 08:32:43 GMT</lastBuildDate><pubDate>Fri, 10 Apr 2026 08:32:43 GMT</pubDate><ttl>60</ttl><item><title>poj3259 WormHoles Spfa || BellmanFord</title><link>http://www.cppblog.com/cucumber/archive/2011/07/06/150263.html</link><dc:creator>cucumber</dc:creator><author>cucumber</author><pubDate>Tue, 05 Jul 2011 17:20:00 GMT</pubDate><guid>http://www.cppblog.com/cucumber/archive/2011/07/06/150263.html</guid><wfw:comment>http://www.cppblog.com/cucumber/comments/150263.html</wfw:comment><comments>http://www.cppblog.com/cucumber/archive/2011/07/06/150263.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cucumber/comments/commentRss/150263.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cucumber/services/trackbacks/150263.html</trackback:ping><description><![CDATA[1) Bellman Ford算法找负环的应用.<br /><br /><br /><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"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstdio</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />#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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">queue</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">deque</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img id="Codehighlighter1_105_151_Open_Image" onclick="this.style.display='none'; Codehighlighter1_105_151_Open_Text.style.display='none'; Codehighlighter1_105_151_Closed_Image.style.display='inline'; Codehighlighter1_105_151_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_105_151_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_105_151_Closed_Text.style.display='none'; Codehighlighter1_105_151_Open_Image.style.display='inline'; Codehighlighter1_105_151_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"> Node </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_105_151_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_105_151_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> to;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> weight;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    Node </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> MAXFIELD (1000 + 10)</span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> MAXPATH (2500 + 10)</span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> MAXWORMHOLE (200 + 10)</span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />Node nodeHead[MAXFIELD];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />Node nodes[MAXPATH </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> MAXWORMHOLE];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> dis[MAXFIELD </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> isInQueue[MAXFIELD </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> allocPos </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /><img id="Codehighlighter1_394_427_Open_Image" onclick="this.style.display='none'; Codehighlighter1_394_427_Open_Text.style.display='none'; Codehighlighter1_394_427_Closed_Image.style.display='inline'; Codehighlighter1_394_427_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_394_427_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_394_427_Closed_Text.style.display='none'; Codehighlighter1_394_427_Open_Image.style.display='inline'; Codehighlighter1_394_427_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" />Node </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">getNode() </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_394_427_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_394_427_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> nodes </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> allocPos</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="COLOR: #000000"><br /><img id="Codehighlighter1_451_575_Open_Image" onclick="this.style.display='none'; Codehighlighter1_451_575_Open_Text.style.display='none'; Codehighlighter1_451_575_Closed_Image.style.display='inline'; Codehighlighter1_451_575_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_451_575_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_451_575_Closed_Text.style.display='none'; Codehighlighter1_451_575_Open_Image.style.display='inline'; Codehighlighter1_451_575_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> initGraph(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n) </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_451_575_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_451_575_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    allocPos </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /><img id="Codehighlighter1_514_573_Open_Image" onclick="this.style.display='none'; Codehighlighter1_514_573_Open_Text.style.display='none'; Codehighlighter1_514_573_Closed_Image.style.display='inline'; Codehighlighter1_514_573_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_514_573_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_514_573_Closed_Text.style.display='none'; Codehighlighter1_514_573_Open_Image.style.display='inline'; Codehighlighter1_514_573_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (i </span><span style="COLOR: #000000">=</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"> n; </span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i) </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_514_573_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_514_573_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        nodeHead[i].next </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> NULL;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        dis[i] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />    }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="COLOR: #000000"><br /><img id="Codehighlighter1_622_785_Open_Image" onclick="this.style.display='none'; Codehighlighter1_622_785_Open_Text.style.display='none'; Codehighlighter1_622_785_Closed_Image.style.display='inline'; Codehighlighter1_622_785_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_622_785_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_622_785_Closed_Text.style.display='none'; Codehighlighter1_622_785_Open_Image.style.display='inline'; Codehighlighter1_622_785_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> addEdge(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> from, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> to, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> timeNeed) </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_622_785_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_622_785_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    Node </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">newNode </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> getNode();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    newNode</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> nodeHead[from].next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    newNode</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">to </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> to;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    newNode</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">weight </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> timeNeed;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    nodeHead[from].next </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> newNode;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img id="Codehighlighter1_799_2814_Open_Image" onclick="this.style.display='none'; Codehighlighter1_799_2814_Open_Text.style.display='none'; Codehighlighter1_799_2814_Closed_Image.style.display='inline'; Codehighlighter1_799_2814_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_799_2814_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_799_2814_Closed_Text.style.display='none'; Codehighlighter1_799_2814_Open_Image.style.display='inline'; Codehighlighter1_799_2814_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main() </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_799_2814_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_799_2814_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> caseCount, fieldCount, pathCount, wormHoleCount;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i, j, from, to, timeNeed;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">caseCount);<br /><img id="Codehighlighter1_957_2797_Open_Image" onclick="this.style.display='none'; Codehighlighter1_957_2797_Open_Text.style.display='none'; Codehighlighter1_957_2797_Closed_Image.style.display='inline'; Codehighlighter1_957_2797_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_957_2797_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_957_2797_Closed_Text.style.display='none'; Codehighlighter1_957_2797_Open_Image.style.display='inline'; Codehighlighter1_957_2797_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (i </span><span style="COLOR: #000000">=</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"> caseCount; i</span><span style="COLOR: #000000">++</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_957_2797_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_957_2797_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">fieldCount, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">pathCount, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">wormHoleCount);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        initGraph(fieldCount </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br /><img id="Codehighlighter1_1100_1244_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1100_1244_Open_Text.style.display='none'; Codehighlighter1_1100_1244_Closed_Image.style.display='inline'; Codehighlighter1_1100_1244_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_1100_1244_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1100_1244_Closed_Text.style.display='none'; Codehighlighter1_1100_1244_Open_Image.style.display='inline'; Codehighlighter1_1100_1244_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />        </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (j </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; j </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000"> pathCount; j</span><span style="COLOR: #000000">++</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_1100_1244_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_1100_1244_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">from, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">to, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">timeNeed);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            addEdge(from, to, timeNeed);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            addEdge(to, from, timeNeed);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />        }</span></span><span style="COLOR: #000000"><br /><img id="Codehighlighter1_1290_1394_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1290_1394_Open_Text.style.display='none'; Codehighlighter1_1290_1394_Closed_Image.style.display='inline'; Codehighlighter1_1290_1394_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_1290_1394_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1290_1394_Closed_Text.style.display='none'; Codehighlighter1_1290_1394_Open_Image.style.display='inline'; Codehighlighter1_1290_1394_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />        </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (j </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; j </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000"> wormHoleCount; </span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j) </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_1290_1394_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_1290_1394_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">from, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">to, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">timeNeed);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            addEdge(from, to, </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">timeNeed);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />        }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 关键: 按照题目的要求, 可以看出是找图中有没有负环<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 引入一个超级点s, s能够到达任意一个field, 但是没有任何field能够到达s<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 然后如果图中不存在负环, 则在经过fieldCount次松弛(我叫优化)以后, <br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 就没有办法使任意一个field节点的权值变小了, 而如果存在负环, <br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 则还能松弛/优化.<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 这就是为什么初始化时需要把所有的field都压入队列.</span><span style="COLOR: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="COLOR: #000000">        deque</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> q;<br /><img id="Codehighlighter1_1711_1782_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1711_1782_Open_Text.style.display='none'; Codehighlighter1_1711_1782_Closed_Image.style.display='inline'; Codehighlighter1_1711_1782_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_1711_1782_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1711_1782_Closed_Text.style.display='none'; Codehighlighter1_1711_1782_Open_Image.style.display='inline'; Codehighlighter1_1711_1782_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />        </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (j </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">; j </span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000"> fieldCount; </span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j) </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_1711_1782_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_1711_1782_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            q.push_back(j);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            isInQueue[j] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />        }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        </span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> answer </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />        </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> round </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /><img id="Codehighlighter1_1863_2684_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1863_2684_Open_Text.style.display='none'; Codehighlighter1_1863_2684_Closed_Image.style.display='inline'; Codehighlighter1_1863_2684_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_1863_2684_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1863_2684_Closed_Text.style.display='none'; Codehighlighter1_1863_2684_Open_Image.style.display='inline'; Codehighlighter1_1863_2684_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />        </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">q.empty()) </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_1863_2684_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_1863_2684_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> q.size();<br /><img id="Codehighlighter1_1931_2520_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1931_2520_Open_Text.style.display='none'; Codehighlighter1_1931_2520_Closed_Image.style.display='inline'; Codehighlighter1_1931_2520_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_1931_2520_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1931_2520_Closed_Text.style.display='none'; Codehighlighter1_1931_2520_Open_Image.style.display='inline'; Codehighlighter1_1931_2520_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />            </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (j </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; j </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000"> n; j</span><span style="COLOR: #000000">++</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_1931_2520_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_1931_2520_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> u </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> q.front();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                q.pop_front();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                isInQueue[u] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                Node </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">tra;<br /><img id="Codehighlighter1_2139_2506_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2139_2506_Open_Text.style.display='none'; Codehighlighter1_2139_2506_Closed_Image.style.display='inline'; Codehighlighter1_2139_2506_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_2139_2506_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_2139_2506_Closed_Text.style.display='none'; Codehighlighter1_2139_2506_Open_Image.style.display='inline'; Codehighlighter1_2139_2506_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />                </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (tra </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> nodeHead[u].next; tra </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> NULL; tra </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tra</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next) </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_2139_2506_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_2139_2506_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> temp </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tra</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">weight </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> dis[u];<br /><img id="Codehighlighter1_2239_2488_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2239_2488_Open_Text.style.display='none'; Codehighlighter1_2239_2488_Closed_Image.style.display='inline'; Codehighlighter1_2239_2488_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_2239_2488_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_2239_2488_Closed_Text.style.display='none'; Codehighlighter1_2239_2488_Open_Image.style.display='inline'; Codehighlighter1_2239_2488_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />                    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (temp </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000"> dis[tra</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">to]) </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_2239_2488_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_2239_2488_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                        dis[tra</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">to] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> temp;<br /><img id="Codehighlighter1_2335_2466_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2335_2466_Open_Text.style.display='none'; Codehighlighter1_2335_2466_Closed_Image.style.display='inline'; Codehighlighter1_2335_2466_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_2335_2466_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_2335_2466_Closed_Text.style.display='none'; Codehighlighter1_2335_2466_Open_Image.style.display='inline'; Codehighlighter1_2335_2466_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />                        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">isInQueue[tra</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">to]) </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_2335_2466_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_2335_2466_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                            q.push_back(tra</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">to);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                            isInQueue[tra</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">to] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />                        }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />                    }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />                }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />            }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            round</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br /><img id="Codehighlighter1_2579_2674_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2579_2674_Open_Text.style.display='none'; Codehighlighter1_2579_2674_Closed_Image.style.display='inline'; Codehighlighter1_2579_2674_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_2579_2674_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_2579_2674_Closed_Text.style.display='none'; Codehighlighter1_2579_2674_Open_Image.style.display='inline'; Codehighlighter1_2579_2674_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (round </span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> fieldCount) </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_2579_2674_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_2579_2674_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                answer </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                q.clear();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />                </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />            }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />        }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /><img id="Codehighlighter1_2707_2742_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2707_2742_Open_Text.style.display='none'; Codehighlighter1_2707_2742_Closed_Image.style.display='inline'; Codehighlighter1_2707_2742_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_2707_2742_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_2707_2742_Closed_Text.style.display='none'; Codehighlighter1_2707_2742_Open_Image.style.display='inline'; Codehighlighter1_2707_2742_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (answer) </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_2707_2742_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_2707_2742_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">YES</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />        }</span></span><span style="COLOR: #000000"><br /><img id="Codehighlighter1_2757_2791_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2757_2791_Open_Text.style.display='none'; Codehighlighter1_2757_2791_Closed_Image.style.display='inline'; Codehighlighter1_2757_2791_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="DISPLAY: none" id="Codehighlighter1_2757_2791_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_2757_2791_Closed_Text.style.display='none'; Codehighlighter1_2757_2791_Open_Image.style.display='inline'; Codehighlighter1_2757_2791_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" />        </span><span style="COLOR: #0000ff">else</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_2757_2791_Closed_Text"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_2757_2791_Open_Text"><span style="COLOR: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />            puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">NO</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />        }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />    }</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="COLOR: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span></div><img src ="http://www.cppblog.com/cucumber/aggbug/150263.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cucumber/" target="_blank">cucumber</a> 2011-07-06 01:20 <a href="http://www.cppblog.com/cucumber/archive/2011/07/06/150263.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>poj1062 昂贵的婚礼</title><link>http://www.cppblog.com/cucumber/archive/2011/07/06/150262.html</link><dc:creator>cucumber</dc:creator><author>cucumber</author><pubDate>Tue, 05 Jul 2011 16:58:00 GMT</pubDate><guid>http://www.cppblog.com/cucumber/archive/2011/07/06/150262.html</guid><wfw:comment>http://www.cppblog.com/cucumber/comments/150262.html</wfw:comment><comments>http://www.cppblog.com/cucumber/archive/2011/07/06/150262.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cucumber/comments/commentRss/150262.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cucumber/services/trackbacks/150262.html</trackback:ping><description><![CDATA[Dijkstra 未优化版, 算法相对清晰:<br /><br />
<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: #008000">//</span><span style="color: #008000">&nbsp;关键1:&nbsp;处理每个人的地位等级<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;办法:&nbsp;枚举--假设某种方案是最省钱的,&nbsp;<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;则该方案中的所有交易者的地位等级都会落在一个宽度为rankLimit的区间<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;于是可以枚举这个区间:&nbsp;<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;[ownerRank[1]&nbsp;-&nbsp;rankLimit,&nbsp;ownerRank]&nbsp;~&nbsp;[ownerRank[1],&nbsp;ownerRank&nbsp;+&nbsp;rankLimit]<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;于是这道题考察了最短路的dijkstra算法与枚举的结合.<br /></span><span style="color: #008000">//</span><span style="color: #008000"><br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;其中枚举可行是需要考察其复杂度的:&nbsp;<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;dijkstra算法的复杂度为:&nbsp;O(n&nbsp;*&nbsp;n),&nbsp;n为节点数目<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;枚举量为&nbsp;rankLimit&nbsp;+&nbsp;1;<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;于是枚举&nbsp;+&nbsp;dijkstra的算法复杂度为&nbsp;O(n&nbsp;*&nbsp;n)&nbsp;*&nbsp;(rankLimit&nbsp;+&nbsp;1)<br /><br /><br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;关键2:&nbsp;由题意要联想到用最短路,&nbsp;而且是边权为正的最短路<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;1)&nbsp;以物品为图节点<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;2)&nbsp;设i物品如果能用j物品以价格m交换,&nbsp;则边(i,j)的权值为m<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;3)&nbsp;设求得节点1到物品x的最短路,&nbsp;该最短路的权值和为tw(total&nbsp;weight的缩写),&nbsp;<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;则从物品x开始物物交换的所有方案中,&nbsp;最节省的方案会耗费tw&nbsp;+&nbsp;price[x]的金钱<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;而婚礼最少需要的金币数就是所有&nbsp;1&nbsp;&lt;=&nbsp;x&nbsp;&lt;=&nbsp;goodsCount&nbsp;中,&nbsp;<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;tw[1][x]&nbsp;+&nbsp;price[x]最小的那个.&nbsp;(tw[1][x]表示1到x的最短路径权值)<br /><br /><br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;优化1:&nbsp;在dijkstra算法的代码部分,&nbsp;需要对原点到节点的最小距离是否已知作出判断.<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;这个判断是用bool数组disKnown来判断的,&nbsp;浪费大量时间.<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;可以优化为添加一个数组,&nbsp;用该数组保存最小距离未知的节点的编号.&nbsp;<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;只处理数组中的节点.</span><span style="color: #008000"><br /></span><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></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 /><br /></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;Node&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;to;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;weight;<br />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">next;<br />};<br /><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;INF&nbsp;(1&nbsp;&lt;&lt;&nbsp;30)</span><span style="color: #000000"><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MAXNODE&nbsp;(100&nbsp;+&nbsp;10)</span><span style="color: #000000"><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MAXEDGE&nbsp;(MAXNODE&nbsp;*&nbsp;MAXNODE&nbsp;+&nbsp;10)</span><span style="color: #000000"><br />Node&nbsp;nodeHead[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br />Node&nbsp;nodes[MAXEDGE];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;ownerRank[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;price[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;minWeight[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;disKnown[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;allocPos&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">getNode()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;nodes&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;allocPos</span><span style="color: #000000">++</span><span style="color: #000000">;<br />}<br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;initGraph(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;allocPos&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nodeHead[i].next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;NULL;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeight[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;INF;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;addEdge(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;from,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;to,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;weight)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">newNode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;getNode();<br />&nbsp;&nbsp;&nbsp;&nbsp;newNode</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;nodeHead[from].next;<br />&nbsp;&nbsp;&nbsp;&nbsp;newNode</span><span style="color: #000000">-&gt;</span><span style="color: #000000">to&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;to;<br />&nbsp;&nbsp;&nbsp;&nbsp;newNode</span><span style="color: #000000">-&gt;</span><span style="color: #000000">weight&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;weight;<br />&nbsp;&nbsp;&nbsp;&nbsp;nodeHead[from].next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;newNode;<br />}<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;rankLimit,&nbsp;goodsCount,&nbsp;substituteCount,&nbsp;subPrice,&nbsp;num,&nbsp;minPrice,&nbsp;minWei;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;minWeiPos;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,&nbsp;j,&nbsp;rankStart;<br />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">rankLimit,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">goodsCount);<br />&nbsp;&nbsp;&nbsp;&nbsp;initGraph(goodsCount&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;goodsCount;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;price&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;i,&nbsp;ownerRank&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;i,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">substituteCount);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;substituteCount;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">num,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">subPrice);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addEdge(i,&nbsp;num,&nbsp;subPrice);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;minPrice&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;price[</span><span style="color: #000000">1</span><span style="color: #000000">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(rankStart&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;ownerRank[</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;rankLimit;&nbsp;rankStart&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;ownerRank[</span><span style="color: #000000">1</span><span style="color: #000000">];&nbsp;rankStart</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;goodsCount;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeight[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;INF;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 如果某个节点/商品拥有者的阶级地位不在[rankStart, rankStart + rankLimit]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 的范围内, 就不必考虑该节点<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(ownerRank[i]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;rankStart&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;ownerRank[i]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;rankStart&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;rankLimit)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disKnown[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<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;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disKnown[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disKnown[</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeight[</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;goodsCount;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWei&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;INF;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;goodsCount;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)&nbsp;{<br />&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">&nbsp;(</span><span style="color: #000000">!</span><span style="color: #000000">disKnown[j]&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;minWeight[j]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;minWei)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWei&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;minWeight[j];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeiPos&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;j;<br />&nbsp;&nbsp;&nbsp;&nbsp;&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;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disKnown[minWeiPos]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(minWei&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;price[minWeiPos]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;minPrice)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minPrice&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;minWei&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;price[minWeiPos];<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;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">tra&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;nodeHead[minWeiPos].next;&nbsp;tra&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;NULL;&nbsp;tra&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next)&nbsp;{<br />&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">&nbsp;(</span><span style="color: #000000">!</span><span style="color: #000000">disKnown[tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">to]&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeight[tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">to]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;minWeight[minWeiPos]&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">weight&nbsp;)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeight[tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">to]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;minWeight[minWeiPos]&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">weight;<br />&nbsp;&nbsp;&nbsp;&nbsp;&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;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;minPrice);<br /><br />&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></div><br /><br /><br />优化后, 速度要快一些, 但是代码比较难看, 对变量的命名让人比较恼火:<br /><br />
<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: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></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 /><br /></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;Node&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;to;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;weight;<br />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">next;<br />};<br /><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;INF&nbsp;(1&nbsp;&lt;&lt;&nbsp;30)</span><span style="color: #000000"><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MAXNODE&nbsp;(100&nbsp;+&nbsp;10)</span><span style="color: #000000"><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MAXEDGE&nbsp;(MAXNODE&nbsp;*&nbsp;MAXNODE&nbsp;+&nbsp;10)</span><span style="color: #000000"><br />Node&nbsp;nodeHead[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br />Node&nbsp;nodes[MAXEDGE];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;ownerRank[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;price[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;minWeight[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;distanceUnknown[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;distanceUnknownCount;<br /></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;isDistanceKnown[MAXNODE&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">];<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;allocPos&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">getNode()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;nodes&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;allocPos</span><span style="color: #000000">++</span><span style="color: #000000">;<br />}<br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;initGraph(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;allocPos&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nodeHead[i].next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;NULL;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeight[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;INF;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;addEdge(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;from,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;to,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;weight)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">newNode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;getNode();<br />&nbsp;&nbsp;&nbsp;&nbsp;newNode</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;nodeHead[from].next;<br />&nbsp;&nbsp;&nbsp;&nbsp;newNode</span><span style="color: #000000">-&gt;</span><span style="color: #000000">to&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;to;<br />&nbsp;&nbsp;&nbsp;&nbsp;newNode</span><span style="color: #000000">-&gt;</span><span style="color: #000000">weight&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;weight;<br />&nbsp;&nbsp;&nbsp;&nbsp;nodeHead[from].next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;newNode;<br />}<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;rankLimit,&nbsp;goodsCount,&nbsp;substituteCount,&nbsp;subPrice,&nbsp;num,&nbsp;minPrice,&nbsp;minWei;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;minWeiDisUnkPos;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,&nbsp;j,&nbsp;from;<br />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">rankLimit,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">goodsCount);<br />&nbsp;&nbsp;&nbsp;&nbsp;initGraph(goodsCount&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;goodsCount;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;price&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;i,&nbsp;ownerRank&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;i,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">substituteCount);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;substituteCount;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">num,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">subPrice);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addEdge(i,&nbsp;num,&nbsp;subPrice);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;minPrice&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;price[</span><span style="color: #000000">1</span><span style="color: #000000">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(from&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;ownerRank[</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;rankLimit;&nbsp;from&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;ownerRank[</span><span style="color: #000000">1</span><span style="color: #000000">];&nbsp;from</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;goodsCount;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeight[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;INF;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;distanceUnknownCount&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;goodsCount;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(ownerRank[i]&nbsp;</span><span style="color: #000000">&gt;=</span><span style="color: #000000">&nbsp;from&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;ownerRank[i]&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;from&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;rankLimit)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;distanceUnknown[distanceUnknownCount</span><span style="color: #000000">++</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;i;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isDistanceKnown[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<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;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isDistanceKnown[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeight[</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isDistanceKnown[</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;distanceUnknownCount;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWei&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;INF;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;distanceUnknownCount;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)&nbsp;{<br />&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">&nbsp;(minWeight[&nbsp;distanceUnknown[j]&nbsp;]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;minWei)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWei&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;minWeight[&nbsp;distanceUnknown[j]&nbsp;];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeiDisUnkPos&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;j;<br />&nbsp;&nbsp;&nbsp;&nbsp;&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;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(minWei&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;price[&nbsp;distanceUnknown[minWeiDisUnkPos]&nbsp;]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;minPrice)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minPrice&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;minWei&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;price[&nbsp;distanceUnknown[minWeiDisUnkPos]&nbsp;];<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;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">tra&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;nodeHead[&nbsp;distanceUnknown[minWeiDisUnkPos]&nbsp;].next;&nbsp;tra&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;NULL;&nbsp;tra&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next)&nbsp;{<br />&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">&nbsp;(</span><span style="color: #000000">!</span><span style="color: #000000">isDistanceKnown[tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">to]&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeight[tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">to]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;minWeight[&nbsp;distanceUnknown[minWeiDisUnkPos]&nbsp;]&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">weight&nbsp;)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minWeight[tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">to]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;minWeight[&nbsp;distanceUnknown[minWeiDisUnkPos]&nbsp;]&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;tra</span><span style="color: #000000">-&gt;</span><span style="color: #000000">weight;<br />&nbsp;&nbsp;&nbsp;&nbsp;&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;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isDistanceKnown[&nbsp;distanceUnknown[minWeiDisUnkPos]&nbsp;]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;distanceUnknown[minWeiDisUnkPos]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;distanceUnknown[</span><span style="color: #000000">--</span><span style="color: #000000">distanceUnknownCount];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;minPrice);<br /><br />&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 />}<br /><br /><br /></span></div><img src ="http://www.cppblog.com/cucumber/aggbug/150262.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cucumber/" target="_blank">cucumber</a> 2011-07-06 00:58 <a href="http://www.cppblog.com/cucumber/archive/2011/07/06/150262.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>poj1860 Currency Exchange: spfa / Bellman Ford</title><link>http://www.cppblog.com/cucumber/archive/2011/07/04/150078.html</link><dc:creator>cucumber</dc:creator><author>cucumber</author><pubDate>Mon, 04 Jul 2011 01:18:00 GMT</pubDate><guid>http://www.cppblog.com/cucumber/archive/2011/07/04/150078.html</guid><wfw:comment>http://www.cppblog.com/cucumber/comments/150078.html</wfw:comment><comments>http://www.cppblog.com/cucumber/archive/2011/07/04/150078.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cucumber/comments/commentRss/150078.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cucumber/services/trackbacks/150078.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 1) 实质就是是确定图中是否存在正环--沿着这个环一直走能够使环内节点的权值无限增长.2) 特点是: 每个边的权值是动态变化的, 这点提醒我们适合用Bellman Ford算法来处理(SPFA实质上是Bellman Ford的优化, 算法思想是一样的).SPFA算法: Code highlighting produced by Actipro CodeHighlighter (freeware...&nbsp;&nbsp;<a href='http://www.cppblog.com/cucumber/archive/2011/07/04/150078.html'>阅读全文</a><img src ="http://www.cppblog.com/cucumber/aggbug/150078.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cucumber/" target="_blank">cucumber</a> 2011-07-04 09:18 <a href="http://www.cppblog.com/cucumber/archive/2011/07/04/150078.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>